Skip to content

Allows you to easily load all your schemas and/or models into mongoose.

License

Notifications You must be signed in to change notification settings

WterBerg/mongooseloader

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@wrpterberg/mongooseloader

Build Status

Allows you to easily load all your schemas and/or models into mongoose.

Example usage

Load all schemas and models

const mongoose = require('mongoose'),
      loader = require('@wrpterberg/mongooseloader');

async function prepareMongoose() {
    loader.enableLogging();
    
    await loader.loadSchemas('./schemas');
    await loader.loadModels('./models');
}

prepareMongoose()
    .then(() => {
        let myModel = mongoose.model('MyModel');
    })
    .catch((err) => {
        console.log('Aaah!! My database is on fire!');
        console.log(err);
    });

All schemas and models found in their source directories are now part of your mongoose object.

Load specific schemas and models

const mongoose = require('mongoose'),
      loader = require('@wrpterberg/mongooseloader');

async function prepareSomeSchemasAndModels() {
    loader.enableLogging();
    loader.setSchemaSource('./schemas');
    loader.setModelSource('./models');
    
    await loader.loadSchema('MyModel.js');
    await loader.loadSchema('MyOtherModel.js');
    
    await loader.loadModel('MyModel.js');
    await loader.loadModel('MyOtherModel.js');
}

prepareSomeSchemasAndModels()
    .then(() => {
        let myModel = mongoose.model('MyModel');
    })
    .catch((err) => {
        console.log('Aaah!! My database is on fire!');
        console.log(err);
    });

The specific schemas and models are now part of your mongoose object.

Logging

use loader.enableLogging() to allow MongooseLoader to log its actions and errors into your console.log. This can also be disabled again by calling loader.disableLogging(). Logging is disabled by default.

Schema

The name of a schema is resolved to its filename without the file's extension.

A valid schema must follow the rules and template stated below:

Rules:

  • file extension must be '.js'
  • A schema can't already be defined with the same name
  • A schema must export the functions getRequiredSchemas() and getSchema()
  • If a schema uses another schema as part of its schema, it must be defined in getRequiredSchemas()

Template

let schema = {};
schema.getRequiredSchemas = () => {
    return ['Item'];
};
schema.getSchema = (mongoose, schemas) => {
    let mySchema = mongoose.Schema({
        items: [schemas['Item']]
    });
    
    mySchema.methods.myMethod = (myArgs) => {
        // my method
    };
    
    mySchema.statics.myStaticsMethod = (myArgs) => {
        // myStaticsMethod
    };
    
    return mySchema;
};

module.exports = schema;

Model

The name of a model is resolved to its filename without the file's extension.

A valid model must follow the rules and template stated below:

Rules:

  • file extension must be '.js'
  • The modelfile must export a function
  • A model can't already be defined with the same name
  • There must already be defined a schema with a matching name of the model

Template

module.exports = (mongoose, schema) => {
    let model = mongoose.model('myModel', schema);

    model.on('myEvent', (myArgs) => {
        // my event handler
    });

    return model;
};

Installation

npm install @wrpterberg/mongooseloader --save

Test

npm test

About

Allows you to easily load all your schemas and/or models into mongoose.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published