index.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. }
  80. );
  81. resolve();
  82. })
  83. .catch(err => {
  84. this.log("ERROR", err);
  85. reject(err);
  86. });
  87. });
  88. }
  89. /**
  90. * Returns a database model
  91. *
  92. * @param {object} payload - object containing the payload
  93. * @param {object} payload.modelName - name of the model to get
  94. * @returns {Promise} - returns promise (reject, resolve)
  95. */
  96. GET_MODEL(payload) {
  97. return new Promise(resolve => {
  98. resolve(MigrationModule.models[payload.modelName]);
  99. });
  100. }
  101. /**
  102. * Runs migrations
  103. *
  104. * @param {object} payload - object containing the payload
  105. * @param {object} payload.index - migration index
  106. * @returns {Promise} - returns promise (reject, resolve)
  107. */
  108. RUN_MIGRATION(payload) {
  109. return new Promise((resolve, reject) => {
  110. import(`./migrations/migration${payload.index}`).then(module => {
  111. this.log("INFO", `Running migration ${payload.index}`);
  112. module.default
  113. .apply(this, [MigrationModule])
  114. .then(response => {
  115. resolve(response);
  116. })
  117. .catch(err => {
  118. reject(err);
  119. });
  120. });
  121. });
  122. }
  123. }
  124. export default new _MigrationModule();