DataModule.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import config from "config";
  2. // import { createClient, RedisClientType } from "redis";
  3. import mongoose, {
  4. Connection,
  5. MongooseDefaultQueryMiddleware,
  6. MongooseDistinctQueryMiddleware,
  7. MongooseQueryOrDocumentMiddleware
  8. } from "mongoose";
  9. import JobContext from "../JobContext";
  10. import BaseModule, { ModuleStatus } from "../BaseModule";
  11. import { UniqueMethods } from "../types/Modules";
  12. import { Models, Schemas } from "../types/Models";
  13. import getDataPlugin from "../schemas/plugins/getData";
  14. export default class DataModule extends BaseModule {
  15. private models?: Models;
  16. private mongoConnection?: Connection;
  17. // private redisClient?: RedisClientType;
  18. /**
  19. * Data Module
  20. */
  21. public constructor() {
  22. super("data");
  23. }
  24. /**
  25. * startup - Startup data module
  26. */
  27. public override async startup() {
  28. await super.startup();
  29. const { user, password, host, port, database } = config.get<{
  30. user: string;
  31. password: string;
  32. host: string;
  33. port: number;
  34. database: string;
  35. }>("mongo");
  36. const mongoUrl = `mongodb://${user}:${password}@${host}:${port}/${database}`;
  37. this.mongoConnection = await mongoose
  38. .createConnection(mongoUrl)
  39. .asPromise();
  40. this.mongoConnection.set("runValidators", true);
  41. this.mongoConnection.set("sanitizeFilter", true);
  42. this.mongoConnection.set("strict", "throw");
  43. this.mongoConnection.set("strictQuery", "throw");
  44. mongoose.SchemaTypes.String.set("trim", true);
  45. this.mongoConnection.plugin(getDataPlugin, {
  46. tags: ["useGetDataPlugin"]
  47. });
  48. await this.loadModels();
  49. // @ts-ignore
  50. // this.redisClient = createClient({ ...config.get("redis") });
  51. //
  52. // await this.redisClient.connect();
  53. //
  54. // const redisConfigResponse = await this.redisClient.sendCommand([
  55. // "CONFIG",
  56. // "GET",
  57. // "notify-keyspace-events"
  58. // ]);
  59. //
  60. // if (
  61. // !(
  62. // Array.isArray(redisConfigResponse) &&
  63. // redisConfigResponse[1] === "xE"
  64. // )
  65. // )
  66. // throw new Error(
  67. // `notify-keyspace-events is NOT configured correctly! It is set to: ${
  68. // (Array.isArray(redisConfigResponse) &&
  69. // redisConfigResponse[1]) ||
  70. // "unknown"
  71. // }`
  72. // );
  73. await super.started();
  74. }
  75. /**
  76. * shutdown - Shutdown data module
  77. */
  78. public override async shutdown() {
  79. await super.shutdown();
  80. // if (this.redisClient) await this.redisClient.quit();
  81. if (this.mongoConnection) await this.mongoConnection.close();
  82. }
  83. /**
  84. * loadModel - Import and load model schema
  85. *
  86. * @param modelName - Name of the model
  87. * @returns Model
  88. */
  89. private async loadModel<ModelName extends keyof Models>(
  90. modelName: ModelName
  91. ) {
  92. if (!this.mongoConnection) throw new Error("Mongo is not available");
  93. const { schema }: { schema: Schemas[ModelName] } = await import(
  94. `../schemas/${modelName.toString()}`
  95. );
  96. const preMethods: string[] = [
  97. "aggregate",
  98. "count",
  99. "countDocuments",
  100. "deleteOne",
  101. "deleteMany",
  102. "estimatedDocumentCount",
  103. "find",
  104. "findOne",
  105. "findOneAndDelete",
  106. "findOneAndRemove",
  107. "findOneAndReplace",
  108. "findOneAndUpdate",
  109. "init",
  110. "insertMany",
  111. "remove",
  112. "replaceOne",
  113. "save",
  114. "update",
  115. "updateOne",
  116. "updateMany",
  117. "validate"
  118. ];
  119. preMethods.forEach(preMethod => {
  120. // @ts-ignore
  121. schema.pre(preMethods, () => {
  122. console.log(`Pre-${preMethod}!`);
  123. });
  124. });
  125. return this.mongoConnection.model(modelName.toString(), schema);
  126. }
  127. /**
  128. * loadModels - Load and initialize all models
  129. *
  130. * @returns Promise
  131. */
  132. private async loadModels() {
  133. this.models = {
  134. abc: await this.loadModel("abc"),
  135. news: await this.loadModel("news"),
  136. station: await this.loadModel("station")
  137. };
  138. }
  139. /**
  140. * getModel - Get model
  141. *
  142. * @returns Model
  143. */
  144. public async getModel<ModelName extends keyof Models>(
  145. jobContext: JobContext,
  146. payload: ModelName | { name: ModelName }
  147. ) {
  148. if (!this.models) throw new Error("Models not loaded");
  149. if (this.getStatus() !== ModuleStatus.STARTED)
  150. throw new Error("Module not started");
  151. const name = typeof payload === "object" ? payload.name : payload;
  152. return this.models[name];
  153. }
  154. }
  155. export type DataModuleJobs = {
  156. [Property in keyof UniqueMethods<DataModule>]: {
  157. payload: Parameters<UniqueMethods<DataModule>[Property]>[1];
  158. returns: Awaited<ReturnType<UniqueMethods<DataModule>[Property]>>;
  159. };
  160. };