index.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import async from "async";
  2. import config from "config";
  3. import mongoose from "mongoose";
  4. import bluebird from "bluebird";
  5. import fs from "fs";
  6. import { fileURLToPath } from "url";
  7. import path from "path";
  8. import CoreClass from "../../core";
  9. const __filename = fileURLToPath(import.meta.url);
  10. const __dirname = path.dirname(__filename);
  11. let MigrationModule;
  12. mongoose.Promise = bluebird;
  13. class _MigrationModule extends CoreClass {
  14. // eslint-disable-next-line require-jsdoc
  15. constructor() {
  16. super("migration");
  17. MigrationModule = this;
  18. }
  19. /**
  20. * Initialises the migration module
  21. *
  22. * @returns {Promise} - returns promise (reject, resolve)
  23. */
  24. initialize() {
  25. return new Promise((resolve, reject) => {
  26. this.models = {};
  27. const mongoUrl = config.get("mongo").url;
  28. mongoose
  29. .connect(mongoUrl, {
  30. useNewUrlParser: true,
  31. useUnifiedTopology: true,
  32. useCreateIndex: true
  33. })
  34. .then(async () => {
  35. mongoose.connection.on("error", err => {
  36. this.log("ERROR", err);
  37. });
  38. mongoose.connection.on("disconnected", () => {
  39. this.log("ERROR", "Disconnected, going to try to reconnect...");
  40. this.setStatus("RECONNECTING");
  41. });
  42. mongoose.connection.on("reconnected", () => {
  43. this.log("INFO", "Reconnected.");
  44. this.setStatus("READY");
  45. });
  46. mongoose.connection.on("reconnectFailed", () => {
  47. this.log("INFO", "Reconnect failed, stopping reconnecting.");
  48. // this.failed = true;
  49. // this._lockdown();
  50. this.setStatus("FAILED");
  51. });
  52. this.models = {
  53. song: mongoose.model("song", new mongoose.Schema({}, { strict: false })),
  54. queueSong: mongoose.model("queueSong", new mongoose.Schema({}, { strict: false })),
  55. station: mongoose.model("station", new mongoose.Schema({}, { strict: false })),
  56. user: mongoose.model("user", new mongoose.Schema({}, { strict: false })),
  57. activity: mongoose.model("activity", new mongoose.Schema({}, { strict: false })),
  58. playlist: mongoose.model("playlist", new mongoose.Schema({}, { strict: false })),
  59. news: mongoose.model("news", new mongoose.Schema({}, { strict: false })),
  60. report: mongoose.model("report", new mongoose.Schema({}, { strict: false })),
  61. punishment: mongoose.model("punishment", new mongoose.Schema({}, { strict: false }))
  62. };
  63. const files = fs.readdirSync(path.join(__dirname, "migrations"));
  64. const migrations = files.length;
  65. async.timesLimit(
  66. migrations,
  67. 1,
  68. (index, next) => {
  69. MigrationModule.runJob("RUN_MIGRATION", { index: index + 1 }, null, -1)
  70. .then(() => {
  71. next();
  72. })
  73. .catch(err => {
  74. next(err);
  75. });
  76. },
  77. err => {
  78. if (err) console.log("Migration error", err);
  79. else console.log("Migration completed");
  80. }
  81. );
  82. resolve();
  83. })
  84. .catch(err => {
  85. this.log("ERROR", err);
  86. reject(err);
  87. });
  88. });
  89. }
  90. /**
  91. * Returns a database model
  92. *
  93. * @param {object} payload - object containing the payload
  94. * @param {object} payload.modelName - name of the model to get
  95. * @returns {Promise} - returns promise (reject, resolve)
  96. */
  97. GET_MODEL(payload) {
  98. return new Promise(resolve => {
  99. resolve(MigrationModule.models[payload.modelName]);
  100. });
  101. }
  102. /**
  103. * Runs migrations
  104. *
  105. * @param {object} payload - object containing the payload
  106. * @param {object} payload.index - migration index
  107. * @returns {Promise} - returns promise (reject, resolve)
  108. */
  109. RUN_MIGRATION(payload) {
  110. return new Promise((resolve, reject) => {
  111. import(`./migrations/migration${payload.index}`).then(module => {
  112. this.log("INFO", `Running migration ${payload.index}`);
  113. module.default
  114. .apply(this, [MigrationModule])
  115. .then(response => {
  116. resolve(response);
  117. })
  118. .catch(err => {
  119. reject(err);
  120. });
  121. });
  122. });
  123. }
  124. }
  125. export default new _MigrationModule();