accountSchema.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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.accountSchemaSchema = await this.mongoModule.schema("accountSchema");
  17. this.accountSchemaModel = await this.mongoModule.model("accountSchema");
  18. this.schemaDirectoryPath = path.join(__dirname, "..", "schemas", "account");
  19. resolve();
  20. })
  21. }
  22. async getLatest() {
  23. return new Promise(async (resolve, reject) => {
  24. try { await this._validateHook(); } catch { return; }
  25. this.accountSchemaModel.find({}, null, { sort: "-version", limit: 1 }, (err, schemas) => {
  26. if (err || !schemas || schemas.length !== 1) reject(new Error("Something went wrong."))
  27. else resolve(schemas[0]);
  28. });
  29. });
  30. }
  31. async getAllVersions() {
  32. return new Promise(async (resolve, reject) => {
  33. try { await this._validateHook(); } catch { return; }
  34. this.accountSchemaModel.find({}, null, { sort: "-version" }, (err, schemas) => {
  35. if (err || !schemas) reject(new Error("Something went wrong."))
  36. else resolve(schemas.map(schema => schema.version));
  37. });
  38. });
  39. }
  40. async getAll() {
  41. return new Promise(async (resolve, reject) => {
  42. try { await this._validateHook(); } catch { return; }
  43. this.accountSchemaModel.find({}, null, { sort: "-version" }, (err, schemas) => {
  44. if (err || !schemas) reject(new Error("Something went wrong."))
  45. else resolve(schemas)
  46. });
  47. });
  48. }
  49. async getById(schemaId) {
  50. return new Promise(async (resolve, reject) => {
  51. try { await this._validateHook(); } catch { return; }
  52. this.accountSchemaModel.findById(schemaId, (err, schema) => {
  53. if (err || !schema) reject(new Error("Something went wrong."))
  54. else resolve(schema)
  55. });
  56. });
  57. }
  58. async removeById(schemaId) {
  59. return new Promise(async (resolve, reject) => {
  60. try { await this._validateHook(); } catch { return; }
  61. this.accountSchemaModel.deleteOne({ _id: schemaId }, (err, res) => {
  62. if (err) reject(new Error("Something went wrong."));
  63. else if (res.deletedCount !== 1) reject(new Error("Nothing was removed."));
  64. else resolve();
  65. });
  66. });
  67. }
  68. async getByVersion(version) {
  69. return new Promise(async (resolve, reject) => {
  70. try { await this._validateHook(); } catch { return; }
  71. this.accountSchemaModel.findOne({ version }, (err, schema) => {
  72. if (err || !schema) reject(new Error("Something went wrong."))
  73. else resolve(schema)
  74. });
  75. });
  76. }
  77. async listSchemasInDirectory() {
  78. return new Promise(async (resolve, reject) => {
  79. try { await this._validateHook(); } catch { return; }
  80. fs.readdir(this.schemaDirectoryPath, (err, files) => {
  81. if (err) reject(new Error(err.message))
  82. else resolve(files.map(file => file.substring(0, file.length - 3)));
  83. });
  84. });
  85. }
  86. async import(name) {
  87. return new Promise(async (resolve, reject) => {
  88. try { await this._validateHook(); } catch { return; }
  89. let schema = require(`../schemas/account/${name}`);
  90. this.accountSchemaModel.create(schema, (err) => {
  91. if (err) reject(new Error(err.message))
  92. else resolve();
  93. });
  94. });
  95. }
  96. }