convertSchema.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. 'use strict';
  2. const async = require("async");
  3. const fs = require("fs");
  4. const path = require("path");
  5. const coreClass = require("../core");
  6. const config = require('config');
  7. module.exports = class extends coreClass {
  8. constructor(name, moduleManager) {
  9. super(name, moduleManager);
  10. this.dependsOn = ["mongo"];
  11. }
  12. initialize() {
  13. return new Promise(async (resolve, reject) => {
  14. this.setStage(1);
  15. this.mongoModule = this.moduleManager.modules["mongo"];
  16. this.convertSchemaSchema = await this.mongoModule.schema("convertSchema");
  17. this.convertSchemaModel = await this.mongoModule.model("convertSchema");
  18. this.schemaDirectoryPath = path.join(__dirname, "..", "schemas", "convert");
  19. resolve();
  20. })
  21. }
  22. async getForVersion(version) {
  23. return new Promise(async (resolve, reject) => {
  24. try { await this._validateHook(); } catch { return; }
  25. this.convertSchemaModel.findOne({ versionFrom: version }, (err, schema) => {
  26. if (err) reject(new Error(err.message));
  27. else if (!schema) reject(new Error("Schema not found."));
  28. else resolve(schema)
  29. });
  30. });
  31. }
  32. async getById(schemaId) {
  33. return new Promise(async (resolve, reject) => {
  34. try { await this._validateHook(); } catch { return; }
  35. this.convertSchemaModel.findById(schemaId, (err, schema) => {
  36. if (err || !schema) reject(new Error("Something went wrong."))
  37. else resolve(schema)
  38. });
  39. });
  40. }
  41. async removeById(schemaId) {
  42. return new Promise(async (resolve, reject) => {
  43. try { await this._validateHook(); } catch { return; }
  44. this.convertSchemaModel.deleteOne({ _id: schemaId }, (err, res) => {
  45. if (err) reject(new Error("Something went wrong."));
  46. else if (res.deletedCount !== 1) reject(new Error("Nothing was removed."));
  47. else resolve();
  48. });
  49. });
  50. }
  51. async getAll() {
  52. return new Promise(async (resolve, reject) => {
  53. try { await this._validateHook(); } catch { return; }
  54. this.convertSchemaModel.find({ }, (err, schemas) => {
  55. if (err) reject(new Error(err.message));
  56. else resolve(schemas)
  57. });
  58. });
  59. }
  60. async listSchemasInDirectory() {
  61. return new Promise(async (resolve, reject) => {
  62. try { await this._validateHook(); } catch { return; }
  63. fs.readdir(this.schemaDirectoryPath, (err, files) => {
  64. if (err) reject(new Error(err.message))
  65. else resolve(files.map(file => file.substring(0, file.length - 3)));
  66. });
  67. });
  68. }
  69. async import(name) {
  70. return new Promise(async (resolve, reject) => {
  71. try { await this._validateHook(); } catch { return; }
  72. let schema = require(`../schemas/convert/${name}`);
  73. this.convertSchemaModel.create(schema, (err) => {
  74. if (err) reject(new Error(err.message))
  75. else resolve();
  76. });
  77. });
  78. }
  79. }