Mongo
Mongo module built with appolo-mongo and mongoose
#
Installation#
Optionskey | 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 Optionskey | 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
#
propThe prop decorator define class property to the Mongoose schema as a property
#
sub documentdefine sumDocument
#
refadd a ref to another mongoose schema
the ref schema must be defined as model with model
#
arraysdefine any field as array using []
#
requireddefine required field
#
indexdefine index field
#
uniquedefine unique field
#
enumdefine enum field
#
defaultdefine field with default
#
validatevalidate using minlength / maxlength / match / min /max same as mongoose
#
custom validateor use custom validator same as mongoose
#
virtualdefine virtual getter setters as in mongoose
#
static methoddefine 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 Methoddefine instance method as in mongoose instance methods are created on mongoose document
#
predefine mongoose pre hooks the pre function will be executed before the defined hook
#
postdefine mongoose post hooks the post function will be executed after the defined hook
#
Model#
define modelregister new schema model
#
inject modelinject model to class
#
Schema Api#
collectionNameget the defined collection name on schema
#
getSchemareturn mongoose schema object
#
getModeldefine and mongoose model instance by mongoose connection
#
ModelRepositorywith modelRepository you can access to the mongoose connection and all the models
#
connectiongetter return mongoose connection
#
getModel#
getModel<T>(model: typeof Schema): Model<T>return mongoose model by schema type