node package manager

ngoose

javascript object factory with defaults

ngoose

javascript object factory with defaults.

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

npm install --save ngoose
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 } }

See tests for further usage examples.

The MIT License (MIT)

Copyright (c) 2015 parro-it