Socket
Socket
Sign inDemoInstall

@axmit/express-core-mongodb

Package Overview
Dependencies
252
Maintainers
3
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @axmit/express-core-mongodb

A package provide Mongodb module for @axmit/express-core using Mongoose ODM


Version published
Weekly downloads
2
Maintainers
3
Created
Weekly downloads
 

Readme

Source

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(); //prints all records in the collection  
const amazingInstance = await new AmazingModel({ name: 'test', description: 'test1' }).save();  
amazingInstance.printName(); //prints test  

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() {  
    //Fill your model here and return amount of created documents 
 }
}  

Note: you must create index file in seeders dir which will export all seeders

Keywords

FAQs

Last updated on 06 Apr 2019

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc