Client.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. "use strict"
  2. var r2mConsts = require('./Header.js');
  3. var R2m_Socket = require('./Socket.js');
  4. var R2m_Helper = require('./Helper.js');
  5. var AppErrors = require('../AppErrors.js');
  6. var events = require('events');
  7. class R2m_Client {
  8. constructor(tableName, dbKey, cacheKey) {
  9. dbKey = dbKey || 'default';
  10. cacheKey = cacheKey || 'default';
  11. this._pwd = global.r2mConf.pwd;
  12. this._conf = [tableName, dbKey, cacheKey];
  13. // TODO 这里并发会有问题
  14. if (!R2m_Client._objHelper) {
  15. let objSocket = new R2m_Socket(global.r2mConf.host, global.r2mConf.port);
  16. R2m_Client._objHelper = new R2m_Helper(objSocket, R2m_Client);
  17. }
  18. }
  19. auth(pwd) {
  20. R2m_Client._objHelper.request(null, r2mConsts.CMD_AUTH, pwd, 0, r2mConsts.CONTENT_TYPE_TEXT);
  21. }
  22. // addObject(...args) {
  23. // return this._request(r2mConsts.CMD_ADD_OBJECT, args);
  24. // }
  25. addObject(args, updateList) {
  26. return this._request(r2mConsts.CMD_ADD_OBJECT, arguments);
  27. }
  28. addObjectNx(args, where, updateList) {
  29. return this._request(r2mConsts.CMD_ADD_OBJECT_NX, arguments);
  30. }
  31. addObjects(cols, args, updateList) {
  32. return this._request(r2mConsts.CMD_ADD_OBJECTS, arguments);
  33. }
  34. addObjects2(args, updateList) {
  35. return this._request(r2mConsts.CMD_ADD_OBJECTS2, arguments);
  36. }
  37. getInsertId() {
  38. return this._request(r2mConsts.CMD_GET_INSERT_ID, arguments);
  39. }
  40. getRow(where) {
  41. return this._request(r2mConsts.CMD_GET_ROW, arguments);
  42. }
  43. getAll(where, keyWord, updateList) {
  44. return this._request(r2mConsts.CMD_GET_ALL, arguments);
  45. }
  46. getAllSql(sql) {
  47. return this._request(r2mConsts.CMD_GET_ALL_SQL, arguments);
  48. }
  49. delRowCaches(where) {
  50. return this._request(r2mConsts.CMD_DEL_ROW_CACHE, arguments);
  51. }
  52. delListCache(where) {
  53. return this._request(r2mConsts.CMD_DEL_LIST_CACHE, arguments);
  54. }
  55. replaceObject(args, updateList) {
  56. return this._request(r2mConsts.CMD_REPLACE_OBJECT, arguments);
  57. }
  58. updateObject(args, where, updateList) {
  59. return this._request(r2mConsts.CMD_UPDATE_OBJECT, arguments);
  60. }
  61. updateObjects(args, where, updateList) {
  62. return this._request(r2mConsts.CMD_UPDATE_OBJECTS, arguments);
  63. }
  64. delObject(where, updateList) {
  65. return this._request(r2mConsts.CMD_DEL_OBJECT, arguments);
  66. }
  67. delObjects(where, updateList) {
  68. return this._request(r2mConsts.CMD_DEL_OBJECTS, arguments);
  69. }
  70. setDebug(debug) {
  71. return this._request(r2mConsts.CMD_SET_DEBUG, arguments);
  72. }
  73. getDebugMsg() {
  74. return this._request(r2mConsts.CMD_DEBUG_MSG, arguments);
  75. }
  76. _request(cmd, data) {
  77. if (!this.is_auth && this._pwd) {
  78. this.auth(this._pwd);
  79. this.is_auth = true;
  80. }
  81. let id = Math.floor(Math.random() * 1000000) + 1;
  82. R2m_Client._objHelper.request(this._conf, cmd, data, id);
  83. R2m_Client._objHelper._events[id] = new events.EventEmitter;
  84. let promise = new Promise((resolve, reject) => {
  85. R2m_Client._objHelper._events[id].once('receive', (result) => {
  86. if (result.cmd == r2mConsts.RESULT_SUCCESS) {
  87. resolve(result.content);
  88. } else if (result.cmd == r2mConsts.RESULT_ERROR) {
  89. let error = JSON.stringify(result);
  90. reject(error);
  91. throw new AppErrors.R2MError(error);
  92. } else {
  93. let error = JSON.stringify(result);
  94. reject(error);
  95. throw new AppErrors.R2MError(error);
  96. }
  97. });
  98. });
  99. return promise;
  100. }
  101. }
  102. module.exports = R2m_Client;