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.
 
 
 

284 lines
4.6 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. deleteFilterAsync: async function ({
  168. table,
  169. filter
  170. }) {
  171. await r.table(table)
  172. .filter(filter)
  173. .delete()
  174. .run();
  175. },
  176. subscribe: function (table) {
  177. return r.table(table)
  178. .changes()
  179. .run();
  180. },
  181. append: async function ({
  182. table,
  183. key,
  184. value,
  185. field
  186. }) {
  187. try {
  188. await r.table(table)
  189. .get(key)
  190. .update(row => {
  191. return r.branch(
  192. row('value').typeOf().eq('ARRAY'),
  193. {
  194. [field]: row('value').setUnion(value)
  195. },
  196. {
  197. [field]: value
  198. }
  199. );
  200. })
  201. .run();
  202. } catch (e) {
  203. this.logError({
  204. sourceModule: 'ioRethink',
  205. sourceMethod: 'append',
  206. error: e,
  207. info: {
  208. table,
  209. key,
  210. field,
  211. value: JSON.stringify(value)
  212. }
  213. });
  214. }
  215. },
  216. exists: async function ({
  217. table,
  218. key
  219. }) {
  220. let res = await r.table(table)
  221. .get(key)
  222. .run();
  223. return !!res;
  224. },
  225. logError: async function ({ sourceModule, sourceMethod, error, info }) {
  226. try {
  227. await this.setAsync({
  228. table: 'error',
  229. value: {
  230. date: new Date(),
  231. sourceModule,
  232. sourceMethod,
  233. error: error.toString(),
  234. stack: error.stack.toString(),
  235. info
  236. }
  237. });
  238. } catch (e) {}
  239. if (process.send) {
  240. process.send({
  241. event: 'onCrashed'
  242. });
  243. } else
  244. process.exit();
  245. }
  246. };