Mongo
Mongo module built with appolo-mongo and mongoose
Installation#
Options#
| key | Description | Type | Default |
|---|---|---|---|
id | ModelRepository injection id | string | modelRepository |
connection | mongo connection string | string | null |
config | mongose connection options | object | {} |
exitOnDisconnect | `call process.exit on disconnect | boolean | false ` |
Default Connections Options#
| key | Default |
|---|---|
keepAlive | true |
useNewUrlParser | true |
useCreateIndex | true |
autoReconnect | true |
reconnectTries | Number.MAX_VALUE |
reconnectInterval | 500 |
in config/modules/all.ts
Usage#
Schema#
options#
@schema(collectionName:string,options:SchemaOptions)#
define new schema with collectionName and mongose schema options
prop#
The prop decorator define class property to the Mongoose schema as a property
sub document#
define sumDocument
ref#
add a ref to another mongoose schema
the ref schema must be defined as model with model
arrays#
define any field as array using []
required#
define required field
index#
define index field
unique#
define unique field
enum#
define enum field
default#
define field with default
validate#
validate using minlength / maxlength / match / min /max same as mongoose
custom validate#
or use custom validator same as mongoose
virtual#
define virtual getter setters as in mongoose
static method#
define static method as in mongoose the method will be created on the mongose model
you need added the typeof User in order to use the static method findByName
instance Method#
define instance method as in mongoose instance methods are created on mongoose document
pre#
define mongoose pre hooks the pre function will be executed before the defined hook
post#
define mongoose post hooks the post function will be executed after the defined hook
Model#
define model#
register new schema model
inject model#
inject model to class
Schema Api#
collectionName#
get the defined collection name on schema
getSchema#
return mongoose schema object
getModel#
define and mongoose model instance by mongoose connection
ModelRepository#
with modelRepository you can access to the mongoose connection and all the models
connection#
getter return mongoose connection
getModel#
getModel<T>(model: typeof Schema): Model<T>#
return mongoose model by schema type