Mongoose Helper
Generate Mongoose schema based on your domain model.
#
Enable The FunctionalityMongoDB 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
MongooseFacility
constructor, like example above. - By providing the environment variable named
PLUM_MONGODB_URI
. This can be achieve using.env
file or by set the environment variable manually. - If none above provided, connection should be done manually using
mongoose.connect()
function.
#
Domain Model DeclarationPlumier Mongoose Helper uses reflection to extract type metadata on runtime. Currently there are two domain models declaration supported
#
Using Property FieldThis 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.
#
Using TypeScript Parameter PropertiesThis declaration good when tsconfig.json
uses strict: true
because we unable to use field properties. Using this declaration reduce the need of using @collection.properties()
on all properties.
#
Helper API OverviewPlumier 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.
#
Helper API#
Basic Schema Generation#
Advanced Data TypeArray type required extra decorator information. Use @reflect.type([<type>])
decorator to inform generator about extra type information.
#
Nested Document With Ref (Populate)#
Configure PropertiesExtra Mongoose schema configuration can be passed to each decorator
#
InheritanceInheritance 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 createdAt
and updatedAt
which automatically populated as timestamps.
#
Custom Model Name#
Schema Generation HookIts possible to provide hook when mongoose schema generated, so its possible to register the mongoose middleware from provided schema like below:
#
PreSave DecoratorYou 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.
#
Relation with Cyclic DependencyIts possible to map relation with cyclic dependency using mongoose helper using proxy
method. proxy
will defer schema generation until its first accessed, thus make it able to get the proper data type.
info
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.
#
Unique ValidationMongoose helper provided @val.unique()
that augmented (merged) with @plumier/validator
module. Means if you install @plumier/mongoose
@val
decorator will automatically has unique()
function.
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.
#
POST Form With Relational DataMongoose 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: