npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

json-mongoose

v0.3.5

Published

Mongoose model representation like waterline models

Downloads

204

Readme

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 :

| Option | Type | Required | Description | | ---- | ---- | ---- | ---- | | mongoose | Object | true | Mongoose instance | | schema | Object | true | Joi schema or JSON schema. If you use JOI schema, you should be used the schemaUpdate Method to set the unique fields or other params | | collection | string | true | Collection name | | connection | String or Object | false | Mongoose connection created with mongoose.createConnection() or juste the string for the new connection | | schemaUpdate | function | false | This function is used if your schema is a JOI schema. In this case, you can set specifics fields unique, ... | | virtual | object | false |define model virtual fields | | options | object | false | Mongoose schema options | | pre | object | false | Define different preprocess functions in the middleware for your model | | post | object | false | Define different preprocess functions in the middleware for your model | | methods | object | false | Define all instance functions for your model | | statics | object | false | Define all statics functions for your model | | index | object[]/Array[] | false | Define model indexes. Use an array to pass index options. | | transform | function | false | Define the default transform method for your model | | autoinc | Object | false | Autoincrement config for this Model | | autoinc.field | String | false | Field with the autoinc | | autoinc.startAt | Number | false | Start value for the auto inc. Default : 0 | | autoinc.incrementBy | Number | false | Auto Inc steps. Default : 1 |