Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 

222 righe
3.3 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. getAllAsync: async function ({
  81. table,
  82. key,
  83. isArray,
  84. noDefault
  85. }) {
  86. let res = await r.table(table)
  87. .run();
  88. if (res)
  89. return res;
  90. else if (isArray && !noDefault)
  91. return [];
  92. return res;
  93. },
  94. setAsync: async function ({
  95. table,
  96. key: id,
  97. value,
  98. conflict = 'update'
  99. }) {
  100. try {
  101. await r.table(table)
  102. .insert({
  103. id,
  104. value
  105. }, {
  106. conflict
  107. })
  108. .run();
  109. } catch (e) {
  110. this.logError(e, table, id);
  111. }
  112. },
  113. setFlat: async function ({
  114. table,
  115. value,
  116. conflict = 'update'
  117. }) {
  118. try {
  119. await r.table(table)
  120. .insert(value, { conflict })
  121. .run();
  122. } catch (e) {
  123. this.logError(e, table, JSON.stringify(value));
  124. }
  125. },
  126. deleteAsync: async function ({
  127. key,
  128. table
  129. }) {
  130. await r.table(table)
  131. .get(key)
  132. .delete()
  133. .run();
  134. },
  135. subscribe: function (table) {
  136. return r.table(table)
  137. .changes()
  138. .run();
  139. },
  140. append: async function ({
  141. table,
  142. key,
  143. value,
  144. field
  145. }) {
  146. try {
  147. await r.table(table)
  148. .get(key)
  149. .update(row => {
  150. return r.branch(
  151. row('value').typeOf().eq('ARRAY'),
  152. {
  153. [field]: row('value').setUnion(value)
  154. },
  155. {
  156. [field]: value
  157. }
  158. );
  159. })
  160. .run();
  161. } catch (e) {
  162. this.logError(e, table, key);
  163. }
  164. },
  165. exists: async function ({
  166. table,
  167. key
  168. }) {
  169. let res = await r.table(table)
  170. .get(key)
  171. .run();
  172. return !!res;
  173. },
  174. logError: async function (error, table, key) {
  175. try {
  176. const errorValue = `${error.toString()} | ${error.stack.toString()} | ${table} | ${key}`;
  177. await this.setAsync({
  178. key: new Date(),
  179. table: 'error',
  180. value: errorValue
  181. });
  182. } catch (e) {}
  183. process.send({
  184. event: 'onCrashed'
  185. });
  186. }
  187. };