@akanjs/server 0.9.43 → 0.9.44

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/cjs/src/boot.js CHANGED
@@ -56,6 +56,7 @@ var import_dgram = __toESM(require("dgram"));
56
56
  var import_events = __toESM(require("events"));
57
57
  var import_graphql_upload = require("graphql-upload");
58
58
  var import_meilisearch = require("meilisearch");
59
+ var import_mongoose2 = __toESM(require("mongoose"));
59
60
  var import_redis = require("redis");
60
61
  var import_base2 = require("./base.module");
61
62
  var import_gql = require("./gql");
@@ -110,9 +111,10 @@ const createNestApp = async ({ registerModules, serverMode = "federation", env,
110
111
  provide: "MEILI_CLIENT",
111
112
  useFactory: () => new import_meilisearch.MeiliSearch({ host: meiliUri, apiKey: (0, import_nest.generateMeiliKey)(env) })
112
113
  },
113
- { provide: "GLOBAL_ENV", useValue: env }
114
+ { provide: "GLOBAL_ENV", useValue: env },
115
+ { provide: "MONGO_CLIENT", useValue: import_mongoose2.default.connection }
114
116
  ],
115
- exports: ["REDIS_CLIENT", "MEILI_CLIENT", "GLOBAL_ENV"]
117
+ exports: ["REDIS_CLIENT", "MEILI_CLIENT", "GLOBAL_ENV", "MONGO_CLIENT"]
116
118
  })
117
119
  ], GlobalProvideModule);
118
120
  let AppModule = class {
package/cjs/src/schema.js CHANGED
@@ -206,13 +206,42 @@ const schemaOf = (modelRef, docRef, middleware) => {
206
206
  schema.methods[name] = Object.getOwnPropertyDescriptor(docRef.prototype, name)?.value;
207
207
  });
208
208
  schema.pre("save", async function(next) {
209
- const model = this.constructor;
210
- if (this.isNew)
211
- model.addSummary(["total", this.status]);
212
- else if (!!this.removedAt && this.isModified("removedAt"))
213
- model.subSummary(["total", this.status]);
209
+ const saveType = this.isNew ? "create" : this.isModified("removedAt") ? this.removedAt ? "remove" : "create" : "update";
210
+ const saveListeners = [
211
+ ...this.constructor.preSaveListenerSet,
212
+ ...saveType === "create" ? [...this.constructor.preCreateListenerSet] : saveType === "update" ? [...this.constructor.preUpdateListenerSet] : [...this.constructor.preRemoveListenerSet]
213
+ ];
214
+ await Promise.all(
215
+ saveListeners.map(async (listener) => {
216
+ try {
217
+ await listener(this, saveType);
218
+ } catch (e) {
219
+ import_common.Logger.error(
220
+ `Pre Save Listener Error ${this.constructor.modelName}: ${e instanceof Error ? e.message : typeof e === "string" ? e : "unknown error"}`
221
+ );
222
+ }
223
+ })
224
+ );
214
225
  next();
215
226
  });
227
+ schema.post("save", async function() {
228
+ const saveType = this.isNew ? "create" : this.isModified("removedAt") ? this.removedAt ? "remove" : "create" : "update";
229
+ const saveListeners = [
230
+ ...this.constructor.postSaveListenerSet,
231
+ ...saveType === "create" ? [...this.constructor.postCreateListenerSet] : saveType === "update" ? [...this.constructor.postUpdateListenerSet] : [...this.constructor.postRemoveListenerSet]
232
+ ];
233
+ await Promise.all(
234
+ saveListeners.map(async (listener) => {
235
+ try {
236
+ await listener(this, saveType);
237
+ } catch (e) {
238
+ import_common.Logger.error(
239
+ `Post Save Listener Error ${this.constructor.modelName}: ${e instanceof Error ? e.message : typeof e === "string" ? e : "unknown error"}`
240
+ );
241
+ }
242
+ })
243
+ );
244
+ });
216
245
  const onSchema = Object.getOwnPropertyDescriptor(middleware.prototype, "onSchema")?.value;
217
246
  onSchema?.(schema);
218
247
  schema.index({ removedAt: -1 });
package/esm/src/boot.js CHANGED
@@ -47,6 +47,7 @@ import dgram from "dgram";
47
47
  import events from "events";
48
48
  import { graphqlUploadExpress } from "graphql-upload";
49
49
  import { MeiliSearch } from "meilisearch";
50
+ import mongoose from "mongoose";
50
51
  import { createClient } from "redis";
51
52
  import { registerBaseModule } from "./base.module";
52
53
  import { DateScalar } from "./gql";
@@ -101,9 +102,10 @@ const createNestApp = async ({ registerModules, serverMode = "federation", env,
101
102
  provide: "MEILI_CLIENT",
102
103
  useFactory: () => new MeiliSearch({ host: meiliUri, apiKey: generateMeiliKey(env) })
103
104
  },
104
- { provide: "GLOBAL_ENV", useValue: env }
105
+ { provide: "GLOBAL_ENV", useValue: env },
106
+ { provide: "MONGO_CLIENT", useValue: mongoose.connection }
105
107
  ],
