"use strict" var r2mConsts = require('./Header.js'); var R2m_Socket = require('./Socket.js'); var R2m_Helper = require('./Helper.js'); var AppErrors = require('../AppErrors.js'); var events = require('events'); class R2m_Client { constructor(tableName, dbKey, cacheKey) { dbKey = dbKey || 'default'; cacheKey = cacheKey || 'default'; this._pwd = global.r2mConf.pwd; this._conf = [tableName, dbKey, cacheKey]; // TODO 这里并发会有问题 if (!R2m_Client._objHelper) { let objSocket = new R2m_Socket(global.r2mConf.host, global.r2mConf.port); R2m_Client._objHelper = new R2m_Helper(objSocket, R2m_Client); } } auth(pwd) { R2m_Client._objHelper.request(null, r2mConsts.CMD_AUTH, pwd, 0, r2mConsts.CONTENT_TYPE_TEXT); } // addObject(...args) { // return this._request(r2mConsts.CMD_ADD_OBJECT, args); // } addObject(args, updateList) { return this._request(r2mConsts.CMD_ADD_OBJECT, arguments); } addObjectNx(args, where, updateList) { return this._request(r2mConsts.CMD_ADD_OBJECT_NX, arguments); } addObjects(cols, args, updateList) { return this._request(r2mConsts.CMD_ADD_OBJECTS, arguments); } addObjects2(args, updateList) { return this._request(r2mConsts.CMD_ADD_OBJECTS2, arguments); } getInsertId() { return this._request(r2mConsts.CMD_GET_INSERT_ID, arguments); } getRow(where) { return this._request(r2mConsts.CMD_GET_ROW, arguments); } getAll(where, keyWord, updateList) { return this._request(r2mConsts.CMD_GET_ALL, arguments); } getAllSql(sql) { return this._request(r2mConsts.CMD_GET_ALL_SQL, arguments); } delRowCaches(where) { return this._request(r2mConsts.CMD_DEL_ROW_CACHE, arguments); } delListCache(where) { return this._request(r2mConsts.CMD_DEL_LIST_CACHE, arguments); } replaceObject(args, updateList) { return this._request(r2mConsts.CMD_REPLACE_OBJECT, arguments); } updateObject(args, where, updateList) { return this._request(r2mConsts.CMD_UPDATE_OBJECT, arguments); } updateObjects(args, where, updateList) { return this._request(r2mConsts.CMD_UPDATE_OBJECTS, arguments); } delObject(where, updateList) { return this._request(r2mConsts.CMD_DEL_OBJECT, arguments); } delObjects(where, updateList) { return this._request(r2mConsts.CMD_DEL_OBJECTS, arguments); } setDebug(debug) { return this._request(r2mConsts.CMD_SET_DEBUG, arguments); } getDebugMsg() { return this._request(r2mConsts.CMD_DEBUG_MSG, arguments); } _request(cmd, data) { if (!this.is_auth && this._pwd) { this.auth(this._pwd); this.is_auth = true; } let id = Math.floor(Math.random() * 1000000) + 1; R2m_Client._objHelper.request(this._conf, cmd, data, id); R2m_Client._objHelper._events[id] = new events.EventEmitter; let promise = new Promise((resolve, reject) => { R2m_Client._objHelper._events[id].once('receive', (result) => { if (result.cmd == r2mConsts.RESULT_SUCCESS) { resolve(result.content); } else if (result.cmd == r2mConsts.RESULT_ERROR) { let error = JSON.stringify(result); reject(error); throw new AppErrors.R2MError(error); } else { let error = JSON.stringify(result); reject(error); throw new AppErrors.R2MError(error); } }); }); return promise; } } module.exports = R2m_Client;