index.js 3.7 KB

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