json-schema-entity
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
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 db = new PgCrLayer(config)
var invoiceClass = jse('invoice', {
properties: {
id: {
type: 'integer',
autoIncrement: true,
primaryKey: true
},
client: {
type: 'string'
}
}
});
invoiceClass.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;
var invoice = invoiceClass.new(db);
invoice.createTables()
.then(function() {
return invoice.syncTables();
})
.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, ' '));
License
MIT © Andre Gloria