index.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. * @returns {Promise} - returns promise (reject, resolve)
  22. */
  23. initialize() {
  24. return new Promise((resolve, reject) => {
  25. this.models = {};
  26. const { user, password, host, port, database } = config.get("mongo");
  27. mongoose
  28. .connect(`mongodb://${user}:${password}@${host}:${port}/${database}`, {
  29. useNewUrlParser: true,
  30. useUnifiedTopology: true
  31. })
  32. .then(async () => {
  33. mongoose.connection.on("error", err => this.log("ERROR", err));
  34. mongoose.connection.on("disconnected", () => {
  35. this.log("ERROR", "Disconnected, going to try to reconnect...");
  36. this.setStatus("RECONNECTING");
  37. });
  38. mongoose.connection.on("reconnected", () => {
  39. this.log("INFO", "Reconnected.");
  40. this.setStatus("READY");
  41. });
  42. mongoose.connection.on("reconnectFailed", () => {
  43. this.log("INFO", "Reconnect failed, stopping reconnecting.");
  44. this.setStatus("FAILED");
  45. });
  46. this.models = {
  47. song: mongoose.model("song", new mongoose.Schema({}, { strict: false })),
  48. queueSong: mongoose.model("queueSong", new mongoose.Schema({}, { strict: false })),
  49. station: mongoose.model("station", new mongoose.Schema({}, { strict: false })),
  50. user: mongoose.model("user", new mongoose.Schema({}, { strict: false })),
  51. activity: mongoose.model("activity", new mongoose.Schema({}, { strict: false })),
  52. playlist: mongoose.model("playlist", new mongoose.Schema({}, { strict: false })),
  53. news: mongoose.model("news", new mongoose.Schema({}, { strict: false })),
  54. report: mongoose.model("report", new mongoose.Schema({}, { strict: false })),
  55. punishment: mongoose.model("punishment", new mongoose.Schema({}, { strict: false })),
  56. ratings: mongoose.model("ratings", new mongoose.Schema({}, { strict: false })),
  57. stationHistory: mongoose.model("stationHistory", 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. * @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. * @param {object} payload - object containing the payload
  96. * @param {object} payload.index - migration index
  97. * @returns {Promise} - returns promise (reject, resolve)
  98. */
  99. RUN_MIGRATION(payload) {
  100. return new Promise((resolve, reject) => {
  101. import(`./migrations/migration${payload.index}`).then(module => {
  102. this.log("INFO", `Running migration ${payload.index}`);
  103. module.default
  104. .apply(this, [MigrationModule])
  105. .then(response => {
  106. resolve(response);
  107. })
  108. .catch(err => {
  109. reject(err);
  110. });
  111. });
  112. });
  113. }
  114. }
  115. export default new _MigrationModule();