Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 

140 řádky
2.3 KiB

  1. let sqlite = require('sqlite3').verbose();
  2. let r = require('rethinkdb');
  3. let util = require('util');
  4. const config = {
  5. file: './storage.db',
  6. dbName: 'dev',
  7. dropTables: true,
  8. maxBusy: 100,
  9. tables: [
  10. 'characterList',
  11. 'stash',
  12. 'skins',
  13. 'login',
  14. 'leaderboard',
  15. 'customMap',
  16. 'mail',
  17. 'customChannels',
  18. 'error',
  19. 'modLog',
  20. 'accountInfo',
  21. 'character'
  22. ]
  23. };
  24. let converter = {
  25. dbS: null,
  26. dbR: null,
  27. connection: null,
  28. res: null,
  29. busy: 0,
  30. records: [],
  31. currentTable: null,
  32. init: async function () {
  33. this.dbS = new sqlite.Database(config.file, this.onDbCreated.bind(this));
  34. },
  35. onDbCreated: async function () {
  36. this.connection = await r.connect({
  37. host: 'localhost',
  38. port: 28015
  39. });
  40. await this.connection.use(this.useDb);
  41. await this.setupRethink();
  42. await this.convertTables();
  43. },
  44. setupRethink: async function () {
  45. try {
  46. await r.dbCreate(config.dbName).run(this.connection);
  47. } catch (e) {
  48. }
  49. for (const table of config.tables) {
  50. try {
  51. if (config.dropTables)
  52. await r.tableDrop(table).run(this.connection);
  53. await r.tableCreate(table).run(this.connection);
  54. } catch (e) {
  55. if (!e.msg.includes('already exists'))
  56. console.log(e);
  57. }
  58. }
  59. },
  60. convertTables: async function () {
  61. for (let table of config.tables) {
  62. this.currentTable = table;
  63. this.records = await util.promisify(this.dbS.all.bind(this.dbS))(`SELECT * FROM ${table}`);
  64. console.log(`${table}: ${this.records.length} records`);
  65. await this.startConvert();
  66. console.log('done');
  67. }
  68. },
  69. startConvert: async function () {
  70. return new Promise(res => {
  71. this.res = res;
  72. this.work();
  73. });
  74. },
  75. work: function () {
  76. if (!this.records.length) {
  77. this.res();
  78. return;
  79. }
  80. if (this.busy === config.maxBusy)
  81. return;
  82. let record = this.records.pop();
  83. if (record) {
  84. this.processRecord(record);
  85. this.work();
  86. }
  87. },
  88. processRecord: async function (record) {
  89. this.busy++;
  90. let id = record.key;
  91. let value = record.value
  92. .split('`')
  93. .join('\'');
  94. let obj = value;
  95. if (!['login'].includes(this.currentTable)) {
  96. if (this.currentTable === 'mail' && value === '')
  97. value = '{}';
  98. obj = JSON.parse(value);
  99. }
  100. await r.table(this.currentTable)
  101. .insert({
  102. id,
  103. value: obj
  104. })
  105. .run(this.connection);
  106. this.busy--;
  107. this.work();
  108. }
  109. };
  110. converter.init();