JSPM

  • Created
  • Published
  • Downloads 12
  • Score
    100M100P100Q70688F
  • License MIT

Manage a group of tables with a parent child relation in SQL that will be seen as a document, or entity, like a no SQL database

Package Exports

  • json-schema-entity

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (json-schema-entity) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

json-schema-entity NPM version Build Status Dependency Status Coverage percentage

Manage a group of tables with a parent child relation in SQL that will be seen as a document, or entity, like a no SQL dtaabase

Install

$ npm install --save json-schema-entity

Usage (require pg-cr-layer or mssql-cr-layer)

var jse = require('json-schema-entity');
var pgCrLayer = require('pg-cr-layer');
 
var config = {
  user: 'me',
  password: 'my password',
  host: 'localhost',
  port: 5432,
  pool: {
    max: 25,
    idleTimeout: 30000
  }
};
 
var layer = new PgCrLayer(config)
 
layer.connect()
  .then(function() {

    var invoice = jse('invoice', {
        properties: {
          id: {
            type: 'integer',
            autoIncrement: true,
            primaryKey: true
          },
          client: {
            type: 'string'
          }
        }
      }, {db: layer});
      invoice.hasMany('items', {
        properties: {
          id: {
            type: 'integer',
            autoIncrement: true,
            primaryKey: true
          },
          name: {
            type: 'string'
          },
          description: {
            type: 'string'
          },
          price: {
            type: 'number',
            maxLength: 10,
            decimals: 2
          },
          invoiceId: {
            type: 'integer',
            $ref: 'invoice'
          }
        }
      });
      var invoiceInstance;
      invoice.createTables() // Will create tables invoice and items
        .then(function() {
          return invoice.syncTables(); // Then the reference in items
        })
        .then(function() {
          invoiceInstance = invoice.createInstance({
            client: 'Jessica',
            items: [
              {
                name: 'diamond',
                description: 'a beautiful diamond',
                price: 9999.99
              }
            ]
          });
          return invoiceInstance.save();
        })
        .then(function() {
          console.log(JSON.stringify(invoiceInstance, null, ' '));
          /* will log
           {
            "id": 1,
            "client": "Jessica",
            "items": [
             {
              "id": 1,
              "name": "diamond",
              "description": "a beautiful diamond",
              "price": 9999.99,
              "invoiceId": 1
             }
            ]
           }
          */
        })
    });
  

License

MIT © Andre Gloria