ngoose
v1.1.1
Published
javascript object factory with defaults
Downloads
20
Readme
ngoose
javascript object factory with defaults.
ngoose does one thing well: it create new objects with default fields values based on a schema definition.
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