DataModule.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import config from "config";
  2. import { createClient, RedisClientType } from "redis";
  3. import mongoose from "mongoose";
  4. import JobContext from "../JobContext";
  5. import BaseModule from "../BaseModule";
  6. import { UniqueMethods } from "../types/Modules";
  7. import { Models, Schemas } from "../types/Models";
  8. export default class DataModule extends BaseModule {
  9. private models?: Models;
  10. private redisClient?: RedisClientType;
  11. /**
  12. * Data Module
  13. */
  14. public constructor() {
  15. super("data");
  16. }
  17. /**
  18. * startup - Startup data module
  19. */
  20. public override async startup() {
  21. await super.startup();
  22. const mongoUrl = config.get<string>("mongo.url");
  23. await mongoose.connect(mongoUrl);
  24. await this.loadModels();
  25. const { url } = config.get<{ url: string }>("redis");
  26. this.redisClient = createClient({ url });
  27. await this.redisClient.connect();
  28. const redisConfigResponse = await this.redisClient.sendCommand([
  29. "CONFIG",
  30. "GET",
  31. "notify-keyspace-events"
  32. ]);
  33. if (
  34. !(
  35. Array.isArray(redisConfigResponse) &&
  36. redisConfigResponse[1] === "xE"
  37. )
  38. )
  39. throw new Error(
  40. `notify-keyspace-events is NOT configured correctly! It is set to: ${
  41. (Array.isArray(redisConfigResponse) &&
  42. redisConfigResponse[1]) ||
  43. "unknown"
  44. }`
  45. );
  46. await super.started();
  47. }
  48. /**
  49. * shutdown - Shutdown data module
  50. */
  51. public override async shutdown() {
  52. await super.shutdown();
  53. if (this.redisClient) await this.redisClient.quit();
  54. await mongoose.disconnect();
  55. }
  56. /**
  57. * loadModel - Import and load model schema
  58. *
  59. * @param modelName - Name of the model
  60. * @returns Model
  61. */
  62. private async loadModel<ModelName extends keyof Models>(
  63. modelName: ModelName
  64. ) {
  65. const { schema }: { schema: Schemas[ModelName] } = await import(
  66. `../models/${modelName.toString()}`
  67. );
  68. return mongoose.model(modelName.toString(), schema);
  69. }
  70. /**
  71. * loadModels - Load and initialize all models
  72. *
  73. * @returns Promise
  74. */
  75. private async loadModels() {
  76. this.models = {
  77. abc: await this.loadModel("abc"),
  78. station: await this.loadModel("station")
  79. };
  80. }
  81. /**
  82. * getModel - Get model
  83. *
  84. * @param modelName - Name of the model
  85. * @returns Model
  86. */
  87. public getModel<ModelName extends keyof Models>(
  88. jobContext: JobContext,
  89. modelName: ModelName
  90. ) {
  91. if (!this.models) throw new Error("Models not loaded");
  92. return this.models[modelName];
  93. }
  94. }
  95. export type DataModuleJobs = {
  96. [Property in keyof UniqueMethods<DataModule>]: {
  97. payload: Parameters<UniqueMethods<DataModule>[Property]>[1];
  98. returns: Awaited<ReturnType<UniqueMethods<DataModule>[Property]>>;
  99. };
  100. };