index.js 4.0 KB

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