https://github.com/parro-it/ngoose
javascript object factory with defaults
https://github.com/parro-it/ngoose
Last synced: 10 months ago
JSON representation
javascript object factory with defaults
- Host: GitHub
- URL: https://github.com/parro-it/ngoose
- Owner: parro-it
- License: mit
- Created: 2015-12-27T22:31:23.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2016-01-13T22:43:16.000Z (about 10 years ago)
- Last Synced: 2025-03-26T20:01:40.792Z (10 months ago)
- Language: JavaScript
- Size: 27.3 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- Changelog: changelog.md
- License: license
Awesome Lists containing this project
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.
[](http://travis-ci.org/parro-it/ngoose)
[](https://npmjs.org/package/ngoose)
[](https://npmjs.org/package/ngoose)
[](https://codeclimate.com/github/parro-it/ngoose/coverage)
[](https://codeclimate.com/github/parro-it/ngoose)
[](https://codeclimate.com/github/parro-it/ngoose)
## Installation
```bash
npm install --save ngoose
```
## Usage
```js
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:
```js
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:
```js
const instance = user({
name: 'Garibaldi',
address: 'somewhere'
});
console.log(instance);
```
> {
> age: 0,
> name: "Garibaldi"
> }
You can specify default values in schema:
```js
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:
```js
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](test.js) for further usage examples.
## License
The MIT License (MIT)
Copyright (c) 2015 parro-it