106
- exports: ["REDIS_CLIENT", "MEILI_CLIENT", "GLOBAL_ENV"]
108
+ exports: ["REDIS_CLIENT", "MEILI_CLIENT", "GLOBAL_ENV", "MONGO_CLIENT"]
107
109
  })
108
110
  ], GlobalProvideModule);
109
111
  let AppModule = class {
package/esm/src/schema.js CHANGED
@@ -8,7 +8,7 @@ import {
8
8
  Int,
9
9
  JSON
10
10
  } from "@akanjs/base";
11
- import { isDayjs } from "@akanjs/common";
11
+ import { isDayjs, Logger } from "@akanjs/common";
12
12
  import { getClassMeta, getFieldMetas, getFullModelRef, getInputModelRef } from "@akanjs/constant";
13
13
  import { getDefaultSchemaOptions, ObjectId } from "@akanjs/document";
14
14
  import { makeDefault } from "@akanjs/signal";
@@ -191,13 +191,42 @@ const schemaOf = (modelRef, docRef, middleware) => {
191
191
  schema.methods[name] = Object.getOwnPropertyDescriptor(docRef.prototype, name)?.value;
192
192
  });
193
193
  schema.pre("save", async function(next) {
194
- const model = this.constructor;
195
- if (this.isNew)
196
- model.addSummary(["total", this.status]);
197
- else if (!!this.removedAt && this.isModified("removedAt"))
198
- model.subSummary(["total", this.status]);
194
+ const saveType = this.isNew ? "create" : this.isModified("removedAt") ? this.removedAt ? "remove" : "create" : "update";
195
+ const saveListeners = [
196
+ ...this.constructor.preSaveListenerSet,
197
+ ...saveType === "create" ? [...this.constructor.preCreateListenerSet] : saveType === "update" ? [...this.constructor.preUpdateListenerSet] : [...this.constructor.preRemoveListenerSet]
198
+ ];
199
+ await Promise.all(
200
+ saveListeners.map(async (listener) => {
201
+ try {
202
+ await listener(this, saveType);
203
+ } catch (e) {
204
+ Logger.error(
205
+ `Pre Save Listener Error ${this.constructor.modelName}: ${e instanceof Error ? e.message : typeof e === "string" ? e : "unknown error"}`
206
+ );
207
+ }
208
+ })
209
+ );
199
210
  next();
200
211
  });
212
+ schema.post("save", async function() {
213
+ const saveType = this.isNew ? "create" : this.isModified("removedAt") ? this.removedAt ? "remove" : "create" : "update";
214
+ const saveListeners = [
215
+ ...this.constructor.postSaveListenerSet,
216
+ ...saveType === "create" ? [...this.constructor.postCreateListenerSet] : saveType === "update" ? [...this.constructor.postUpdateListenerSet] : [...this.constructor.postRemoveListenerSet]
217
+ ];
218
+ await Promise.all(
219
+ saveListeners.map(async (listener) => {
220
+ try {
221
+ await listener(this, saveType);
222
+ } catch (e) {
223
+ Logger.error(
224
+ `Post Save Listener Error ${this.constructor.modelName}: ${e instanceof Error ? e.message : typeof e === "string" ? e : "unknown error"}`
225
+ );
226
+ }
227
+ })
228
+ );
229
+ });
201
230
  const onSchema = Object.getOwnPropertyDescriptor(middleware.prototype, "onSchema")?.value;
202
231
  onSchema?.(schema);
203
232
  schema.index({ removedAt: -1 });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@akanjs/server",
3
- "version": "0.9.43",
3
+ "version": "0.9.44",
4
4
  "sourceType": "module",
5
5
  "publishConfig": {
6
6
  "access": "public"
package/src/module.d.ts CHANGED
@@ -4,7 +4,7 @@ import { Database } from "@akanjs/document";
4
4
  import { DynamicModule } from "@nestjs/common";
5
5
  interface DatabaseModuleCreateOptions {
6
6
  constant: ConstantModel<string, any, any, any, any, any>;
7
- database: Database<string, any, any, any, any, any, any, any, any>;
7
+ database: Database<string, any, any, any, any, any, any, any>;
8
8
  signal: Type;
9
9
  service: Type;
10
10
  }
@@ -1,5 +1,30 @@
1
1
  import type { Type } from "@akanjs/base";
2
2
  import { type TextDoc } from "@akanjs/constant";
3
+ import type { Types } from "mongoose";
4
+ export interface ChangedData {
5
+ _id: {
6
+ _data: string;
7
+ };
8
+ operationType: "update" | "insert" | "delete";
9
+ clusterTime: {
10
+ t: number;
11
+ i: number;
12
+ };
13
+ wallTime: Date;
14
+ ns: {
15
+ db: string;
16
+ coll: string;
17
+ };
18
+ documentKey: {
19
+ _id: Types.ObjectId;
20
+ };
21
+ updateDescription?: {
22
+ updatedFields: Record<string, any>;
23
+ removedFields: string[];
24
+ truncatedArrays: any[];
25
+ };
26
+ fullDocument?: Record<string, any>;
27
+ }
3
28
  export declare const makeTextFilter: (modelRef: Type) => (data: Record<string, any>, assignObj?: {
4
29
  [key: string]: string;
5
30
  }) => TextDoc;