portal-models

2.9.2 • Public • Published

portal-models

This project has for its first goal to uniformize MongoDB data across endpoints that require it with Mongoose. Mongoose has for requirements to get schema of resources it will call in order to work. Duplicating schemas across projects will be hard to maintain since schemas like user need some libraries to work (bcrypt, etc...).

Installation

npm install --save portal-models

Usage

In order to use it, you just need to import the model you need:

const Report = require('portal-models').Report;

Report.create(...);

Bluebird promises

The main advantage of using this library instead of an embedded existing schema is bluebird promises. It allows us some pretty manipulations instead of mongoose callbacks while using it:

User
  .findOne({'statProviderId': ...})
  .then(user => // do stuff with user)
  .catch(error => // handle error);

Since it return promise, you can also yield for result from an ES6 generator:

function* createUser() {
  try {
    let user = yield User.create({ firstName: 'John', lastName: 'Doe' });

    console.log(user.firstName, user.lastName); // Will print you 'John Doe', synchronous writing for asynchronous operations thanks to the yield.

  } catch(e) {
    console.log(e.message); // Will tell you why the operation has failed.
  }
}

Readme

Keywords

Package Sidebar

Install

npm i portal-models

Weekly Downloads

3

Version

2.9.2

License

ISC

Last publish

Collaborators

  • plop