You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

237 lines
3.5 KiB

  1. const serverConfig = require('../config/serverConfig');
  2. const tableNames = require('./tableNames');
  3. const r = require('rethinkdbdash')({
  4. host: serverConfig.dbHost,
  5. port: serverConfig.dbPort,
  6. db: serverConfig.dbName,
  7. user: serverConfig.dbUser,
  8. password: serverConfig.dpPass
  9. });
  10. module.exports = {
  11. staticCon: null,
  12. init: async function (cbReady) {
  13. await this.create();
  14. cbReady();
  15. },
  16. create: async function () {
  17. try {
  18. await r.dbCreate(serverConfig.dbName).run();
  19. } catch (e) {
  20. }
  21. for (const table of tableNames) {
  22. try {
  23. await r.tableCreate(table).run();
  24. } catch (e) {
  25. if (!e.message.includes('already exists'))
  26. _.log(e);
  27. }
  28. }
  29. },
  30. createTable: async function (tableName) {
  31. try {
  32. await r.tableCreate(tableName).run();
  33. } catch (e) {
  34. if (!e.message.includes('already exists'))
  35. _.log(e);
  36. }
  37. },
  38. getAsyncIgnoreCase: async function (table, key) {
  39. const res = await r.table(table)
  40. .filter(doc => doc('id').match(`(?i)^${key}$`))
  41. .run();
  42. return res[0];
  43. },
  44. getAsync: async function ({
  45. table,
  46. key,
  47. isArray,
  48. noDefault,
  49. ignoreCase
  50. }) {
  51. let res = null;
  52. if (ignoreCase)
  53. res = await this.getAsyncIgnoreCase(table, key);
  54. else {
  55. res = await r.table(table)
  56. .get(key)
  57. .run();
  58. }
  59. if (res)
  60. return res.value;
  61. else if (isArray && !noDefault)
  62. return [];
  63. return res;
  64. },
  65. getFilterAsync: async function ({ table, noDefault, filter, limit, offset }) {
  66. let res = r
  67. .table(table)
  68. .filter(filter);
  69. if (offset)
  70. res = res.skip(offset);
  71. if (limit)
  72. res = res.limit(limit);
  73. await res.run();
  74. if (res)
  75. return res;
  76. if (!noDefault)
  77. return [];
  78. return null;
  79. },
  80. getFilterFlat: async function ({ table, noDefault, filter }) {
  81. const res = await r
  82. .table(table)
  83. .filter(filter)
  84. .run();
  85. if (res)
  86. return res;
  87. if (!noDefault)
  88. return [];
  89. return null;
  90. },
  91. getAllAsync: async function ({
  92. table,
  93. key,
  94. isArray,
  95. noDefault
  96. }) {
  97. let res = await r.table(table)
  98. .run();
  99. if (res)
  100. return res;
  101. else if (isArray && !noDefault)
  102. return [];
  103. return res;
  104. },
  105. setAsync: async function ({
  106. table,
  107. key: id,
  108. value,
  109. conflict = 'update'
  110. }) {
  111. try {
  112. await r.table(table)
  113. .insert({
  114. id,
  115. value
  116. }, {
  117. conflict
  118. })
  119. .run();
  120. } catch (e) {
  121. this.logError(e, table, id);
  122. }
  123. },
  124. setFlat: async function ({
  125. table,
  126. value,
  127. conflict = 'update'
  128. }) {
  129. try {
  130. await r.table(table)
  131. .insert(value, { conflict })
  132. .run();
  133. } catch (e) {
  134. this.logError(e, table, JSON.stringify(value));
  135. }
  136. },
  137. deleteAsync: async function ({
  138. key,
  139. table
  140. }) {
  141. await r.table(table)
  142. .get(key)
  143. .delete()
  144. .run();
  145. },
  146. subscribe: function (table) {
  147. return r.table(table)
  148. .changes()
  149. .run();
  150. },
  151. append: async function ({
  152. table,
  153. key,
  154. value,
  155. field
  156. }) {
  157. try {
  158. await r.table(table)
  159. .get(key)
  160. .update(row => {
  161. return r.branch(
  162. row('value').typeOf().eq('ARRAY'),
  163. {
  164. [field]: row('value').setUnion(value)
  165. },
  166. {
  167. [field]: value
  168. }
  169. );
  170. })
  171. .run();
  172. } catch (e) {
  173. this.logError(e, table, key);
  174. }
  175. },
  176. exists: async function ({
  177. table,
  178. key
  179. }) {
  180. let res = await r.table(table)
  181. .get(key)
  182. .run();
  183. return !!res;
  184. },
  185. logError: async function (error, table, key) {
  186. try {
  187. const errorValue = `${error.toString()} | ${error.stack.toString()} | ${table} | ${key}`;
  188. await this.setAsync({
  189. key: new Date(),
  190. table: 'error',
  191. value: errorValue
  192. });
  193. } catch (e) {}
  194. process.send({
  195. event: 'onCrashed'
  196. });
  197. }
  198. };