Neolithic Populous Metropolis
    Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org »

    ngoosepublic

    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

    Keywords

    none

    install

    npm i ngoose

    Downloadsweekly downloads

    7

    version

    1.1.1

    license

    MIT

    repository

    githubgithub

    last publish

    collaborators

    • avatar