Generate Mongoose schema based on your domain model.
MongoDB helper is optional in Plumier, it can be enabled by installing
@plumier/mongoose module and plug
MongooseFacility into Plumier application.
Mongoose facility will automatically connect to the MongoDB database and make sure it ready before application started.
There are several ways to use the mongodb connection:
- By providing the uri on the
MongooseFacilityconstructor, like example above.
- By providing the environment variable named
PLUM_MONGODB_URI. This can be achieve using
.envfile or by set the environment variable manually.
- If none above provided, connection should be done manually using
Plumier Mongoose Helper uses reflection to extract type metadata on runtime. Currently there are two domain models declaration supported
This is the common model declaration when you are familiar with Nest.js or other TypeScript framework. This declaration required
strictPropertyInitialization disabled on
tsconfig.json file. Note that the
@collection.property() is required when there are no decorator applied on the property.
This declaration good when
strict: true because we unable to use field properties. Using this declaration reduce the need of using
@collection.properties() on all properties.
Plumier Mongoose Helper help you easily map your domain model and create Mongoose model using it. Helper automatically generate schema definition based on your domain model metadata.
Array type required extra decorator information. Use
@reflect.type([<type>]) decorator to inform generator about extra type information.
Extra Mongoose schema configuration can be passed to each decorator
Inheritance work naturally, all child document will inherit parent configuration properly.
Using configuration above, all class inherited from
DomainBase will have
deleted property with default value
false and properties
updatedAt which automatically populated as timestamps.
Its possible to provide hook when mongoose schema generated, so its possible to register the mongoose middleware from provided schema like below:
You can add hook during schema generation, but for simple use case to hash password before saving is too messy if using hook and
pre middleware. Plumier provided
@collection.preSave() decorator to automatically call decorated method before save.
Its possible to map relation with cyclic dependency using mongoose helper using
proxy will defer schema generation until its first accessed, thus make it able to get the proper data type.
Note when you define model with cyclic dependency its required to use
Ref<T> data type and use callback on the
@collection.ref() parameter to prevent TypeScript
ReferenceError: Model is not defined error.
Mongoose helper provided
@val.unique() that augmented (merged) with
@plumier/validator module. Means if you install
@val decorator will automatically has
This function is not using the mongoose
unique schema, but it automatically check to the database for uniqueness, so validation engine can execute the validation rule without touching controller.
Mongoose helper provided custom object converter, so it possible to post relational data (with populate) from HTML Form by providing the ObjectId of the child model.
Above code showing that we created a route named
POST /animal/save which will save Animal information with relational data which is images data that previously saved. Below request will be valid: