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.
 
 
 

274 lines
4.4 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. //Create indices used for case-insensitive checks
  30. if (!(await r.table('login').indexList()).includes('idLowerCase')) {
  31. await r.table('login').indexCreate('idLowerCase', r.row('id').downcase());
  32. _.log('Created index: idLowerCase on table: login');
  33. }
  34. if (!(await r.table('character').indexList()).includes('idLowerCase')) {
  35. await r.table('character').indexCreate('idLowerCase', r.row('id').downcase());
  36. _.log('Created index: idLowerCase on table: character');
  37. }
  38. },
  39. createTable: async function (tableName) {
  40. try {
  41. await r.tableCreate(tableName).run();
  42. } catch (e) {
  43. if (!e.message.includes('already exists'))
  44. _.log(e);
  45. }
  46. },
  47. getAsyncIgnoreCase: async function (table, key) {
  48. const res = await r.table(table)
  49. .getAll(key.toLowerCase(), { index: 'idLowerCase' })
  50. .run();
  51. return res[0];
  52. },
  53. getAsync: async function ({
  54. table,
  55. key,
  56. isArray,
  57. noDefault,
  58. ignoreCase
  59. }) {
  60. let res = null;
  61. if (ignoreCase)
  62. res = await this.getAsyncIgnoreCase(table, key);
  63. else {
  64. res = await r.table(table)
  65. .get(key)
  66. .run();
  67. }
  68. if (res)
  69. return res.value;
  70. else if (isArray && !noDefault)
  71. return [];
  72. return res;
  73. },
  74. getFilterAsync: async function (
  75. { table, noDefault, filter, limit, offset, orderAsc, orderDesc }
  76. ) {
  77. let res = r
  78. .table(table)
  79. .filter(filter);
  80. if (orderAsc)
  81. res = res.orderBy(orderAsc);
  82. if (orderDesc)
  83. res = res.orderBy(r.desc(orderDesc));
  84. if (offset)
  85. res = res.skip(offset);
  86. if (limit)
  87. res = res.limit(limit);
  88. await res.run();
  89. if (res)
  90. return res;
  91. if (!noDefault)
  92. return [];
  93. return null;
  94. },
  95. getAllAsync: async function ({
  96. table,
  97. key,
  98. isArray,
  99. noDefault
  100. }) {
  101. let res = await r.table(table)
  102. .run();
  103. if (res)
  104. return res;
  105. else if (isArray && !noDefault)
  106. return [];
  107. return res;
  108. },
  109. setAsync: async function ({
  110. table,
  111. key: id,
  112. value,
  113. conflict = 'update'
  114. }) {
  115. try {
  116. await r.table(table)
  117. .insert({
  118. id,
  119. value
  120. }, {
  121. conflict
  122. })
  123. .run();
  124. } catch (e) {
  125. this.logError({
  126. sourceModule: 'ioRethink',
  127. sourceMethod: 'setAsync',
  128. error: e,
  129. info: {
  130. table,
  131. key: id,
  132. value: JSON.stringify(value)
  133. }
  134. });
  135. }
  136. },
  137. setFlat: async function ({
  138. table,
  139. value,
  140. conflict = 'update'
  141. }) {
  142. try {
  143. await r.table(table)
  144. .insert(value, { conflict })
  145. .run();
  146. } catch (e) {
  147. this.logError({
  148. sourceModule: 'ioRethink',
  149. sourceMethod: 'setFlat',
  150. error: e,
  151. info: {
  152. table,
  153. value: JSON.stringify(value)
  154. }
  155. });
  156. }
  157. },
  158. deleteAsync: async function ({
  159. key,
  160. table
  161. }) {
  162. await r.table(table)
  163. .get(key)
  164. .delete()
  165. .run();
  166. },
  167. subscribe: function (table) {
  168. return r.table(table)
  169. .changes()
  170. .run();
  171. },
  172. append: async function ({
  173. table,
  174. key,
  175. value,
  176. field
  177. }) {
  178. try {
  179. await r.table(table)
  180. .get(key)
  181. .update(row => {
  182. return r.branch(
  183. row('value').typeOf().eq('ARRAY'),
  184. {
  185. [field]: row('value').setUnion(value)
  186. },
  187. {
  188. [field]: value
  189. }
  190. );
  191. })
  192. .run();
  193. } catch (e) {
  194. this.logError({
  195. sourceModule: 'ioRethink',
  196. sourceMethod: 'append',
  197. error: e,
  198. info: {
  199. table,
  200. key,
  201. field,
  202. value: JSON.stringify(value)
  203. }
  204. });
  205. }
  206. },
  207. exists: async function ({
  208. table,
  209. key
  210. }) {
  211. let res = await r.table(table)
  212. .get(key)
  213. .run();
  214. return !!res;
  215. },
  216. logError: async function ({ sourceModule, sourceMethod, error, info }) {
  217. try {
  218. await this.setAsync({
  219. table: 'error',
  220. value: {
  221. date: new Date(),
  222. sourceModule,
  223. sourceMethod,
  224. error: error.toString(),
  225. stack: error.stack.toString(),
  226. info
  227. }
  228. });
  229. } catch (e) {}
  230. if (process.send) {
  231. process.send({
  232. event: 'onCrashed'
  233. });
  234. } else
  235. process.exit();
  236. }
  237. };