@fjsolutions/adonis-mongoose v2.0.1
adonis-mongoose
An AdonisJS 5 provider for Mongoose.
npm install @fjsolutions/adonis-mongoose --saveAfter installation completes run the following to setup the package in the project:
node ace invoke @fjsolutions/adonis-mongooseConfiguration file
A skeleton configuration file is created by the package. It contains a configuration array with a single bare-bones configuration instance.
Each configuration has a name attribute. If there is a single configuration and the name is not supplied then it is set to 'Default'. But if there are multiple configurations in the array one must be named 'Default' and is set as the initial connection instance.
The provider initializes each of the connections with its configuration as part of its boot sequence, so if any of the configurations contain errors or the host cannot be reached it will be detected early.
The AdonisMongoose instance
The AdonisMongoose instance returned from importing @ioc:Providers/Mongoose is a singleton, and has two methods:
model()for creatingMongoosemodels.use()which returns the namedMongooseconnection.
All other interaction with MongoDB is done through the Model.
Features
The module provides Mongoose based models. It contains:
- A provider to boot
Mongooseand add it to the IOC container (@ioc:Providers/Mongoose). - A
mongoose:modelcommand for generatingMongoosemodels inAdonis. existsanduniquevalidation rules.- A strongly typed configuration system that can configure multiple Mongoose connections.
- Contracts for intellisense with
@ioc:Providers/Mongooseand@ioc:Adonis/Core/Validator.
Classes
src/Providers/MongooseProvider.ts- The mongoose provider for AdonisJS 5src/validation/index.ts- The file that contains the database validators:existsandunique(they are registered by the provider).templates/validator.ts.mustache- (Template) The interface for the extended database validators.templates/mongoose.ts.mustache- (Template) The interface for the MongooseProvider returned from theioccontainer.templates/database.ts.mustache- (Template) The MongoDB database configuration file.commands/MongooseModel.ts- Theacecommand for creating Mongoose models.commands/templates/model-template.mustache- Themustachetemplate files.
TODO Still:
src/commands/Seeder.ts- Create amongooseseeder class to populate the database.src/commands/Seed.ts- Create a class that runs the seeders from the seeder folder.