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.
 
 
 

261 lines
4.0 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 (
  66. { table, noDefault, filter, limit, offset, orderAsc, orderDesc }
  67. ) {
  68. let res = r
  69. .table(table)
  70. .filter(filter);
  71. if (orderAsc)
  72. res = res.orderBy(orderAsc);
  73. if (orderDesc)
  74. res = res.orderBy(r.desc(orderDesc));
  75. if (offset)
  76. res = res.skip(offset);
  77. if (limit)
  78. res = res.limit(limit);
  79. await res.run();
  80. if (res)
  81. return res;
  82. if (!noDefault)
  83. return [];
  84. return null;
  85. },
  86. getAllAsync: async function ({
  87. table,
  88. key,
  89. isArray,
  90. noDefault
  91. }) {
  92. let res = await r.table(table)
  93. .run();
  94. if (res)
  95. return res;
  96. else if (isArray && !noDefault)
  97. return [];
  98. return res;
  99. },
  100. setAsync: async function ({
  101. table,
  102. key: id,
  103. value,
  104. conflict = 'update'
  105. }) {
  106. try {
  107. await r.table(table)
  108. .insert({
  109. id,
  110. value
  111. }, {
  112. conflict
  113. })
  114. .run();
  115. } catch (e) {
  116. this.logError({
  117. sourceModule: 'ioRethink',
  118. sourceMethod: 'setAsync',
  119. error: e,
  120. info: {
  121. table,
  122. key: id,
  123. value: JSON.stringify(value)
  124. }
  125. });
  126. }
  127. },
  128. setFlat: async function ({
  129. table,
  130. value,
  131. conflict = 'update'
  132. }) {
  133. try {
  134. await r.table(table)
  135. .insert(value, { conflict })
  136. .run();
  137. } catch (e) {
  138. this.logError({
  139. sourceModule: 'ioRethink',
  140. sourceMethod: 'setFlat',
  141. error: e,
  142. info: {
  143. table,
  144. value: JSON.stringify(value)
  145. }
  146. });
  147. }
  148. },
  149. deleteAsync: async function ({
  150. key,
  151. table
  152. }) {
  153. await r.table(table)
  154. .get(key)
  155. .delete()
  156. .run();
  157. },
  158. subscribe: function (table) {
  159. return r.table(table)
  160. .changes()
  161. .run();
  162. },
  163. append: async function ({
  164. table,
  165. key,
  166. value,
  167. field
  168. }) {
  169. try {
  170. await r.table(table)
  171. .get(key)
  172. .update(row => {
  173. return r.branch(
  174. row('value').typeOf().eq('ARRAY'),
  175. {
  176. [field]: row('value').setUnion(value)
  177. },
  178. {
  179. [field]: value
  180. }
  181. );
  182. })
  183. .run();
  184. } catch (e) {
  185. this.logError({
  186. sourceModule: 'ioRethink',
  187. sourceMethod: 'append',
  188. error: e,
  189. info: {
  190. table,
  191. key,
  192. field,
  193. value: JSON.stringify(value)
  194. }
  195. });
  196. }
  197. },
  198. exists: async function ({
  199. table,
  200. key
  201. }) {
  202. let res = await r.table(table)
  203. .get(key)
  204. .run();
  205. return !!res;
  206. },
  207. logError: async function ({ sourceModule, sourceMethod, error, info }) {
  208. try {
  209. await this.setAsync({
  210. table: 'error',
  211. value: {
  212. date: new Date(),
  213. sourceModule,
  214. sourceMethod,
  215. error: error.toString(),
  216. stack: error.stack.toString(),
  217. info
  218. }
  219. });
  220. } catch (e) {}
  221. if (process.send) {
  222. process.send({
  223. event: 'onCrashed'
  224. });
  225. } else
  226. process.exit();
  227. }
  228. };