Datamodel
Design datamodels with asynchronous functions and flows.
Contributors
Installation
With npm do:
$ npm install datamodel
Tests
Use mocha to run the tests.
$ npm install mocha
$ mocha test
Examples
Example 1 : Create a datamodel with 3 files and 1 directory
var fs = require('fs')
, datamodel = require('datamodel');
datamodel()
.declare('dirname', function(input, fill) {
fill('/etc');
})
.append('file1', function(input, fill) {
fs.readFile(this.dirname + '/group', function (err, data) {
fill(err ? err : data.toString().substring(0, 7).concat('...'));
});
})
.append('file2', function(input, fill) {
fs.readFile(this.dirname + '/hosts', function (err, data) {
fill(err ? err : data.toString().substring(0, 7).concat('...'));
});
})
.append('file3', function(input, fill) {
fs.readFile(this.dirname + '/unknown', function (err, data) {
fill(err ? err : data.toString().substring(0, 7).concat('...'));
});
})
.apply(function(err, res) {
console.error('Error>', err);
console.log('Result>', res);
});
output
Error> { [Error: Some fields are errors.] fields: [ 'file3' ] }
Result> { dirname: '/etc',
file1: 'root:x:...',
file2: '127.0.0...',
file3: { [Error: ENOENT, open '/etc/unknown'] errno: 34, code: 'ENOENT', path: '/etc/unknown' } }
Example 2 : Declare a datamodel as package/module
obj.js
var datamodel = require('datamodel');
datamodel()
.declare('a', function(input, fill) {
fill(input + 'A');
})
.append('b.1', function(input, fill) {
fill(input + 'B1');
})
.append('b.2', function(input, fill) {
fill(input + 'B1');
})
.append('b.3', function(input, fill) {
fill(input + 'B3');
})
.complete('c', function(input, fill) {
fill(input + 'C');
})
.attach(module);
index.js
var util = require('util');
require('./obj.js')('#', function(o) {
console.log(util.inspect(o, { colors:true, depth: null }));
}
);
Output
{ a: '#A',
'b.1': '#B1',
'b.2': '#B1',
'b.3': '#B3',
c: '#C' }
Example 3 : Combine a datamodel and a route with Express
var app = require('express');
app.route('/resource/:param1/:param2').all(require('./obj.js'));
API Documentation
datamodel()
declare(String key, Function callback)
append(String key, Function callback)
complete(String key, Function callback)
transform(Function callback)
send(Function callback)
apply(Mixed input, Mixed output, Function callback)
attach(Object module)
License
MIT/X11