index.js 3.8 KB

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