index.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. })
  33. .then(async () => {
  34. mongoose.connection.on("error", err => this.log("ERROR", err));
  35. mongoose.connection.on("disconnected", () => {
  36. this.log("ERROR", "Disconnected, going to try to reconnect...");
  37. this.setStatus("RECONNECTING");
  38. });
  39. mongoose.connection.on("reconnected", () => {
  40. this.log("INFO", "Reconnected.");
  41. this.setStatus("READY");
  42. });
  43. mongoose.connection.on("reconnectFailed", () => {
  44. this.log("INFO", "Reconnect failed, stopping reconnecting.");
  45. this.setStatus("FAILED");
  46. });
  47. this.models = {
  48. song: mongoose.model("song", new mongoose.Schema({}, { strict: false })),
  49. queueSong: mongoose.model("queueSong", new mongoose.Schema({}, { strict: false })),
  50. station: mongoose.model("station", new mongoose.Schema({}, { strict: false })),
  51. user: mongoose.model("user", new mongoose.Schema({}, { strict: false })),
  52. activity: mongoose.model("activity", new mongoose.Schema({}, { strict: false })),
  53. playlist: mongoose.model("playlist", new mongoose.Schema({}, { strict: false })),
  54. news: mongoose.model("news", new mongoose.Schema({}, { strict: false })),
  55. report: mongoose.model("report", new mongoose.Schema({}, { strict: false })),
  56. punishment: mongoose.model("punishment", new mongoose.Schema({}, { strict: false }))
  57. };
  58. const files = fs.readdirSync(path.join(__dirname, "migrations"));
  59. const migrations = files.length;
  60. async.timesLimit(
  61. migrations,
  62. 1,
  63. (index, next) => {
  64. MigrationModule.runJob("RUN_MIGRATION", { index: index + 1 }, null, -1)
  65. .then(() => next())
  66. .catch(err => next(err));
  67. },
  68. err => {
  69. if (err) console.log("Migration error", err);
  70. else console.log("Migration completed");
  71. }
  72. );
  73. resolve();
  74. })
  75. .catch(err => {
  76. this.log("ERROR", err);
  77. reject(err);
  78. });
  79. });
  80. }
  81. /**
  82. * Returns a database model
  83. *
  84. * @param {object} payload - object containing the payload
  85. * @param {object} payload.modelName - name of the model to get
  86. * @returns {Promise} - returns promise (reject, resolve)
  87. */
  88. GET_MODEL(payload) {
  89. return new Promise(resolve => {
  90. resolve(MigrationModule.models[payload.modelName]);
  91. });
  92. }
  93. /**
  94. * Runs migrations
  95. *
  96. * @param {object} payload - object containing the payload
  97. * @param {object} payload.index - migration index
  98. * @returns {Promise} - returns promise (reject, resolve)
  99. */
  100. RUN_MIGRATION(payload) {
  101. return new Promise((resolve, reject) => {
  102. import(`./migrations/migration${payload.index}`).then(module => {
  103. this.log("INFO", `Running migration ${payload.index}`);
  104. module.default
  105. .apply(this, [MigrationModule])
  106. .then(response => {
  107. resolve(response);
  108. })
  109. .catch(err => {
  110. reject(err);
  111. });
  112. });
  113. });
  114. }
  115. }
  116. export default new _MigrationModule();