Common information
This package provide Mongodb module for @axmit/express-core
using Mongoose ODM
Prerequisites
Before using this package you need to authorize yourself into npm registry using npm adduser
Installation
npm i @axmit/express-core-mongodb
or yarn add @axmit/express-core-mongodb
Usage
import { ExpressApplication } from '@axmit/express-core';
import { MongodbModule } from '@axmit/express-core-mongodb';
class YourApp extends ExpressApplication {
public async start() {
await this.addModule(new MongodbModule());
}
}
You must also specify MONGO_DB_URL
variable in your .env
file.
Gulp Mongoose Seed
This package also provide gulp CLI command to seed you DB, you can add it in your gulp file like that:
require('@axmit/express-core-mongodb/gulp');
Use gulp mongoose-seed
to load you MongoDB seeders from seeders
folder
See MongooseSeeder
section for more info
Included Modules
MongooseModelBuilder
Builder for mongoose models, which provides amount of methods to create your model and contain following methods:
constructor(modelName: string) : MongooseModelBuilder
Creates builder instance using provided model name
.useFields(fields): MongooseModelBuilder
Adds model schema by fields
param containing classic object defining mongoose schema
.addIndex(fields: any, options?: any): MongooseModelBuilder
Adds mongoose model index on specified fields
params, and also you can provide additional mongoose index options
.addSchemaOptions(schemaOptions): MongooseModelBuilder
Adds Mongoose schema options
.addMethod(name: string, method: (...args: any[]) => any): MongooseModelBuilder
Adds mongoose method to model instance
Params:
- name {String} - method name
- method {Function} - model instance method (where
this
is mongoose instance model)
.addStatic(name: string, method: (this: Model, ...arg: any[]) => any): MongooseModelBuilder
Adds mongoose method to model instance
Params:
- name {String} - method name
- method {Function} - model instance method (where
this
is mongoose instance model)
.buildSchema()
Builds only mongoose Schema
and returns it
.build()
Build schema and model and return mongoose model class
Usage:
import { MongooseModelBuilder } from '@axmit/express-core-mongodb';
const AmazingModel = new MongooseModelBuilder('ModelName')
.useFields({ name: { required: true, type: String },
description: { required: true, type: String }
})
.build();
await new AmazingModel({ name: 'test', description: 'test' }).save();
Note: if you want TypeScript to correct handle your custom methods and statics, you must specify interfaces and pass it into builder generic params as follows:
import { MongooseDocument, MongooseModel, MongooseModelBuilder } from '@axmit/express-core-mongodb';
interface IAmazingDocumentFields extends MongooseDocument {
name: string;
description: string;
}
interface IAmazingDocument extends IAmazingDocumentFields {
printName(): void;
}
interface IAmazingModel extends MongooseModel<IAmazingDocument> {
printAllRecords(): void;
}
const AmazingModel = new MongooseModelBuilder<IAmazingDocument, IAmazingModel>('ModelName')
.useFields({ name: { required: true, type: String },
description: { required: true, type: String }
})
.addMethod('printName', function() {
console.log(this.name);
})
.addStatic('printAllRecords', async function() {
const records = await this.find({});
console.log(records);
})
.build();
Now you can use your static and method as usual
await AmazingModel.printAllRecords();
const amazingInstance = await new AmazingModel({ name: 'test', description: 'test1' }).save();
amazingInstance.printName();
clearMongoDB
This helper cleans up all you collections
Usage:
import { clearMongoDB } from '@axmit/express-core-mongodb';
await clearMongoDB();
MongooseSeeder
This abstract class helps you to implement mongoose seeders, you need
to create seeders
folder in your application root and make it return set of
seeders classes like this
import { MongooseSeeder } from '@axmit/express-core-mongodb';
export class TestSeeder extends MongooseSeeder {
public async shouldRun() {
return true;
}
public async run() {
}
}
Note: you must create index
file in seeders
dir which will export all seeders