var config = require('./../tcrud-config'); var debug = require('debug')('ff:tcrud:mongoose'); var clone = require('clone'); var validate = require('validate.io'); // ------- MongoosePlugin Object function MongoosePlugin(key,conn) { this.key = key; this.conn = conn; } MongoosePlugin.prototype.configPlugin = function (ctx) { ctx.key='mongoose#'+this.key; ctx.description='Adds ldap backend support.'; }; MongoosePlugin.prototype.createBackend = function() { return new MongooseBackend(this); } //------- MongooseBackend Object function MongooseBackend(plugin) { this.plugin = plugin; } MongooseBackend.prototype.getKey = function() { return this.plugin.key; } MongooseBackend.prototype.findAll = function(tview,crudType) { var self = this; return function(data, dataParam, cb) { debug('find all..'+tview.tmeta.tmodel.tid); var model = self.plugin.conn.model(tview.tmeta.tmodel.tid); model.find({},function (err, result) { if (err) { debug(err); cb(err); } else { cb(err,result.rows); } }); // cb(); // var querySql = 'SELECT '+selectField(tview,crudType)+' FROM '+tview.tmeta.tmodel.tid+''; // debug('findAll %s',querySql); // var query = self.db.query(querySql,[], }; } MongooseBackend.prototype.createNew = function(tview,crudType) { var self = this; return function(data, dataParam, cb) { }; } MongooseBackend.prototype.findOne = function(tview,crudType) { var self = this; return function(data, dataParam, cb) { debug('findOne param %s',JSON.stringify(dataParam)); //debug('findOne sql %s',querySql); var model = self.plugin.conn.model(tview.tmeta.tmodel.tid); model.findById(dataParam,function (err, result) { if (err) { debug(err); cb(err); } else { cb(err,result.rows); } }); // var querySql = 'SELECT '+selectField(tview,crudType)+' FROM '+tview.tmeta.tmodel.tid+' WHERE '+whereKeys(tview)+''; // }; } MongooseBackend.prototype.updateOne = function(tview,crudType) { var self = this; return function(data, dataParam, cb) { debug('updateOne param %s',JSON.stringify(dataParam)); debug('updateOne data %s',JSON.stringify(data)); var model = self.plugin.conn.model(tview.tmeta.tmodel.tid); model.save(data,function (err, result) { if (err) { debug(err); cb(err); } else { cb(err,result.rows); } }); // var querySql = 'UPDATE '+tview.tmeta.tmodel.tid+' '+updateValues(tview,data)+' WHERE '+whereKeys(tview); // // debug('updateOne sql %s',querySql); // var query = self.db.query(querySql, data, function (err, result) { // if (err) { // debug(err); // cb(err); // } else { // cb(err,result.rows[0]); // } // }); }; } MongooseBackend.prototype.deleteOne = function(tview,crudType) { var self = this; return function(data, dataParam, cb) { // var querySql = 'DELETE FROM '+tview.tmeta.tmodel.tid+' WHERE '+whereKeys(tview); // debug('deleteOne param %s',JSON.stringify(dataParam)); // debug('deleteOne sql %s',querySql); // var query = self.db.query(querySql, dataParam, function (err, result) { // if (err) { // debug(err); // cb(err); // } else { // cb(err,result.rows[0]); // } // }); }; } MongooseBackend.prototype.findAllCount = function(tview,crudType) { var self = this; return function(data, dataParam, cb) { // var querySql = 'SELECT count(*) FROM '+tview.tmeta.tmodel.tid+' WHERE '+whereKeys(tview); // debug('findAllCount param %s',JSON.stringify(dataParam)); // debug('findAllCount sql %s',querySql); // var query = self.db.query(querySql, dataParam, function (err, result) { // if (err) { // debug(err); // cb(err); // } else { // cb(err,result.rows[0]); // } // }); }; } function autoFieldType(fieldMeta,fieldType) { if (!fieldMeta) { throw new Error('no fieldMeta'); } if (fieldType && fieldType.length != 0) { return fieldType; } if (fieldMeta.type == Date) { return 'date'; } return 'text'; } buildFields = function(modelMeta) { if (!modelMeta) { throw new Error('no modelMeta'); } var tfields = {}; var keys = Object.keys(modelMeta); for (i = 0; i < keys.length; i++) { var key = keys[i]; var value = modelMeta[key]; var tfield = null; if (key && value && value.tfield) { debug('tfield model cloned'); tfield = clone(value.tfield); tfield.tid = key; tfield.tname = tfield.tname; tfield.type = autoFieldType(value,tfield.ttype); } else if (key && value) { debug('tfield model auto created'); tfield = config.createTField(key); tfield.type = autoFieldType(value); } if (tfield.tvalidate && tfield.tvalidate.io) { debug('tfield validate rule: '+tfield.tvalidate.io); } tfields[tfield.tid] = tfield; } return tfields; } function ss(valueRule) { return function (value, response) { response(validate(valueRule,value)); }; } createModelValidators = function (modelSchema,modelFields) { if (!modelSchema) { throw new Error('no modelSchema'); } if (!modelFields) { throw new Error('no modelFields'); } var keys = Object.keys(modelFields); for (var i = 0; i < keys.length; i++) { var key = keys[i]; var tfield = modelFields[key]; if (!tfield.tvalidate) { continue; } if (!tfield.tvalidate.io) { continue; } modelSchema.path(tfield.tid).validate(ss(tfield.tvalidate.io), '{PATH} validation failed: '+tfield.tvalidate.io); } } buildStatics = function(modelFields,modelStatics) { if (!modelFields) { throw new Error('no modelFields'); } if (!modelStatics) { modelStatics = {}; } modelStatics['ff_tcrud_fields'] = modelFields; return modelStatics; } buildTEntityModel = function(mongoose,tcrudParent,name) { var model = mongoose.model(name); var tcrud = config.createTEntity(tcrudParent,name, '_id'); var tfields = model['ff_tcrud_fields']; if (tfields) { tcrud.tmeta.tfields = tfields; } tcrud.tmodel = name; return tcrud; } buildTEntityModels = function(conn,tcrudParent) { var result = []; var modelNames = conn.modelNames(); for (var i = 0; i < modelNames.length; i++) { result.push(buildTEntityModel(conn,tcrudParent,modelNames[i])) } return result; } // ----- wrappers buildStaticsModel = function(modelMeta,modelStatics) { return buildStatics(buildFields(modelMeta),modelStatics); } buildStaticsModelValidated = function(modelMeta,modelSchema,modelStatics) { var modelFields = buildFields(modelMeta); createModelValidators(modelSchema,modelFields); return buildStatics(modelFields,modelStatics); } module.exports = { buildTEntityModels: buildTEntityModels, buildStaticsModelValidated: buildStaticsModelValidated, buildFields: buildFields, registrate: function(key,conn) { config.registratePlugin(new MongoosePlugin(key,conn)); } }