README
Installation
Install the dependencies and devDependencies and start the server.
$ npm install winrow
// init server
const winrow = require('winrow');
winrow.init(sandbox);
// start server
winrow.start();
// connect mongoose;
winrow.connect_mongoose();
// stop server
winrow.stop();
// disconnect mongoose
winrow.disconnect_mongoose();
// mapping api
winrow.mapping();
// mappings
const mappings = [
// create
{
path : '/example',
method : 'POST',
methodName : 'exampleCreate',
serviceName : ExampleServices,
input : {
transform : function(req, opts) {
const { validator } = opts;
// schema
const schema = {
type: 'object',
properties: {
name: { type: 'string' },
},
required: ['name']
}
// validate request
const {valid, errors} = validator(schema, req.body);
if(!valid) {
return Promise.reject(errors);
}
return {
name : req.body.name
}
}
},
output: {
transform: function (response) {
return {
body: response
}
}
}
},
// get
{
pathName: '/example',
method: 'GET',
methodName: 'exampleGet',
serviceName: ExampleServices,
input: {
transform: function (req) {
return {
params: req.query
}
}
},
output: {
transform: function (response) {
return {
headers: {
'X-Total-Count': response.total,
'Access-Control-Expose-Headers': 'X-Total-Count'
},
body: response.data
}
}
}
},
// get by id
{
pathName: '/example/:id',
method: 'GET',
methodName: 'exampleGetById',
serviceName: ExampleServices,
input: {
transform: function (req) {
return {
id: req.params.id
}
}
},
output: {
transform: function (response) {
return {
body: response
}
}
}
},
// update
{
pathName: '/example/:id',
method: 'PUT',
methodName: 'exampleUpdate',
serviceName: ExampleServices,
input: {
transform: function (req) {
return {
id: req.params.id,
...req.body
}
}
},
output: {
transform: function (response) {
return {
body: response
}
}
}
},
];
// end mappings
const sandbox = {
application: {
pathServer: '/rest/api',
enable: false,
data_ssl: {
port: 443,
host: 'exampledomain.com'
},
bridge: {
connect: {
database_local: {
host: 'localhost',
port: '27017',
name: 'name_mongoose_local',
},
database_server: {
host: 'localhost',
port: '27017',
name: 'name_mongoose_server',
}
},
rest_api: {
mappings: mappings
}
},
},
}
const winrow = require('winrow');
// get protoTypes in server.
const Promise = winrow.require('bluebird');
const lodash = winrow.require('lodash');
const moment = winrow.require('moment');
// service
function ExampleServices(params = {}) {
const { dataStore, returnCodes } = params;
// create services
this.exampleCreate = function(args, opts) {
const { loggingFactory, requestId } = opts;
loggingFactory.debug('create begin', {requestId : `${requestId}`})
return dataStore.create({
type : 'name of model'
data : {} || args
})
.then(result => '...do something then return result')
.catch(err => {
loggingFactory.error('create begin', {requestId : `${requestId}`})
return Promise.reject(err);
})
}
// get services
this.exampleGet = function(args, opts) {
const { loggingFactory } = opts;
return dataStore.find({
type : 'name of model',
filter : {} || 'query with field model',
projection : {} || 'get field in model',
options : {} || 'pagination mongoose',
populates : [
{
path : 'field of reference in model',
select : 'get field in model of reference'
}
]
})
.then(result => '... do something')
}
// get by id servers
this.exampleGetById = function(args, opts) {
const { loggingFactory } = opts;
return dataStore.get({
type : 'name of model',
id : args.id,
populates : [
{
path : 'field of reference in model',
select : 'get field in model of reference'
}
]
})
.then(result => '...do something')
}
// update server
this.exampleUpdate = function(args, opts) {
const { loggingFactory } = opts;
return dataStore.update({
type : 'name of model',
id : args.id,
data : {} || args
})
.then(result => '...do something')
}
}
// end service
// Example Connect API
[github](https:github.com/NguyenPhuocMinh/example-node-api)