2
0
Fork 0
tcrud/lib/backend/mongo.js

128 lines
3.2 KiB
JavaScript
Raw Permalink Normal View History

2017-06-10 14:53:29 +00:00
var config = require('./../tcrud-config');
var debug = require('debug')('ff:tcrud:mongo');
module.exports = {
registrate: function(key,server) {
config.registratePlugin(new MongoPlugin(key,server));
}
}
// ------- MongoPlugin Object
function MongoPlugin(key,server) {
this.key = key;
this.server = server;
}
MongoPlugin.prototype.configPlugin = function (ctx) {
ctx.key='mongo#'+this.key;
ctx.description='Adds mongo backend support.';
};
MongoPlugin.prototype.createBackend = function() {
return new MongoBackend(this);
}
// -------- MongoBackend
function MongoBackend(plugin) {
this.plugin = plugin;
}
MongoBackend.prototype.getKey = function() {
return this.plugin.key;
}
MongoBackend.prototype.findAll = function(tview,crudType) {
var self = this;
return function(data, dataParam, cb) {
var querySql = 'SELECT '+selectField(tview,crudType)+' FROM '+tview.tmeta.tmodel.tid+'';
debug('findAll %s',querySql);
var query = self.db.query(querySql,[],function (err, result) {
if (err) {
debug(err);
cb(err);
} else {
cb(err,result.rows);
}
});
};
}
MongoBackend.prototype.createNew = function(tview,crudType) {
var self = this;
return function(data, dataParam, cb) {
};
}
MongoBackend.prototype.findOne = function(tview,crudType) {
var self = this;
return function(data, dataParam, cb) {
var querySql = 'SELECT '+selectField(tview,crudType)+' FROM '+tview.tmeta.tmodel.tid+' WHERE '+whereKeys(tview)+'';
debug('findOne param %s',JSON.stringify(dataParam));
debug('findOne 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]);
}
});
};
}
MongoBackend.prototype.updateOne = function(tview,crudType) {
var self = this;
return function(data, dataParam, cb) {
var querySql = 'UPDATE '+tview.tmeta.tmodel.tid+' '+updateValues(tview,data)+' WHERE '+whereKeys(tview);
debug('updateOne param %s',JSON.stringify(dataParam));
debug('updateOne data %s',JSON.stringify(data));
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]);
}
});
};
}
MongoBackend.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]);
}
});
};
}
MongoBackend.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]);
}
});
};
}