accountSchema.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. 'use strict';
  2. const async = require("async");
  3. const coreClass = require("../core");
  4. const config = require('config');
  5. module.exports = class extends coreClass {
  6. constructor(name, moduleManager) {
  7. super(name, moduleManager);
  8. this.dependsOn = ["mongo"];
  9. }
  10. initialize() {
  11. return new Promise(async (resolve, reject) => {
  12. this.setStage(1);
  13. this.mongoModule = this.moduleManager.modules["mongo"];
  14. this.accountSchemaSchema = await this.mongoModule.schema("accountSchema");
  15. this.accountSchemaModel = await this.mongoModule.model("accountSchema");
  16. resolve();
  17. })
  18. }
  19. async getLatest() {
  20. return new Promise(async (resolve, reject) => {
  21. try { await this._validateHook(); } catch { return; }
  22. this.accountSchemaModel.find({}, null, { sort: "-version", limit: 1 }, (err, schemas) => {
  23. if (err || !schemas || schemas.length !== 1) reject(new Error("Something went wrong."))
  24. else resolve(schemas[0]);
  25. });
  26. });
  27. }
  28. async getAll() {
  29. return new Promise(async (resolve, reject) => {
  30. try { await this._validateHook(); } catch { return; }
  31. this.accountSchemaModel.find({}, null, { sort: "-version" }, (err, schemas) => {
  32. if (err || !schemas) reject(new Error("Something went wrong."))
  33. else resolve(schemas)
  34. });
  35. });
  36. }
  37. async getById(schemaId) {
  38. return new Promise(async (resolve, reject) => {
  39. try { await this._validateHook(); } catch { return; }
  40. this.accountSchemaModel.findById(schemaId, (err, schema) => {
  41. if (err || !schema) reject(new Error("Something went wrong."))
  42. else resolve(schema)
  43. });
  44. });
  45. }
  46. async import(name) {
  47. return new Promise(async (resolve, reject) => {
  48. try { await this._validateHook(); } catch { return; }
  49. this.accountSchemaModel.create(this.accountSchemaSchema, (err) => {
  50. if (err) reject(new Error("Something went wrong."))
  51. else resolve();
  52. });
  53. });
  54. }
  55. }