262 lines
9.1 KiB
JavaScript
262 lines
9.1 KiB
JavaScript
var debug = require('debug')('ff:tcrud:build:server:api:tentity');
|
|
var fs = require('fs');
|
|
var ejs = require('ejs');
|
|
var ncrud = require('node-crud');
|
|
var configTCrud = require('./tcrud-config');
|
|
var configRegistry = require('./config-registry');
|
|
var buildServerApiUrl = require('./build-server-api-url');
|
|
var buildServerExpress = require('./build-server-express');
|
|
var _ = require('underscore');
|
|
|
|
var mod = (function () {
|
|
|
|
var renderTemplateDataRead = function(tview, exportType, contentType) {
|
|
return function(data, query, cb) {
|
|
var res = this.response;
|
|
res.set('Content-Type', contentType);
|
|
|
|
//var templateReadHeader = fs.readFileSync('www_views/node-ff-tcrud/'+exportType+'/read-header.ejs', 'utf8');
|
|
var templateReadRecord = fs.readFileSync('./../lib/www_views/node-ff-tcrud/'+exportType+'/read-record.ejs', 'utf8');
|
|
//var templateReadFooter = fs.readFileSync('www_views/node-ff-tcrud/'+exportType+'/read-footer.ejs', 'utf8');
|
|
|
|
//var result = ejs.render(templateHeader,{
|
|
// tview: tview,
|
|
//});
|
|
for (var i = 0; i < data.length; i++) {
|
|
var row = data[i];
|
|
result += ejs.render(templateRecord,{
|
|
tview: tview,
|
|
record: row,
|
|
});
|
|
}
|
|
//result +=ejs.render(templateFooter,{
|
|
// tview: tview,
|
|
//});
|
|
res.write(result);
|
|
res.end();
|
|
this.close(); // end chain (cb set headers)
|
|
}
|
|
};
|
|
|
|
var renderTemplateDataList = function(tview, exportType, contentType) {
|
|
return function(data, query, cb) {
|
|
var res = this.response;
|
|
//res.set('Content-Type', contentType);
|
|
|
|
var templateHeader = fs.readFileSync('./../lib/www_views/node-ff-tcrud/'+exportType+'/list-header.ejs', 'utf8');
|
|
var templateRecord = fs.readFileSync('./../lib/www_views/node-ff-tcrud/'+exportType+'/list-record.ejs', 'utf8');
|
|
var templateFooter = fs.readFileSync('./../lib/www_views/node-ff-tcrud/'+exportType+'/list-footer.ejs', 'utf8');
|
|
|
|
var result = ejs.render(templateHeader,{
|
|
tview: tview,
|
|
});
|
|
for (var i = 0; i < data.length; i++) {
|
|
var row = data[i];
|
|
result += ejs.render(templateRecord,{
|
|
tview: tview,
|
|
record: row,
|
|
});
|
|
}
|
|
result +=ejs.render(templateFooter,{
|
|
tview: tview,
|
|
});
|
|
res.write(result);
|
|
res.end();
|
|
this.close(); // end chain (cb set headers)
|
|
};
|
|
};
|
|
|
|
var filterQueryList = function (model) {
|
|
return mcrud.parseQuery()
|
|
.defaults({ limit: '10' })
|
|
.overrides({})
|
|
.maxes({ limit: 1000 });
|
|
//.removes()
|
|
};
|
|
|
|
var filterDataUpdate = function (model) {
|
|
return mcrud.parseData()
|
|
overrides({});
|
|
//.removes('auth')
|
|
//.overrides({ updated: Date.now })
|
|
//.defaults({ 'info.gender': 'M' }))
|
|
};
|
|
|
|
function allowCrudType(tview,tcrudType) {
|
|
return tview[tcrudType].tenable; // todo add roles
|
|
};
|
|
|
|
var createMethodObject = function (entityObject,httpMethod) {
|
|
if ('GET' === httpMethod) {
|
|
return entityObject.Read();
|
|
} else if ('POST' === httpMethod) {
|
|
return entityObject.Create();
|
|
} else if ('PUT' === httpMethod) {
|
|
return entityObject.Update();
|
|
} else if ('DELETE' === httpMethod) {
|
|
return entityObject.Delete();
|
|
} else {
|
|
throw 'unknown httpMethod: '+httpMethod;
|
|
}
|
|
};
|
|
|
|
var createMethodExport = function (ctx,crudType,backendPipe,keySlug,exportMethod,exportMethodPre,exportMethodUse) {
|
|
var plugin = ctx.tplugin;
|
|
if (plugin[exportMethod] && allowCrudType(ctx.tview,crudType)) {
|
|
var apiUrl = buildServerApiUrl.createSlugApiTEntity(plugin.tmeta.key,ctx.tview,crudType) + keySlug;
|
|
var entityObject = ncrud.entity(apiUrl);
|
|
for (var ii = 0; ii < ctx.tview[crudType].tmethods.length; ii++) {
|
|
var httpMethod = ctx.tview[crudType].tmethods[ii];
|
|
var methodObject = createMethodObject(entityObject,httpMethod);
|
|
debug(plugin.tmeta.key+'.'+httpMethod+' for: '+crudType);
|
|
if (plugin[exportMethodUse]) {
|
|
methodObject.use(plugin[exportMethodUse](ctx));
|
|
}
|
|
//methodObject.pipe(filterQueryList(model))
|
|
//methodObject.pipe(filterDateUpdate(model))
|
|
if (plugin[exportMethodPre]) {
|
|
methodObject.pipe(plugin[exportMethodPre](ctx));
|
|
}
|
|
methodObject.pipe(backendPipe);
|
|
methodObject.pipe(plugin[exportMethod](ctx));
|
|
|
|
methodObject.on('close', function(event) {
|
|
debug('event close: '+event)
|
|
});
|
|
|
|
ctx.tplugin['troutes'].push({
|
|
urlPath: ctx.tview.tmeta.tserver.tslugs.tbase + '/' + apiUrl,
|
|
httpMethod: httpMethod
|
|
});
|
|
}
|
|
}
|
|
};
|
|
|
|
var registrateMenu2 = function(menu,key) {
|
|
if (menu['items'] === undefined) {
|
|
menu['items'] = [];
|
|
}
|
|
configRegistry.getMasterConfig().rootTMenu[key] = menu; // TODO; move to reg
|
|
};
|
|
|
|
var buildCrud = function (server,tcrud) {
|
|
|
|
if (!tcrud.tenable) {
|
|
debug('skip tentity by tenable: %s',tcrud.tid);
|
|
return;
|
|
}
|
|
var tview = configTCrud.createTView(tcrud /*, fetchCrudRoles()*/);
|
|
|
|
if (tview.tmeta.tmenu.tenable && tview.tmeta.tmenu.tkey !== null && tview.tmeta.tmenu.titem === false) {
|
|
registrateMenu2({
|
|
name: tview.tmeta.tmenu.tname,
|
|
icon: tview.tmeta.tmenu.ticon
|
|
},tview.tmeta.tmenu.tkey);
|
|
}
|
|
|
|
if (!tview.tmeta.tmodel.tbackend) {
|
|
debug('skip tentity no backend: %s',tcrud.tid);
|
|
return;
|
|
}
|
|
if (!tview.tmeta.tmodel.tid) {
|
|
debug('skip tentity no model: %s',tcrud.tid);
|
|
return;
|
|
}
|
|
var backend = configTCrud.getBackend(tview.tmeta.tmodel.tbackend);
|
|
if (!backend) {
|
|
debug('skip tentity illegal backend: %s',tcrud.tid);
|
|
return;
|
|
}
|
|
var keySlug = '/'+tview.tmeta.tmodel.tkey; // TODO: remove me
|
|
for (var i = 0; i < configRegistry.getMasterConfig().plugins.length; i++) {
|
|
var plugin = configRegistry.getMasterConfig().plugins[i];
|
|
var pluginKey = plugin.tmeta.key;
|
|
if (!_.contains(tview.tmeta.tserver.tformats,pluginKey)) {
|
|
continue;
|
|
}
|
|
|
|
var ctx = {
|
|
tplugin: plugin,
|
|
tview: tview,
|
|
createSlugApiTEntityBasePlugin: function(key,postfix) {
|
|
return buildServerApiUrl.createSlugApiTEntityBase(key,ctx.tview,postfix);
|
|
},
|
|
createSlugApiTEntityBase: function(postfix) {
|
|
return buildServerApiUrl.createSlugApiTEntityBase(pluginKey,ctx.tview,postfix); //createSlugApiTEntity: buildServerUrl.createSlugApiTEntity,
|
|
},
|
|
|
|
renderTemplateDataList: function(contentType) {
|
|
return renderTemplateDataList(ctx.tview,pluginKey,contentType);
|
|
},
|
|
renderTemplateDataRead: function(contentType) {
|
|
return renderTemplateDataRead(ctx.tview,pluginKey,contentType);
|
|
},
|
|
|
|
registrateMenu: function(menu,key) {
|
|
if (menu['items'] === undefined) {
|
|
menu['items'] = [];
|
|
}
|
|
configRegistry.getMasterConfig().rootTMenu[key] = menu;
|
|
},
|
|
registrateMenuItem: function(menuItem,key) {
|
|
if (configRegistry.getMasterConfig().rootTMenu[key] === undefined) {
|
|
return; // sub tentities
|
|
}
|
|
configRegistry.getMasterConfig().rootTMenu[key].items.push(menuItem);
|
|
},
|
|
|
|
registrateClientJSResource: function(clientResource) {
|
|
configRegistry.registrateClientResource(clientResource,'js');
|
|
},
|
|
registrateClientCSSResource: function(clientResource) {
|
|
configRegistry.registrateClientResource(clientResource,'css'); // +data or rm others
|
|
},
|
|
renderFunctionJSON: buildServerExpress.renderFunctionJSON,
|
|
server: server
|
|
}
|
|
if (plugin.configApi) {
|
|
debug(pluginKey+'.configApi(tview='+tview.tid+',backend='+tview.tmeta.tmodel.tbackend+')');
|
|
plugin.configApi(ctx);
|
|
}
|
|
createMethodExport(ctx,'tlist', backend.findAll(tview, 'tlist'), '', 'configApiTListExport', 'configApiTListExportPre', 'configApiTListExportUse');
|
|
createMethodExport(ctx,'tcreate', backend.createNew(tview, 'tcreate'), '', 'configApiTCreateExport', 'configApiTCreateExportPre', 'configApiTCreateExportUse');
|
|
createMethodExport(ctx,'tread', backend.findOne(tview, 'tread'), keySlug, 'configApiTReadExport', 'configApiTReadExportPre', 'configApiTReadExportUse');
|
|
createMethodExport(ctx,'tedit', backend.updateOne(tview, 'tedit'), keySlug, 'configApiTEditExport', 'configApiTEditExportPre', 'configApiTEditExportUse');
|
|
createMethodExport(ctx,'tdelete', backend.deleteOne(tview, 'tdelete'), keySlug, 'configApiTDeleteExport', 'configApiTDeleteExportPre', 'configApiTDeleteExportUse');
|
|
createMethodExport(ctx,'tcount', backend.findAllCount(tview,'tcount'), '', 'configApiTCountExport', 'configApiTCountExportPre', 'configApiTCountExportUse');
|
|
|
|
if (allowCrudType(tview,'tverify')) {
|
|
debug('todo verift');
|
|
//ncrud.entity(buildView.createBaseApiUri(tview,'json','tverify') + '/:_id').Update()
|
|
// .pipe(mcrud.findOne(model));
|
|
}
|
|
}
|
|
};
|
|
|
|
function walkTEntityTree(server,tentity) {
|
|
if (tentity.tparent) { // TODO: move skip root node
|
|
buildCrud(server,tentity);
|
|
}
|
|
for (var i = 0; i < tentity.tchilds.length; i++) {
|
|
walkTEntityTree(server,tentity.tchilds[i]);
|
|
}
|
|
};
|
|
|
|
return function BuildServerApiTEntity() {
|
|
|
|
this.build = function(server) {
|
|
debug('buildServerApiTEntity');
|
|
configRegistry.assertPhaseServer();
|
|
var troot = configTCrud.getRootTEntity();
|
|
walkTEntityTree(server,troot);
|
|
ncrud.launch(server,{
|
|
base: troot.tmeta.tserver.tslugs.tbase,
|
|
cors: false,
|
|
timeoute: troot.tmeta.tserver.tapi.timeoute
|
|
});
|
|
};
|
|
};
|
|
})();
|
|
|
|
module.exports = new mod();
|