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ů.
 
 
 

133 řádky
2.2 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. 'character'/*,
  11. 'characterList',
  12. 'stash',
  13. 'skins',
  14. 'login',
  15. 'leaderboard',
  16. 'customMap',
  17. 'mail',
  18. 'customChannels',
  19. 'error',
  20. 'modLog',
  21. 'accountInfo'*/
  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. }
  67. },
  68. startConvert: async function () {
  69. return new Promise(res => {
  70. this.res = res;
  71. this.work();
  72. });
  73. },
  74. work: function () {
  75. if (!this.records.length) {
  76. this.res();
  77. return;
  78. }
  79. if (this.busy === config.maxBusy)
  80. return;
  81. let record = this.records.pop();
  82. if (record) {
  83. this.processRecord(record);
  84. this.work();
  85. }
  86. },
  87. processRecord: async function (record) {
  88. this.busy++;
  89. let id = record.key;
  90. let value = record.value
  91. .split('`')
  92. .join('\'');
  93. let obj = JSON.parse(value);
  94. await r.table(this.currentTable)
  95. .insert({
  96. id,
  97. value: obj
  98. })
  99. .run(this.connection);
  100. this.busy--;
  101. this.work();
  102. }
  103. };
  104. converter.init();