DataModule.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 { patchHistoryPlugin, patchEventEmitter } from "ts-patch-mongoose";
  10. import { readdir } from "fs/promises";
  11. import path from "path";
  12. import JobContext from "../JobContext";
  13. import BaseModule, { ModuleStatus } from "../BaseModule";
  14. import { UniqueMethods } from "../types/Modules";
  15. import { Models } from "../types/Models";
  16. import { Schemas } from "../types/Schemas";
  17. import documentVersionPlugin from "../schemas/plugins/documentVersion";
  18. import getDataPlugin from "../schemas/plugins/getData";
  19. import Migration from "../Migration";
  20. export default class DataModule extends BaseModule {
  21. private models?: Models;
  22. private mongoConnection?: Connection;
  23. // private redisClient?: RedisClientType;
  24. /**
  25. * Data Module
  26. */
  27. public constructor() {
  28. super("data");
  29. this.dependentModules = ["events"];
  30. }
  31. /**
  32. * startup - Startup data module
  33. */
  34. public override async startup() {
  35. await super.startup();
  36. await this.createMongoConnection();
  37. await this.runMigrations();
  38. await this.loadModels();
  39. await this.syncModelIndexes();
  40. // @ts-ignore
  41. // this.redisClient = createClient({ ...config.get("redis") });
  42. //
  43. // await this.redisClient.connect();
  44. //
  45. // const redisConfigResponse = await this.redisClient.sendCommand([
  46. // "CONFIG",
  47. // "GET",
  48. // "notify-keyspace-events"
  49. // ]);
  50. //
  51. // if (
  52. // !(
  53. // Array.isArray(redisConfigResponse) &&
  54. // redisConfigResponse[1] === "xE"
  55. // )
  56. // )
  57. // throw new Error(
  58. // `notify-keyspace-events is NOT configured correctly! It is set to: ${
  59. // (Array.isArray(redisConfigResponse) &&
  60. // redisConfigResponse[1]) ||
  61. // "unknown"
  62. // }`
  63. // );
  64. await super.started();
  65. }
  66. /**
  67. * shutdown - Shutdown data module
  68. */
  69. public override async shutdown() {
  70. await super.shutdown();
  71. // if (this.redisClient) await this.redisClient.quit();
  72. patchEventEmitter.removeAllListeners();
  73. if (this.mongoConnection) await this.mongoConnection.close();
  74. }
  75. /**
  76. * createMongoConnection - Create mongo connection
  77. */
  78. private async createMongoConnection() {
  79. const { user, password, host, port, database } = config.get<{
  80. user: string;
  81. password: string;
  82. host: string;
  83. port: number;
  84. database: string;
  85. }>("mongo");
  86. const mongoUrl = `mongodb://${user}:${password}@${host}:${port}/${database}`;
  87. this.mongoConnection = await mongoose
  88. .createConnection(mongoUrl)
  89. .asPromise();
  90. this.mongoConnection.set("runValidators", true);
  91. this.mongoConnection.set("sanitizeFilter", true);
  92. this.mongoConnection.set("strict", "throw");
  93. this.mongoConnection.set("strictQuery", "throw");
  94. }
  95. /**
  96. * registerEvents - Register events for schema with event module
  97. */
  98. private async registerEvents<
  99. ModelName extends keyof Models,
  100. SchemaType extends Schemas[keyof ModelName]
  101. >(modelName: ModelName, schema: SchemaType) {
  102. // const preMethods: string[] = [
  103. // "aggregate",
  104. // "count",
  105. // "countDocuments",
  106. // "deleteOne",
  107. // "deleteMany",
  108. // "estimatedDocumentCount",
  109. // "find",
  110. // "findOne",
  111. // "findOneAndDelete",
  112. // "findOneAndRemove",
  113. // "findOneAndReplace",
  114. // "findOneAndUpdate",
  115. // "init",
  116. // "insertMany",
  117. // "remove",
  118. // "replaceOne",
  119. // "save",
  120. // "update",
  121. // "updateOne",
  122. // "updateMany",
  123. // "validate"
  124. // ];
  125. // preMethods.forEach(preMethod => {
  126. // // @ts-ignore
  127. // schema.pre(preMethods, () => {
  128. // console.log(`Pre-${preMethod}!`);
  129. // });
  130. // });
  131. const { enabled, eventCreated, eventUpdated, eventDeleted } =
  132. schema.get("patchHistory") ?? {};
  133. if (!enabled) return;
  134. Object.entries({
  135. created: eventCreated,
  136. updated: eventUpdated,
  137. deleted: eventDeleted
  138. })
  139. .filter(([, event]) => !!event)
  140. .forEach(([action, event]) => {
  141. patchEventEmitter.on(event, async ({ doc }) => {
  142. await this.jobQueue.runJob("events", "publish", {
  143. channel: `model.${modelName}.${doc._id}.${action}`,
  144. value: doc
  145. });
  146. });
  147. });
  148. }
  149. /**
  150. * loadModel - Import and load model schema
  151. *
  152. * @param modelName - Name of the model
  153. * @returns Model
  154. */
  155. private async loadModel<ModelName extends keyof Models>(
  156. modelName: ModelName
  157. ): Promise<Models[ModelName]> {
  158. if (!this.mongoConnection) throw new Error("Mongo is not available");
  159. const { schema }: { schema: Schemas[ModelName] } = await import(
  160. `../schemas/${modelName.toString()}`
  161. );
  162. schema.plugin(documentVersionPlugin);
  163. schema.set("timestamps", schema.get("timestamps") ?? true);
  164. const patchHistoryConfig = {
  165. enabled: true,
  166. patchHistoryDisabled: true,
  167. eventCreated: `${modelName}.created`,
  168. eventUpdated: `${modelName}.updated`,
  169. eventDeleted: `${modelName}.deleted`,
  170. ...(schema.get("patchHistory") ?? {})
  171. };
  172. schema.set("patchHistory", patchHistoryConfig);
  173. if (patchHistoryConfig.enabled) {
  174. schema.plugin(patchHistoryPlugin, patchHistoryConfig);
  175. }
  176. const { enabled: getDataEnabled = false } = schema.get("getData") ?? {};
  177. if (getDataEnabled) schema.plugin(getDataPlugin);
  178. await this.registerEvents(modelName, schema);
  179. return this.mongoConnection.model(modelName.toString(), schema);
  180. }
  181. /**
  182. * loadModels - Load and initialize all models
  183. *
  184. * @returns Promise
  185. */
  186. private async loadModels() {
  187. mongoose.SchemaTypes.String.set("trim", true);
  188. this.models = {
  189. abc: await this.loadModel("abc"),
  190. news: await this.loadModel("news"),
  191. session: await this.loadModel("session"),
  192. station: await this.loadModel("station"),
  193. user: await this.loadModel("user")
  194. };
  195. }
  196. /**
  197. * syncModelIndexes - Sync indexes for all models
  198. */
  199. private async syncModelIndexes() {
  200. if (!this.models) throw new Error("Models not loaded");
  201. await Promise.all(
  202. Object.values(this.models).map(model => model.syncIndexes())
  203. );
  204. }
  205. /**
  206. * getModel - Get model
  207. *
  208. * @returns Model
  209. */
  210. public async getModel<ModelName extends keyof Models>(
  211. jobContext: JobContext,
  212. payload: ModelName | { name: ModelName }
  213. ) {
  214. if (!this.models) throw new Error("Models not loaded");
  215. if (this.getStatus() !== ModuleStatus.STARTED)
  216. throw new Error("Module not started");
  217. const name = typeof payload === "object" ? payload.name : payload;
  218. return this.models[name];
  219. }
  220. private async loadMigrations() {
  221. if (!this.mongoConnection) throw new Error("Mongo is not available");
  222. const migrations = await readdir(
  223. path.resolve(__dirname, "../schemas/migrations/")
  224. );
  225. return Promise.all(
  226. migrations.map(async migrationFile => {
  227. const { default: Migrate }: { default: typeof Migration } =
  228. await import(`../schemas/migrations/${migrationFile}`);
  229. return new Migrate(this.mongoConnection as Connection);
  230. })
  231. );
  232. }
  233. private async runMigrations() {
  234. const migrations = await this.loadMigrations();
  235. for (let i = 0; i < migrations.length; i += 1) {
  236. const migration = migrations[i];
  237. // eslint-disable-next-line no-await-in-loop
  238. await migration.up();
  239. }
  240. }
  241. }
  242. export type DataModuleJobs = {
  243. [Property in keyof UniqueMethods<DataModule>]: {
  244. payload: Parameters<UniqueMethods<DataModule>[Property]>[1];
  245. returns: Awaited<ReturnType<UniqueMethods<DataModule>[Property]>>;
  246. };
  247. };