0.3.5 • Published 7 years ago

json-mongoose v0.3.5

Weekly downloads
77
License
Apache-2.0
Repository
github
Last release
7 years ago

json-mongoose

Mongoose model representation like waterline models

Installation

npm install json-mongoose

This package is compatible with : Node.js >= 4.x (node.JS 4, 5 and 6 for now)

Usage

This library was created to simplify and organize code in mongose model declaration and it add some new features :

  • Create model from Joi schema to use it for validation
  • Promisify mongoose functions

Declaration example

Schema :

'use strict';

const Joi = require('joi');

module.exports = Joi.object({
    firstName       : Joi.string().required(),
    lastName        : Joi.string().required(),
    age             : Joi.number().integer(),
    emailAddress    : Joi.string().email().required(),
    login           : Joi.string().required(),
    password        : Joi.string().required()
});

Model :

'use strict';

const jsonToMongoose    = require('json-mongoose');
const async             = require('async');
const bcrypt            = require('bcrypt');
const mongoose          = require('mongoose');

module.exports = jsonToMongoose({
    mongoose    : mongoose,
    collection  : 'user',
    schema      : require('../schemas/user'),
    autoinc     : {
        field : '_id'
    },
    pre         : {
        save : (doc, next) => {
            async.parallel({
                password : done => {
                    bcrypt.hash(doc.password, 10, (err, hash) => {
                        if (err) {
                            return next(err);
                        }
                        doc.password = hash;
                        done();
                    });
                }
            }, next);
        }
    },
    schemaUpdate : (schema) => {
        schema.login.unique         = true;
        schema.emailAddress.unique  = true;

        return schema;
    },
    transform : (doc, ret, options) => {
        delete ret.password;

        return ret;
    },
    options : {

    }
});

Parameters

The function get only one parameter with may options :

OptionTypeRequiredDescription
mongooseObjecttrueMongoose instance
schemaObjecttrueJoi schema or JSON schema. If you use JOI schema, you should be used the schemaUpdate Method to set the unique fields or other params
collectionstringtrueCollection name
connectionString or ObjectfalseMongoose connection created with mongoose.createConnection() or juste the string for the new connection
schemaUpdatefunctionfalseThis function is used if your schema is a JOI schema. In this case, you can set specifics fields unique, ...
virtualobjectfalsedefine model virtual fields
optionsobjectfalseMongoose schema options
preobjectfalseDefine different preprocess functions in the middleware for your model
postobjectfalseDefine different preprocess functions in the middleware for your model
methodsobjectfalseDefine all instance functions for your model
staticsobjectfalseDefine all statics functions for your model
indexobject[]/Array[]falseDefine model indexes. Use an array to pass index options.
transformfunctionfalseDefine the default transform method for your model
autoincObjectfalseAutoincrement config for this Model
autoinc.fieldStringfalseField with the autoinc
autoinc.startAtNumberfalseStart value for the auto inc. Default : 0
autoinc.incrementByNumberfalseAuto Inc steps. Default : 1
0.3.5

7 years ago

0.3.4

7 years ago

0.3.3

7 years ago

0.3.2

8 years ago

0.3.1

8 years ago

0.2.1

8 years ago

0.2.0

8 years ago

0.1.0

8 years ago