@tsed/mongoose 8.4.0 → 8.4.2

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.
@@ -1 +1 @@
1
- {"version":3,"file":"postHook.js","sourceRoot":"","sources":["../../../src/decorators/postHook.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,eAAe,EAAE,cAAc,EAAwB,MAAM,YAAY,CAAC;AAGlF,OAAO,EAAC,aAAa,EAAC,MAAM,2BAA2B,CAAC;AAgDxD,MAAM,UAAU,QAAQ,CAAU,MAAc,EAAE,GAAG,MAAa;IAChE,OAAO,CAAC,CAAC,GAAG,IAAW,EAAE,EAAE;QACzB,IAAI,OAAO,GAAwB,MAAM,CAAC,CAAC,CAAC,CAAC;QAC7C,IAAI,EAAE,GAA0B,MAAM,CAAC,CAAC,CAAC,CAAC;QAE1C,IAAI,eAAe,CAAC,IAAI,CAAC,KAAK,cAAc,CAAC,UAAU,EAAE,CAAC;YACxD,OAAO,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC;YACpB,EAAE,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;QACtC,CAAC;QAED,aAAa,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE;YACrB,IAAI,EAAE;gBACJ;oBACE,MAAM;oBACN,EAAE;oBACF,OAAO;iBACR;aACF;SACF,CAAC,CAAC;IACL,CAAC,CAAQ,CAAC;AACZ,CAAC"}
1
+ {"version":3,"file":"postHook.js","sourceRoot":"","sources":["../../../src/decorators/postHook.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,eAAe,EAAE,cAAc,EAAwB,MAAM,YAAY,CAAC;AAGlF,OAAO,EAAC,aAAa,EAAC,MAAM,2BAA2B,CAAC;AAgDxD,MAAM,UAAU,QAAQ,CAAU,MAAuB,EAAE,GAAG,MAAa;IACzE,OAAO,CAAC,CAAC,GAAG,IAAW,EAAE,EAAE;QACzB,IAAI,OAAO,GAAwB,MAAM,CAAC,CAAC,CAAC,CAAC;QAC7C,IAAI,EAAE,GAA0B,MAAM,CAAC,CAAC,CAAC,CAAC;QAE1C,IAAI,eAAe,CAAC,IAAI,CAAC,KAAK,cAAc,CAAC,UAAU,EAAE,CAAC;YACxD,OAAO,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC;YACpB,EAAE,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;QACtC,CAAC;QAED,aAAa,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE;YACrB,IAAI,EAAE;gBACJ;oBACE,MAAM;oBACN,EAAE;oBACF,OAAO;iBACR;aACF;SACF,CAAC,CAAC;IACL,CAAC,CAAQ,CAAC;AACZ,CAAC"}
@@ -1,67 +1,29 @@
1
1
  import { decoratorTypeOf, DecoratorTypes } from "@tsed/core";
2
2
  import { schemaOptions } from "../utils/schemaOptions.js";
3
- /**
4
- *
5
- * We can simply attach a `@PreHook` decorator to your model class and
6
- * define the hook function like you normally would in Mongoose.
7
- *
8
- * ```typescript
9
- * import {Ignore, Required} from "@tsed/platform-http";
10
- * import {PreHook, Model} from "@tsed/mongoose";
11
- *
12
- * @Model()
13
- * @PreHook("save", (car: CarModel, next) => {
14
- * if (car.model === 'Tesla') {
15
- * car.isFast = true;
16
- * }
17
- * next();
18
- *})
19
- * export class CarModel {
20
- *
21
- * @Ignore()
22
- * _id: string;
23
- *
24
- * @Required()
25
- * model: string;
26
- *
27
- * @Required()
28
- * isFast: boolean;
29
- *
30
- * // or Prehook on static method
31
- * @PreHook("save")
32
- * static preSave(car: CarModel, next) {
33
- * if (car.model === 'Tesla') {
34
- * car.isFast = true;
35
- * }
36
- * next();
37
- * }
38
- * }
39
- * ```
40
- *
41
- * This will execute the pre-save hook each time a `CarModel` document is saved.
42
- *
43
- * @param {string} method
44
- * @param fn
45
- * @param options
46
- * @returns {Function}
47
- * @decorator
48
- * @class
49
- */
50
- export function PreHook(method, fn, options) {
3
+ export function PreHook(method, ...decoratorArgs) {
51
4
  return (...args) => {
52
5
  if (decoratorTypeOf(args) === DecoratorTypes.METHOD_STC) {
53
- options = fn;
54
- fn = args[0][args[1]].bind(args[0]);
6
+ schemaOptions(args[0], {
7
+ pre: [
8
+ {
9
+ method,
10
+ fn: args[0][args[1]].bind(args[0]),
11
+ options: decoratorArgs[0]
12
+ }
13
+ ]
14
+ });
15
+ }
16
+ else {
17
+ schemaOptions(args[0], {
18
+ pre: [
19
+ {
20
+ method,
21
+ fn: decoratorArgs[0],
22
+ options: decoratorArgs[1]
23
+ }
24
+ ]
25
+ });
55
26
  }
56
- schemaOptions(args[0], {
57
- pre: [
58
- {
59
- method,
60
- fn: fn,
61
- options
62
- }
63
- ]
64
- });
65
27
  };
66
28
  }
67
29
  //# sourceMappingURL=preHook.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"preHook.js","sourceRoot":"","sources":["../../../src/decorators/preHook.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,eAAe,EAAE,cAAc,EAAC,MAAM,YAAY,CAAC;AAG3D,OAAO,EAAC,aAAa,EAAC,MAAM,2BAA2B,CAAC;AAExD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8CG;AACH,MAAM,UAAU,OAAO,CAAU,MAAc,EAAE,EAA+C,EAAE,OAA6B;IAC7H,OAAO,CAAC,GAAG,IAAW,EAAE,EAAE;QACxB,IAAI,eAAe,CAAC,IAAI,CAAC,KAAK,cAAc,CAAC,UAAU,EAAE,CAAC;YACxD,OAAO,GAAG,EAAyB,CAAC;YACpC,EAAE,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;QACtC,CAAC;QAED,aAAa,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE;YACrB,GAAG,EAAE;gBACH;oBACE,MAAM;oBACN,EAAE,EAAE,EAA0B;oBAC9B,OAAO;iBACR;aACF;SACF,CAAC,CAAC;IACL,CAAC,CAAC;AACJ,CAAC"}
1
+ {"version":3,"file":"preHook.js","sourceRoot":"","sources":["../../../src/decorators/preHook.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,eAAe,EAAE,cAAc,EAAwB,MAAM,YAAY,CAAC;AAGlF,OAAO,EAAC,aAAa,EAAC,MAAM,2BAA2B,CAAC;AAmDxD,MAAM,UAAU,OAAO,CAAU,MAAuB,EAAE,GAAG,aAAoB;IAC/E,OAAO,CAAC,GAAG,IAAW,EAAE,EAAE;QACxB,IAAI,eAAe,CAAC,IAAI,CAAC,KAAK,cAAc,CAAC,UAAU,EAAE,CAAC;YACxD,aAAa,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE;gBACrB,GAAG,EAAE;oBACH;wBACE,MAAM;wBACN,EAAE,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;wBAClC,OAAO,EAAE,aAAa,CAAC,CAAC,CAAC;qBAC1B;iBACF;aACF,CAAC,CAAC;QACL,CAAC;aAAM,CAAC;YACN,aAAa,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE;gBACrB,GAAG,EAAE;oBACH;wBACE,MAAM;wBACN,EAAE,EAAE,aAAa,CAAC,CAAC,CAAC;wBACpB,OAAO,EAAE,aAAa,CAAC,CAAC,CAAC;qBAC1B;iBACF;aACF,CAAC,CAAC;QACL,CAAC;IACH,CAAC,CAAC;AACJ,CAAC"}
@@ -24,9 +24,10 @@ export function buildPreHook(fn) {
24
24
  ? function () {
25
25
  return fn(this);
26
26
  }
27
- : function (next) {
28
- return fn(this, next);
29
- };
27
+ : // we need to explicitly gives args to avoid a bug with mongoose
28
+ function (next, arg1, arg2) {
29
+ return fn(this, next, arg1, arg2);
30
+ };
30
31
  }
31
32
  /**
32
33
  * @ignore
@@ -1 +1 @@
1
- {"version":3,"file":"schemaOptions.js","sourceRoot":"","sources":["../../../src/utils/schemaOptions.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,SAAS,EAAE,KAAK,EAAC,MAAM,YAAY,CAAC;AAG5C,OAAO,EAAC,uBAAuB,EAAC,MAAM,2BAA2B,CAAC;AAUlE;;GAEG;AACH,MAAM,UAAU,aAAa,CAAC,MAAW,EAAE,OAA+B;IACxE,MAAM,KAAK,GAAG,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IAEjC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,uBAAuB,CAAC,EAAE,CAAC;QACxC,KAAK,CAAC,GAAG,CAAC,uBAAuB,EAAE,EAAE,CAAC,CAAC;IACzC,CAAC;IAED,IAAI,OAAO,EAAE,CAAC;QACZ,KAAK,CAAC,GAAG,CAAC,uBAAuB,EAAE,SAAS,CAAC,KAAK,CAAC,GAAG,CAAC,uBAAuB,CAAC,EAAE,OAAO,CAAC,CAAC,CAAC;IAC7F,CAAC;IAED,OAAO,KAAK,CAAC,GAAG,CAAC,uBAAuB,CAAC,CAAC;AAC5C,CAAC;AAED,SAAS,WAAW,CAAC,IAAwC;IAC3D,OAAO,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,OAAO,IAAI,IAAI,CAAC,EAAE,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,MAAM,CAAC,OAAO,CAAQ,CAAC;AAC3G,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,YAAY,CAAC,EAAqB;IAChD,OAAO,EAAE,CAAC,MAAM,KAAK,CAAC;QACpB,CAAC,CAAC;YACE,OAAQ,EAA2B,CAAC,IAAI,CAAC,CAAC;QAC5C,CAAC;QACH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,CAAC,MAAc,EAAE,OAA8B;IAC/E,IAAI,OAAO,CAAC,OAAO,EAAE,CAAC;QACpB,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC;IAC9E,CAAC;IAED,IAAI,OAAO,CAAC,OAAO,EAAE,CAAC;QACpB,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC;IAC7E,CAAC;IAED,IAAI,OAAO,CAAC,GAAG,EAAE,CAAC;QAChB,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE;YAC3B,IAAI,GAAG;gBACL,GAAG,IAAI;gBACP,EAAE,EAAE,YAAY,CAAC,IAAI,CAAC,EAAE,CAAC;aAC1B,CAAC;YAED,MAAM,CAAC,GAAW,CAAC,GAAG,WAAW,CAAC,IAAI,CAAC,CAAC,CAAC;QAC5C,CAAC,CAAC,CAAC;IACL,CAAC;IAED,IAAI,OAAO,CAAC,IAAI,EAAE,CAAC;QACjB,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE,CAAE,MAAM,CAAC,IAAY,CAAC,GAAG,WAAW,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAC7E,CAAC;AACH,CAAC"}
@@ -1,5 +1,5 @@
1
1
  import { StaticMethodDecorator } from "@tsed/core";
2
- import { MongooseHookOptions, MongoosePostHookCB } from "../interfaces/MongooseSchemaOptions.js";
2
+ import { MongooseHookOptions, MongooseMethods, MongoosePostHookCB } from "../interfaces/MongooseSchemaOptions.js";
3
3
  /**
4
4
  * We can simply attach a `@PostHook` decorator to your model class and
5
5
  * define the hook function like you normally would in Mongoose.
@@ -43,6 +43,6 @@ import { MongooseHookOptions, MongoosePostHookCB } from "../interfaces/MongooseS
43
43
  * @mongoose
44
44
  * @class
45
45
  */
46
- export declare function PostHook<T = any>(method: string, fn: MongoosePostHookCB<T>): ClassDecorator;
47
- export declare function PostHook<T = any>(method: string, fn: MongoosePostHookCB<T>, options: MongooseHookOptions): ClassDecorator;
48
- export declare function PostHook<T = any>(method: string, options: MongooseHookOptions): StaticMethodDecorator;
46
+ export declare function PostHook<T = any>(method: MongooseMethods, fn: MongoosePostHookCB<T>): ClassDecorator;
47
+ export declare function PostHook<T = any>(method: MongooseMethods, fn: MongoosePostHookCB<T>, options: MongooseHookOptions): ClassDecorator;
48
+ export declare function PostHook<T = any>(method: MongooseMethods, options: MongooseHookOptions): StaticMethodDecorator;
@@ -1,4 +1,5 @@
1
- import { MongooseHookOptions, MongoosePreHookCB } from "../interfaces/MongooseSchemaOptions.js";
1
+ import { StaticMethodDecorator } from "@tsed/core";
2
+ import type { MongooseHookOptions, MongooseMethods, MongoosePreHookCB } from "../interfaces/MongooseSchemaOptions.js";
2
3
  /**
3
4
  *
4
5
  * We can simply attach a `@PreHook` decorator to your model class and
@@ -39,11 +40,12 @@ import { MongooseHookOptions, MongoosePreHookCB } from "../interfaces/MongooseSc
39
40
  *
40
41
  * This will execute the pre-save hook each time a `CarModel` document is saved.
41
42
  *
42
- * @param {string} method
43
- * @param fn
44
- * @param options
45
43
  * @returns {Function}
46
44
  * @decorator
47
45
  * @class
46
+ * @param method
47
+ * @param fn
48
+ * @param options
48
49
  */
49
- export declare function PreHook<T = any>(method: string, fn?: MongoosePreHookCB<T> | MongooseHookOptions, options?: MongooseHookOptions): Function;
50
+ export declare function PreHook<T = any>(method: MongooseMethods, fn: MongoosePreHookCB<T>, options?: MongooseHookOptions): ClassDecorator;
51
+ export declare function PreHook<T = any>(method: MongooseMethods, options?: MongooseHookOptions): StaticMethodDecorator;
@@ -25,22 +25,20 @@
25
25
  /// <reference types="mongoose/types/inferrawdoctype.js" />
26
26
  import { type IndexOptions, Schema, SchemaOptions } from "mongoose";
27
27
  import { MongooseDocument } from "./MongooseDocument.js";
28
+ export type MongooseMethod = "aggregate" | "bulkWrite" | "createCollection" | "save" | "insertMany" | "estimatedDocumentCount" | "countDocuments" | "deleteMany" | "distinct" | "find" | "findOne" | "findOneAndDelete" | "findOneAndReplace" | "findOneAndUpdate" | "replaceOne" | "updateMany" | "init" | "validate";
29
+ export type MongooseMethods = MongooseMethod | RegExp | MongooseMethod[];
28
30
  export type MongooseNextCB = (err?: Error) => void;
29
- export interface MongooseHookOptions {
30
- document?: boolean;
31
- query?: boolean;
32
- parallel?: boolean;
33
- }
34
- export type MongooseHookPromised<T = any> = (doc: T | MongooseDocument<T>) => Promise<void> | void;
35
- export type MongoosePreHookCB<T = any> = ((doc: T | MongooseDocument<T>, next: MongooseNextCB) => void) | MongooseHookPromised;
36
- export type MongoosePostHookCB<T = any> = ((doc: T | MongooseDocument<T>, error: Error, next: MongooseNextCB) => void) | ((doc: T | MongooseDocument<T>, error: Error) => Promise<void> | void) | ((doc: T | MongooseDocument<T>, next: MongooseNextCB) => void) | MongooseHookPromised;
31
+ export type MongooseHookOptions = Record<string, unknown>;
32
+ export type MongooseHookPromised<T = any> = (doc: T | MongooseDocument<T>) => Promise<void>;
33
+ export type MongoosePreHookCB<T = any> = (doc: T | MongooseDocument<T>, ...args: unknown[]) => Promise<void> | void;
34
+ export type MongoosePostHookCB<T = any> = (doc: T | MongooseDocument<T>, ...args: unknown[]) => Promise<void> | void;
37
35
  export interface MongoosePreHook<T = any> {
38
- method: string | RegExp;
36
+ method: MongooseMethods;
39
37
  fn: MongoosePreHookCB<T>;
40
38
  options?: MongooseHookOptions;
41
39
  }
42
40
  export interface MongoosePostHook<T = any> {
43
- method: string | RegExp;
41
+ method: MongooseMethods;
44
42
  fn: MongoosePostHookCB<T>;
45
43
  options?: MongooseHookOptions;
46
44
  }
@@ -24,7 +24,7 @@
24
24
  /// <reference types="mongoose/types/inferschematype.js" />
25
25
  /// <reference types="mongoose/types/inferrawdoctype.js" />
26
26
  import { Schema } from "mongoose";
27
- import { MongooseNextCB, MongoosePreHookCB, MongooseSchemaOptions } from "../interfaces/MongooseSchemaOptions.js";
27
+ import { MongoosePreHookCB, MongooseSchemaOptions } from "../interfaces/MongooseSchemaOptions.js";
28
28
  /**
29
29
  * @ignore
30
30
  */
@@ -32,7 +32,7 @@ export declare function schemaOptions(target: any, options?: MongooseSchemaOptio
32
32
  /**
33
33
  * @ignore
34
34
  */
35
- export declare function buildPreHook(fn: MongoosePreHookCB): (next: MongooseNextCB) => void | Promise<void>;
35
+ export declare function buildPreHook(fn: MongoosePreHookCB): (next: Function, arg1: unknown, arg2: unknown) => any;
36
36
  /**
37
37
  * @ignore
38
38
  */
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@tsed/mongoose",
3
3
  "description": "Mongoose package for Ts.ED framework",
4
4
  "type": "module",
5
- "version": "8.4.0",
5
+ "version": "8.4.2",
6
6
  "keywords": [
7
7
  "tsed",
8
8
  "mongoose",
@@ -16,7 +16,7 @@
16
16
  "typings": "./lib/types/index.d.ts",
17
17
  "exports": {
18
18
  ".": {
19
- "@tsed/source": "./src/index.ts",
19
+ "tsed-source": "./src/index.ts",
20
20
  "types": "./lib/types/index.d.ts",
21
21
  "import": "./lib/esm/index.js",
22
22
  "default": "./lib/esm/index.js"
@@ -34,27 +34,27 @@
34
34
  "tslib": "2.7.0"
35
35
  },
36
36
  "devDependencies": {
37
- "@tsed/ajv": "8.4.0",
38
- "@tsed/barrels": "8.4.0",
39
- "@tsed/core": "8.4.0",
40
- "@tsed/di": "8.4.0",
41
- "@tsed/json-mapper": "8.4.0",
37
+ "@tsed/ajv": "8.4.2",
38
+ "@tsed/barrels": "8.4.2",
39
+ "@tsed/core": "8.4.2",
40
+ "@tsed/di": "8.4.2",
41
+ "@tsed/json-mapper": "8.4.2",
42
42
  "@tsed/logger": "^7.0.1",
43
- "@tsed/schema": "8.4.0",
44
- "@tsed/testcontainers-mongo": "8.4.0",
45
- "@tsed/typescript": "8.4.0",
43
+ "@tsed/schema": "8.4.2",
44
+ "@tsed/testcontainers-mongo": "8.4.2",
45
+ "@tsed/typescript": "8.4.2",
46
46
  "eslint": "9.12.0",
47
- "mongoose": "8.7.0",
47
+ "mongoose": "8.9.4",
48
48
  "typescript": "5.4.5",
49
49
  "vitest": "2.1.2"
50
50
  },
51
51
  "peerDependencies": {
52
- "@tsed/ajv": "8.4.0",
53
- "@tsed/core": "8.4.0",
54
- "@tsed/di": "8.4.0",
55
- "@tsed/json-mapper": "8.4.0",
52
+ "@tsed/ajv": ">=8.4.2",
53
+ "@tsed/core": ">=8.4.2",
54
+ "@tsed/di": ">=8.4.2",
55
+ "@tsed/json-mapper": ">=8.4.2",
56
56
  "@tsed/logger": ">=7.0.1",
57
- "@tsed/schema": "8.4.0",
57
+ "@tsed/schema": ">=8.4.2",
58
58
  "mongoose": ">=6.0.0"
59
59
  },
60
60
  "repository": "https://github.com/tsedio/tsed",
@@ -8,7 +8,7 @@ describe("@PostHook()", () => {
8
8
  const fn = vi.fn();
9
9
 
10
10
  // WHEN
11
- @PostHook("method", fn)
11
+ @PostHook("save", fn)
12
12
  class Test {}
13
13
 
14
14
  // THEN
@@ -17,7 +17,7 @@ describe("@PostHook()", () => {
17
17
  expect(options).toEqual({
18
18
  post: [
19
19
  {
20
- method: "method",
20
+ method: "save",
21
21
  fn,
22
22
  options: undefined
23
23
  }
@@ -1,6 +1,6 @@
1
1
  import {decoratorTypeOf, DecoratorTypes, StaticMethodDecorator} from "@tsed/core";
2
2
 
3
- import {MongooseHookOptions, MongoosePostHookCB} from "../interfaces/MongooseSchemaOptions.js";
3
+ import {MongooseHookOptions, MongooseMethods, MongoosePostHookCB} from "../interfaces/MongooseSchemaOptions.js";
4
4
  import {schemaOptions} from "../utils/schemaOptions.js";
5
5
 
6
6
  /**
@@ -46,10 +46,10 @@ import {schemaOptions} from "../utils/schemaOptions.js";
46
46
  * @mongoose
47
47
  * @class
48
48
  */
49
- export function PostHook<T = any>(method: string, fn: MongoosePostHookCB<T>): ClassDecorator;
50
- export function PostHook<T = any>(method: string, fn: MongoosePostHookCB<T>, options: MongooseHookOptions): ClassDecorator;
51
- export function PostHook<T = any>(method: string, options: MongooseHookOptions): StaticMethodDecorator;
52
- export function PostHook<T = any>(method: string, ...params: any[]): Function {
49
+ export function PostHook<T = any>(method: MongooseMethods, fn: MongoosePostHookCB<T>): ClassDecorator;
50
+ export function PostHook<T = any>(method: MongooseMethods, fn: MongoosePostHookCB<T>, options: MongooseHookOptions): ClassDecorator;
51
+ export function PostHook<T = any>(method: MongooseMethods, options: MongooseHookOptions): StaticMethodDecorator;
52
+ export function PostHook<T = any>(method: MongooseMethods, ...params: any[]): ClassDecorator | StaticMethodDecorator {
53
53
  return ((...args: any[]) => {
54
54
  let options: MongooseHookOptions = params[1];
55
55
  let fn: MongoosePostHookCB<T> = params[0];
@@ -7,7 +7,7 @@ describe("@PreHook()", () => {
7
7
  const fn = vi.fn();
8
8
 
9
9
  // WHEN
10
- @PreHook("method", fn, {query: true})
10
+ @PreHook("save", fn as any, {query: true})
11
11
  class Test {}
12
12
 
13
13
  // THEN
@@ -16,7 +16,7 @@ describe("@PreHook()", () => {
16
16
  expect(options).toEqual({
17
17
  pre: [
18
18
  {
19
- method: "method",
19
+ method: "save",
20
20
  fn,
21
21
  options: {
22
22
  query: true
@@ -32,7 +32,7 @@ describe("@PreHook()", () => {
32
32
  });
33
33
 
34
34
  // WHEN
35
- @PreHook("method", fn, {query: true})
35
+ @PreHook("save", fn, {query: true})
36
36
  class Test {}
37
37
 
38
38
  // THEN
@@ -41,7 +41,7 @@ describe("@PreHook()", () => {
41
41
  expect(options).toEqual({
42
42
  pre: [
43
43
  {
44
- method: "method",
44
+ method: "save",
45
45
  fn,
46
46
  options: {
47
47
  query: true
@@ -1,6 +1,6 @@
1
- import {decoratorTypeOf, DecoratorTypes} from "@tsed/core";
1
+ import {decoratorTypeOf, DecoratorTypes, StaticMethodDecorator} from "@tsed/core";
2
2
 
3
- import {MongooseHookOptions, MongoosePreHookCB} from "../interfaces/MongooseSchemaOptions.js";
3
+ import type {MongooseHookOptions, MongooseMethods, MongoosePreHookCB} from "../interfaces/MongooseSchemaOptions.js";
4
4
  import {schemaOptions} from "../utils/schemaOptions.js";
5
5
 
6
6
  /**
@@ -43,28 +43,37 @@ import {schemaOptions} from "../utils/schemaOptions.js";
43
43
  *
44
44
  * This will execute the pre-save hook each time a `CarModel` document is saved.
45
45
  *
46
- * @param {string} method
47
- * @param fn
48
- * @param options
49
46
  * @returns {Function}
50
47
  * @decorator
51
48
  * @class
49
+ * @param method
50
+ * @param fn
51
+ * @param options
52
52
  */
53
- export function PreHook<T = any>(method: string, fn?: MongoosePreHookCB<T> | MongooseHookOptions, options?: MongooseHookOptions): Function {
53
+ export function PreHook<T = any>(method: MongooseMethods, fn: MongoosePreHookCB<T>, options?: MongooseHookOptions): ClassDecorator;
54
+ export function PreHook<T = any>(method: MongooseMethods, options?: MongooseHookOptions): StaticMethodDecorator;
55
+ export function PreHook<T = any>(method: MongooseMethods, ...decoratorArgs: any[]): ClassDecorator | StaticMethodDecorator {
54
56
  return (...args: any[]) => {
55
57
  if (decoratorTypeOf(args) === DecoratorTypes.METHOD_STC) {
56
- options = fn as MongooseHookOptions;
57
- fn = args[0][args[1]].bind(args[0]);
58
+ schemaOptions(args[0], {
59
+ pre: [
60
+ {
61
+ method,
62
+ fn: args[0][args[1]].bind(args[0]),
63
+ options: decoratorArgs[0]
64
+ }
65
+ ]
66
+ });
67
+ } else {
68
+ schemaOptions(args[0], {
69
+ pre: [
70
+ {
71
+ method,
72
+ fn: decoratorArgs[0],
73
+ options: decoratorArgs[1]
74
+ }
75
+ ]
76
+ });
58
77
  }
59
-
60
- schemaOptions(args[0], {
61
- pre: [
62
- {
63
- method,
64
- fn: fn as MongoosePreHookCB<T>,
65
- options
66
- }
67
- ]
68
- });
69
78
  };
70
79
  }
@@ -2,32 +2,42 @@ import {type IndexOptions, Schema, SchemaOptions} from "mongoose";
2
2
 
3
3
  import {MongooseDocument} from "./MongooseDocument.js";
4
4
 
5
- export type MongooseNextCB = (err?: Error) => void;
6
-
7
- export interface MongooseHookOptions {
8
- document?: boolean;
9
- query?: boolean;
10
- parallel?: boolean;
11
- }
5
+ export type MongooseMethod =
6
+ | "aggregate"
7
+ | "bulkWrite"
8
+ | "createCollection"
9
+ | "save"
10
+ | "insertMany"
11
+ | "estimatedDocumentCount"
12
+ | "countDocuments"
13
+ | "deleteMany"
14
+ | "distinct"
15
+ | "find"
16
+ | "findOne"
17
+ | "findOneAndDelete"
18
+ | "findOneAndReplace"
19
+ | "findOneAndUpdate"
20
+ | "replaceOne"
21
+ | "updateMany"
22
+ | "init"
23
+ | "validate";
24
+
25
+ export type MongooseMethods = MongooseMethod | RegExp | MongooseMethod[];
12
26
 
13
- export type MongooseHookPromised<T = any> = (doc: T | MongooseDocument<T>) => Promise<void> | void;
14
-
15
- export type MongoosePreHookCB<T = any> = ((doc: T | MongooseDocument<T>, next: MongooseNextCB) => void) | MongooseHookPromised;
16
-
17
- export type MongoosePostHookCB<T = any> =
18
- | ((doc: T | MongooseDocument<T>, error: Error, next: MongooseNextCB) => void)
19
- | ((doc: T | MongooseDocument<T>, error: Error) => Promise<void> | void)
20
- | ((doc: T | MongooseDocument<T>, next: MongooseNextCB) => void)
21
- | MongooseHookPromised;
27
+ export type MongooseNextCB = (err?: Error) => void;
28
+ export type MongooseHookOptions = Record<string, unknown>;
29
+ export type MongooseHookPromised<T = any> = (doc: T | MongooseDocument<T>) => Promise<void>;
30
+ export type MongoosePreHookCB<T = any> = (doc: T | MongooseDocument<T>, ...args: unknown[]) => Promise<void> | void;
31
+ export type MongoosePostHookCB<T = any> = (doc: T | MongooseDocument<T>, ...args: unknown[]) => Promise<void> | void;
22
32
 
23
33
  export interface MongoosePreHook<T = any> {
24
- method: string | RegExp;
34
+ method: MongooseMethods;
25
35
  fn: MongoosePreHookCB<T>;
26
36
  options?: MongooseHookOptions;
27
37
  }
28
38
 
29
39
  export interface MongoosePostHook<T = any> {
30
- method: string | RegExp;
40
+ method: MongooseMethods;
31
41
  fn: MongoosePostHookCB<T>;
32
42
  options?: MongooseHookOptions;
33
43
  }
@@ -4,7 +4,6 @@ import {Schema} from "mongoose";
4
4
  import {MONGOOSE_SCHEMA_OPTIONS} from "../constants/constants.js";
5
5
  import {
6
6
  MongooseHookPromised,
7
- MongooseNextCB,
8
7
  MongoosePostHook,
9
8
  MongoosePreHook,
10
9
  MongoosePreHookCB,
@@ -40,8 +39,9 @@ export function buildPreHook(fn: MongoosePreHookCB) {
40
39
  ? function () {
41
40
  return (fn as MongooseHookPromised)(this);
42
41
  }
43
- : function (next: MongooseNextCB) {
44
- return fn(this, next);
42
+ : // we need to explicitly gives args to avoid a bug with mongoose
43
+ function (next: Function, arg1: unknown, arg2: unknown) {
44
+ return (fn as any)(this, next, arg1, arg2);
45
45
  };
46
46
  }
47
47