ngoose

1.1.1 • Public • Published

ngoose

javascript object factory with defaults.

ngoose does one thing well: it create new objects with default fields values based on a schema definition.

Travis Build Status NPM module NPM downloads

Test Coverage Code Climate Issue Count

Installation

npm install --save ngoose

Usage

const model = require('ngoose');
 
const user = model({
  age: Number,
  name: String
});
 
const instance = user();
console.log(instance);

{ age: 0, name: "" }

You can supply data to the factory method:

const instance = user({
  name: 'Garibaldi'
});
console.log(instance);

{ age: 0, name: "Garibaldi" }

Supplied fields that are not defined in schema are not inserted in created instance:

const instance = user({
  name: 'Garibaldi',
  address: 'somewhere'
});
console.log(instance);

{ age: 0, name: "Garibaldi" }

You can specify default values in schema:

const user = model({
  address: [String, 'somewhere'],
  name: [String, 'Garibaldi']
});
 
const instance=user();
console.log(instance);

{ address: "somewhere", name: "Garibaldi" }

You can compose models with other models or with inlined objects:

  const user = model({
     name: [String, 'unknown'],
     cool: [Boolean, true]
  });
 
  const bill = model({
     customer: user,
     payment: {
        terms: String,
        days: [Number, 30]
     }
  });
 
  const instance = bill();
  console.log(instance);

{ customer: { name: 'unknown', cool: true }, payment: { terms: '', days: 30 } }

Examples

See tests for further usage examples.

License

The MIT License (MIT)

Copyright (c) 2015 parro-it

Readme

Keywords

none

Package Sidebar

Install

npm i ngoose

Weekly Downloads

24

Version

1.1.1

License

MIT

Last publish

Collaborators

  • parroit