No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 

148 líneas
2.1 KiB

  1. let r = require('rethinkdb');
  2. let serverConfig = require('../config/serverConfig');
  3. const tables = [
  4. 'character',
  5. 'characterList',
  6. 'stash',
  7. 'skins',
  8. 'login',
  9. 'leaderboard',
  10. 'customMap',
  11. 'mail',
  12. 'customChannels',
  13. 'error',
  14. 'modLog',
  15. 'accountInfo'
  16. ];
  17. module.exports = {
  18. connection: null,
  19. useDb: 'dev',
  20. init: async function (cbReady) {
  21. const dbConfig = {
  22. host: serverConfig.dbHost,
  23. port: serverConfig.dbPort
  24. };
  25. this.connection = await r.connect(dbConfig);
  26. await this.connection.use(this.useDb);
  27. await this.create();
  28. cbReady();
  29. },
  30. create: async function () {
  31. try {
  32. await r.dbCreate('dev').run(this.connection);
  33. } catch (e) {
  34. }
  35. for (const table of tables) {
  36. try {
  37. await r.tableCreate(table).run(this.connection);
  38. } catch (e) {
  39. if (!e.message.includes('already exists'))
  40. _.log(e);
  41. }
  42. }
  43. },
  44. getAsync: async function ({
  45. table,
  46. key,
  47. isArray,
  48. noDefault
  49. }) {
  50. let res = await r.table(table)
  51. .get(key)
  52. .run(this.connection);
  53. if (res)
  54. return res.value;
  55. else if (isArray && !noDefault)
  56. return [];
  57. return res;
  58. },
  59. getAllAsync: async function ({
  60. table,
  61. key,
  62. isArray,
  63. noDefault
  64. }) {
  65. let res = await r.table(table)
  66. .run(this.connection);
  67. res = await res.toArray();
  68. if (res)
  69. return res;
  70. else if (isArray && !noDefault)
  71. return [];
  72. return res;
  73. },
  74. setAsync: async function ({
  75. table,
  76. key: id,
  77. value,
  78. conflict = 'update'
  79. }) {
  80. await r.table(table)
  81. .insert({
  82. id,
  83. value
  84. }, {
  85. conflict
  86. })
  87. .run(this.connection);
  88. },
  89. deleteAsync: async function ({
  90. key,
  91. table
  92. }) {
  93. await r.table(table)
  94. .get(key)
  95. .delete()
  96. .run(this.connection);
  97. },
  98. subscribe: function (table) {
  99. return r.table(table)
  100. .changes()
  101. .run(this.connection);
  102. },
  103. append: async function ({
  104. table,
  105. key,
  106. value,
  107. field
  108. }) {
  109. await r.table(table)
  110. .get(key)
  111. .update({
  112. [field]: r.row('value').append(...value)
  113. })
  114. .run(this.connection);
  115. },
  116. exists: async function ({
  117. table,
  118. key
  119. }) {
  120. let res = await r.table(table)
  121. .get(key)
  122. .run(this.connection);
  123. return !!res;
  124. }
  125. };