UNPKG

fatty-cli-js

Version:

Fatty client for node.js

163 lines (142 loc) 3.43 kB
var zerorpc = require('zerorpc-fork'); // 数据访问层客户端 var dbClient = module.exports = function(opts) { this.opts = opts || {}; this.opts.host = this.opts.host || '127.0.0.1'; this.opts.port = this.opts.port || 6501; this.opts.timeout = this.opts.timeout || 5; this.conn = new zerorpc.Client({timeout: this.opts.timeout}); this.conn.connect('tcp://' + this.opts.host + ':' + this.opts.port); } // 指定模型 dbClient.prototype.model = function(name) { return new dbClientChain(name, this.conn); } // 数据访问层链 var dbClientChain = function(model, conn) { this.model = model; this.conn = conn; } // 创建数据 dbClientChain.prototype.create = function(data, callback) { callback = callback || function() {}; this.conn.invoke('query', { model: this.model, cmd: 'create', data: data }, function(err, res) { callback(err, err ? null : res); }); } // 更新数据 dbClientChain.prototype.update = function(id, data, callback) { callback = callback || function() {}; this.conn.invoke('query', { model: this.model, cmd: 'update', id: id, data: data }, function(err, res) { callback(err, err ? null : res); }); } // 删除数据 dbClientChain.prototype.delete = function(id, callback) { callback = callback || function() {}; this.conn.invoke('query', { model: this.model, cmd: 'delete', id: id }, function(err, res) { callback(err, err ? null : res); }); } // 任意sequelize查询 dbClientChain.prototype.sequelize = function(method, args, callback) { callback = callback || function() {}; this.conn.invoke('query', { model: this.model, cmd: 'sequelize', method: method, args: args }, function(err, res) { callback(err, err ? null : res); }); } // 获取单条数据 dbClientChain.prototype.get = function(id, opts, callback) { if (typeof opts === 'function') { callback = opts; opts = {}; } opts = opts || {}; callback = callback || function() {}; this.conn.invoke('query', { model: this.model, cmd: 'get', id: id, noCache: opts.noCache }, function(err, res) { callback(err, err ? null : res); }); } // 原生查询 dbClientChain.prototype.sql = function(sql, opts, callback) { if (typeof opts === 'function') { callback = opts; opts = {}; } opts = opts || {}; callback = callback || function() {}; this.conn.invoke('query', { model: this.model, cmd: 'sql', sql: sql, expires: opts.expires, noCache: opts.noCache }, function(err, res) { callback(err, err ? null : res); }); } // JSON查询 dbClientChain.prototype.json = function(method, json, opts, callback) { if (typeof opts === 'function') { callback = opts; opts = {}; } opts = opts || {}; callback = callback || function() {}; this.conn.invoke('query', { model: this.model, cmd: 'json', method: method, json: json, expires: opts.expires, noCache: opts.noCache }, function(err, res) { callback(err, err ? null : res); }); } // 规则查询 dbClientChain.prototype.find = function(cmd, args, opts, callback) { if (typeof args === 'function') { callback = args; args = []; opts = {}; } if (typeof opts === 'function') { callback = opts; opts = {}; } args = args || []; opts = opts || {}; callback = callback || function() {}; this.conn.invoke('query', { model: this.model, cmd: cmd, args: args, noCache: opts.noCache }, function(err, res) { callback(err, err ? null : res); }); }