@kikiutils/mongoose 3.1.0 → 3.1.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.
Files changed (60) hide show
  1. package/README.md +14 -13
  2. package/dist/options.cjs.map +1 -1
  3. package/dist/options.mjs.map +1 -1
  4. package/dist/plugins/normalize.cjs.map +1 -1
  5. package/dist/plugins/normalize.mjs.map +1 -1
  6. package/dist/schema-builders/base.cjs.map +1 -1
  7. package/dist/schema-builders/base.mjs.map +1 -1
  8. package/dist/schema-builders/decimal128.cjs.map +1 -1
  9. package/dist/schema-builders/decimal128.d.ts +8 -8
  10. package/dist/schema-builders/decimal128.d.ts.map +1 -1
  11. package/dist/schema-builders/decimal128.mjs.map +1 -1
  12. package/dist/schema-builders/number.cjs.map +1 -1
  13. package/dist/schema-builders/number.d.ts.map +1 -1
  14. package/dist/schema-builders/number.mjs.map +1 -1
  15. package/dist/schema-builders/string.cjs +2 -2
  16. package/dist/schema-builders/string.cjs.map +1 -1
  17. package/dist/schema-builders/string.d.ts +2 -2
  18. package/dist/schema-builders/string.d.ts.map +1 -1
  19. package/dist/schema-builders/string.mjs +2 -2
  20. package/dist/schema-builders/string.mjs.map +1 -1
  21. package/dist/types/aggregate-paginate.cjs +14 -0
  22. package/dist/types/aggregate-paginate.cjs.map +1 -0
  23. package/dist/types/aggregate-paginate.d.ts +3 -3
  24. package/dist/types/aggregate-paginate.d.ts.map +1 -1
  25. package/dist/types/aggregate-paginate.mjs +10 -0
  26. package/dist/types/aggregate-paginate.mjs.map +1 -0
  27. package/dist/types/data.cjs +3 -0
  28. package/dist/types/data.cjs.map +1 -0
  29. package/dist/types/data.mjs +2 -0
  30. package/dist/types/data.mjs.map +1 -0
  31. package/dist/types/index.cjs +3 -0
  32. package/dist/types/index.cjs.map +1 -0
  33. package/dist/types/index.mjs +2 -0
  34. package/dist/types/index.mjs.map +1 -0
  35. package/dist/types/options.cjs +3 -0
  36. package/dist/types/options.cjs.map +1 -0
  37. package/dist/types/options.mjs +2 -0
  38. package/dist/types/options.mjs.map +1 -0
  39. package/dist/types/paginate.cjs +8 -0
  40. package/dist/types/paginate.cjs.map +1 -0
  41. package/dist/types/paginate.d.ts +26 -26
  42. package/dist/types/paginate.d.ts.map +1 -1
  43. package/dist/types/paginate.mjs +4 -0
  44. package/dist/types/paginate.mjs.map +1 -0
  45. package/dist/types/utils.cjs +3 -0
  46. package/dist/types/utils.cjs.map +1 -0
  47. package/dist/types/utils.mjs +2 -0
  48. package/dist/types/utils.mjs.map +1 -0
  49. package/dist/utils.cjs +2 -2
  50. package/dist/utils.cjs.map +1 -1
  51. package/dist/utils.mjs +2 -2
  52. package/dist/utils.mjs.map +1 -1
  53. package/package.json +29 -21
  54. package/src/plugins/normalize.ts +2 -2
  55. package/src/schema-builders/decimal128.ts +8 -8
  56. package/src/schema-builders/number.ts +4 -3
  57. package/src/schema-builders/string.ts +2 -2
  58. package/src/types/aggregate-paginate.ts +12 -12
  59. package/src/types/paginate.ts +62 -62
  60. package/src/utils.ts +2 -2
@@ -0,0 +1 @@
1
+ {"version":3,"file":"options.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":""}
@@ -0,0 +1,8 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ const _$1 = _;
6
+
7
+ exports.default = _$1;
8
+ //# sourceMappingURL=paginate.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"paginate.cjs","sources":["../../src/types/paginate.ts"],"sourcesContent":["import type {\n FilterQuery,\n mongo,\n PaginateOptions,\n Schema,\n} from 'mongoose';\n\nimport type { IfElse } from './utils';\n\ndeclare module 'mongoose' {\n type PaginateDocument<\n T,\n TMethods,\n TQueryHelpers,\n O extends PaginateOptions = object,\n > = IfElse<\n O['lean'],\n IfElse<\n O['leanWithId'],\n T & { id: string },\n IfElse<\n O['leanWithVirtuals'],\n T & { [key: string]: any },\n T\n >\n >,\n HydratedDocument<T, TMethods, TQueryHelpers>\n >;\n\n interface PaginateCustomLabels<T = boolean | string | undefined> {\n docs?: T;\n hasNextPage?: T;\n hasPrevPage?: T;\n limit?: T;\n meta?: T;\n nextPage?: T;\n page?: T;\n pagingCounter?: T;\n prevPage?: T;\n totalDocs?: T;\n totalPages?: T;\n }\n\n interface PaginateModel<T, TQueryHelpers = object, TMethods = object> extends Model<T, TQueryHelpers, TMethods> {\n paginate: {\n <O extends PaginateOptions>(\n query?: FilterQuery<T>,\n options?: O,\n callback?: (err: any, result: PaginateResult<PaginateDocument<T, TMethods, TQueryHelpers, O>>) => void\n ): Promise<PaginateResult<PaginateDocument<T, TMethods, TQueryHelpers, O>>>;\n\n <UserType = T, O extends PaginateOptions = PaginateOptions>(\n query?: FilterQuery<T>,\n options?: O,\n callback?: (\n err: any,\n result: PaginateResult<PaginateDocument<UserType, TMethods, TQueryHelpers, O>>\n ) => void\n ): Promise<PaginateResult<PaginateDocument<UserType, TMethods, TQueryHelpers, O>>>;\n\n <UserType = T>(\n query?: FilterQuery<T>,\n options?: PaginateOptions,\n callback?: (\n err: any,\n result: PaginateResult<PaginateDocument<UserType, TMethods, TQueryHelpers, PaginateOptions>>\n ) => void\n ): Promise<PaginateResult<PaginateDocument<UserType, TMethods, TQueryHelpers, PaginateOptions>>>;\n };\n }\n\n interface PaginateOptions {\n allowDiskUse?: boolean;\n collation?: mongo.CollationOptions;\n customLabels?: PaginateCustomLabels;\n forceCountFn?: boolean;\n lean?: boolean;\n leanWithId?: boolean;\n leanWithVirtuals?: boolean;\n limit?: number;\n offset?: number;\n options?: QueryOptions;\n page?: number;\n\n /**\n * If pagination is set to `false`, it will return all docs without adding limit condition.\n *\n * @default true\n */\n pagination?: boolean;\n populate?: PopulateOptions | PopulateOptions[] | string | string[];\n projection?: any;\n read?: PaginateReadOptions;\n select?: object | string;\n sort?: object | string;\n useCustomCountFn?: () => Promise<number>;\n useEstimatedCount?: boolean;\n }\n\n interface PaginateReadOptions {\n pref: string;\n tags?: any[];\n }\n\n interface PaginateResult<T> {\n [customLabel: string]: boolean | null | number | T[] | undefined;\n docs: T[];\n hasNextPage: boolean;\n hasPrevPage: boolean;\n limit: number;\n meta?: any;\n nextPage?: null | number;\n offset: number;\n page?: number;\n pagingCounter: number;\n prevPage?: null | number;\n totalDocs: number;\n totalPages: number;\n }\n\n // @ts-expect-error Ignore this error.\n interface Query<\n ResultType,\n DocType,\n THelpers = NonNullable<unknown>,\n RawDocType = DocType,\n _QueryOp = 'find',\n TInstanceMethods = Record<string, never>,\n > {\n paginate: {\n <O extends PaginateOptions>(options?: O): Promise<\n PaginateResult<PaginateDocument<RawDocType, TInstanceMethods, THelpers, O>>\n >;\n\n <UserType = ResultType, O extends PaginateOptions = PaginateOptions>(options?: O): Promise<\n PaginateResult<PaginateDocument<UserType, TInstanceMethods, THelpers, O>>\n >;\n\n <UserType = ResultType>(options?: PaginateOptions): Promise<\n PaginateResult<PaginateDocument<UserType, TInstanceMethods, THelpers, PaginateOptions>>\n >;\n };\n }\n\n interface SubDocumentPagingOptions {\n limit?: number;\n page?: number;\n populate?: PopulateOptions | PopulateOptions[] | string | string[];\n }\n\n interface SubPaginateOptions {\n pagination?: boolean;\n pagingOptions?: SubDocumentPagingOptions;\n populate?: PopulateOptions | PopulateOptions[] | string | string[];\n read?: PaginateReadOptions;\n select?: object | string;\n }\n}\n\ndeclare function _(schema: Schema): void;\n\n// eslint-disable-next-line ts/no-namespace\ndeclare namespace _ {\n const paginate: { options: PaginateOptions };\n const paginateSubDocs: { options: PaginateOptions };\n\n class PaginationParameters<T, O extends PaginateOptions> {\n constructor(request: { query?: Record<string, any> });\n get: () => [FilterQuery<T>, O];\n getOptions: () => O;\n getQuery: () => FilterQuery<T>;\n }\n}\n\nexport default _;\n"],"names":[],"mappings":";;;;AA8KA,YAAe,CAAC;;;;"}
@@ -1,6 +1,11 @@
1
1
  import type { FilterQuery, mongo, PaginateOptions, Schema } from 'mongoose';
2
2
  import type { IfElse } from './utils';
3
3
  declare module 'mongoose' {
4
+ type PaginateDocument<T, TMethods, TQueryHelpers, O extends PaginateOptions = object> = IfElse<O['lean'], IfElse<O['leanWithId'], T & {
5
+ id: string;
6
+ }, IfElse<O['leanWithVirtuals'], T & {
7
+ [key: string]: any;
8
+ }, T>>, HydratedDocument<T, TMethods, TQueryHelpers>>;
4
9
  interface PaginateCustomLabels<T = boolean | string | undefined> {
5
10
  docs?: T;
6
11
  hasNextPage?: T;
@@ -14,9 +19,12 @@ declare module 'mongoose' {
14
19
  totalDocs?: T;
15
20
  totalPages?: T;
16
21
  }
17
- interface PaginateReadOptions {
18
- pref: string;
19
- tags?: any[];
22
+ interface PaginateModel<T, TQueryHelpers = object, TMethods = object> extends Model<T, TQueryHelpers, TMethods> {
23
+ paginate: {
24
+ <O extends PaginateOptions>(query?: FilterQuery<T>, options?: O, callback?: (err: any, result: PaginateResult<PaginateDocument<T, TMethods, TQueryHelpers, O>>) => void): Promise<PaginateResult<PaginateDocument<T, TMethods, TQueryHelpers, O>>>;
25
+ <UserType = T, O extends PaginateOptions = PaginateOptions>(query?: FilterQuery<T>, options?: O, callback?: (err: any, result: PaginateResult<PaginateDocument<UserType, TMethods, TQueryHelpers, O>>) => void): Promise<PaginateResult<PaginateDocument<UserType, TMethods, TQueryHelpers, O>>>;
26
+ <UserType = T>(query?: FilterQuery<T>, options?: PaginateOptions, callback?: (err: any, result: PaginateResult<PaginateDocument<UserType, TMethods, TQueryHelpers, PaginateOptions>>) => void): Promise<PaginateResult<PaginateDocument<UserType, TMethods, TQueryHelpers, PaginateOptions>>>;
27
+ };
20
28
  }
21
29
  interface PaginateOptions {
22
30
  allowDiskUse?: boolean;
@@ -44,17 +52,9 @@ declare module 'mongoose' {
44
52
  useCustomCountFn?: () => Promise<number>;
45
53
  useEstimatedCount?: boolean;
46
54
  }
47
- interface SubPaginateOptions {
48
- pagination?: boolean;
49
- pagingOptions?: SubDocumentPagingOptions;
50
- populate?: PopulateOptions | PopulateOptions[] | string | string[];
51
- read?: PaginateReadOptions;
52
- select?: object | string;
53
- }
54
- interface SubDocumentPagingOptions {
55
- limit?: number;
56
- page?: number;
57
- populate?: PopulateOptions | PopulateOptions[] | string | string[];
55
+ interface PaginateReadOptions {
56
+ pref: string;
57
+ tags?: any[];
58
58
  }
59
59
  interface PaginateResult<T> {
60
60
  [customLabel: string]: boolean | null | number | T[] | undefined;
@@ -71,18 +71,6 @@ declare module 'mongoose' {
71
71
  totalDocs: number;
72
72
  totalPages: number;
73
73
  }
74
- type PaginateDocument<T, TMethods, TQueryHelpers, O extends PaginateOptions = object> = IfElse<O['lean'], IfElse<O['leanWithId'], T & {
75
- id: string;
76
- }, IfElse<O['leanWithVirtuals'], T & {
77
- [key: string]: any;
78
- }, T>>, HydratedDocument<T, TMethods, TQueryHelpers>>;
79
- interface PaginateModel<T, TQueryHelpers = object, TMethods = object> extends Model<T, TQueryHelpers, TMethods> {
80
- paginate: {
81
- <O extends PaginateOptions>(query?: FilterQuery<T>, options?: O, callback?: (err: any, result: PaginateResult<PaginateDocument<T, TMethods, TQueryHelpers, O>>) => void): Promise<PaginateResult<PaginateDocument<T, TMethods, TQueryHelpers, O>>>;
82
- <UserType = T, O extends PaginateOptions = PaginateOptions>(query?: FilterQuery<T>, options?: O, callback?: (err: any, result: PaginateResult<PaginateDocument<UserType, TMethods, TQueryHelpers, O>>) => void): Promise<PaginateResult<PaginateDocument<UserType, TMethods, TQueryHelpers, O>>>;
83
- <UserType = T>(query?: FilterQuery<T>, options?: PaginateOptions, callback?: (err: any, result: PaginateResult<PaginateDocument<UserType, TMethods, TQueryHelpers, PaginateOptions>>) => void): Promise<PaginateResult<PaginateDocument<UserType, TMethods, TQueryHelpers, PaginateOptions>>>;
84
- };
85
- }
86
74
  interface Query<ResultType, DocType, THelpers = NonNullable<unknown>, RawDocType = DocType, _QueryOp = 'find', TInstanceMethods = Record<string, never>> {
87
75
  paginate: {
88
76
  <O extends PaginateOptions>(options?: O): Promise<PaginateResult<PaginateDocument<RawDocType, TInstanceMethods, THelpers, O>>>;
@@ -90,6 +78,18 @@ declare module 'mongoose' {
90
78
  <UserType = ResultType>(options?: PaginateOptions): Promise<PaginateResult<PaginateDocument<UserType, TInstanceMethods, THelpers, PaginateOptions>>>;
91
79
  };
92
80
  }
81
+ interface SubDocumentPagingOptions {
82
+ limit?: number;
83
+ page?: number;
84
+ populate?: PopulateOptions | PopulateOptions[] | string | string[];
85
+ }
86
+ interface SubPaginateOptions {
87
+ pagination?: boolean;
88
+ pagingOptions?: SubDocumentPagingOptions;
89
+ populate?: PopulateOptions | PopulateOptions[] | string | string[];
90
+ read?: PaginateReadOptions;
91
+ select?: object | string;
92
+ }
93
93
  }
94
94
  declare function _(schema: Schema): void;
95
95
  declare namespace _ {
@@ -1 +1 @@
1
- {"version":3,"file":"paginate.d.ts","sourceRoot":"","sources":["../../src/types/paginate.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACR,WAAW,EACX,KAAK,EACL,eAAe,EACf,MAAM,EACT,MAAM,UAAU,CAAC;AAElB,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAEtC,OAAO,QAAQ,UAAU,CAAC;IACtB,UAAU,oBAAoB,CAAC,CAAC,GAAG,OAAO,GAAG,MAAM,GAAG,SAAS;QAC3D,IAAI,CAAC,EAAE,CAAC,CAAC;QACT,WAAW,CAAC,EAAE,CAAC,CAAC;QAChB,WAAW,CAAC,EAAE,CAAC,CAAC;QAChB,KAAK,CAAC,EAAE,CAAC,CAAC;QACV,IAAI,CAAC,EAAE,CAAC,CAAC;QACT,QAAQ,CAAC,EAAE,CAAC,CAAC;QACb,IAAI,CAAC,EAAE,CAAC,CAAC;QACT,aAAa,CAAC,EAAE,CAAC,CAAC;QAClB,QAAQ,CAAC,EAAE,CAAC,CAAC;QACb,SAAS,CAAC,EAAE,CAAC,CAAC;QACd,UAAU,CAAC,EAAE,CAAC,CAAC;KAClB;IAED,UAAU,mBAAmB;QACzB,IAAI,EAAE,MAAM,CAAC;QACb,IAAI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;IAED,UAAU,wBAAwB;QAC9B,KAAK,CAAC,EAAE,MAAM,CAAC;QACf,IAAI,CAAC,EAAE,MAAM,CAAC;QACd,QAAQ,CAAC,EAAE,eAAe,GAAG,eAAe,EAAE,GAAG,MAAM,GAAG,MAAM,EAAE,CAAC;KACtE;IAED,UAAU,kBAAkB;QACxB,UAAU,CAAC,EAAE,OAAO,CAAC;QACrB,aAAa,CAAC,EAAE,wBAAwB,CAAC;QACzC,QAAQ,CAAC,EAAE,eAAe,GAAG,eAAe,EAAE,GAAG,MAAM,GAAG,MAAM,EAAE,CAAC;QACnE,IAAI,CAAC,EAAE,mBAAmB,CAAC;QAC3B,MAAM,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;KAC5B;CACJ;AAED,OAAO,UAAU,CAAC,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;AAGzC,OAAO,WAAW,CAAC,CAAC;IAChB,MAAM,QAAQ,EAAE;QAAE,OAAO,EAAE,eAAe,CAAA;KAAE,CAAC;IAC7C,MAAM,eAAe,EAAE;QAAE,OAAO,EAAE,eAAe,CAAA;KAAE,CAAC;IAEpD,MAAM,oBAAoB,CAAC,CAAC,EAAE,CAAC,SAAS,eAAe;oBACvC,OAAO,EAAE;YAAE,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;SAAE;QACpD,GAAG,EAAE,MAAM,CAAC,WAAW,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;QAC/B,UAAU,EAAE,MAAM,CAAC,CAAC;QACpB,QAAQ,EAAE,MAAM,WAAW,CAAC,CAAC,CAAC,CAAC;KAClC;CACJ;AAED,eAAe,CAAC,CAAC"}
@@ -0,0 +1,4 @@
1
+ const _$1 = _;
2
+
3
+ export { _$1 as default };
4
+ //# sourceMappingURL=paginate.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"paginate.mjs","sources":["../../src/types/paginate.ts"],"sourcesContent":["import type {\n FilterQuery,\n mongo,\n PaginateOptions,\n Schema,\n} from 'mongoose';\n\nimport type { IfElse } from './utils';\n\ndeclare module 'mongoose' {\n type PaginateDocument<\n T,\n TMethods,\n TQueryHelpers,\n O extends PaginateOptions = object,\n > = IfElse<\n O['lean'],\n IfElse<\n O['leanWithId'],\n T & { id: string },\n IfElse<\n O['leanWithVirtuals'],\n T & { [key: string]: any },\n T\n >\n >,\n HydratedDocument<T, TMethods, TQueryHelpers>\n >;\n\n interface PaginateCustomLabels<T = boolean | string | undefined> {\n docs?: T;\n hasNextPage?: T;\n hasPrevPage?: T;\n limit?: T;\n meta?: T;\n nextPage?: T;\n page?: T;\n pagingCounter?: T;\n prevPage?: T;\n totalDocs?: T;\n totalPages?: T;\n }\n\n interface PaginateModel<T, TQueryHelpers = object, TMethods = object> extends Model<T, TQueryHelpers, TMethods> {\n paginate: {\n <O extends PaginateOptions>(\n query?: FilterQuery<T>,\n options?: O,\n callback?: (err: any, result: PaginateResult<PaginateDocument<T, TMethods, TQueryHelpers, O>>) => void\n ): Promise<PaginateResult<PaginateDocument<T, TMethods, TQueryHelpers, O>>>;\n\n <UserType = T, O extends PaginateOptions = PaginateOptions>(\n query?: FilterQuery<T>,\n options?: O,\n callback?: (\n err: any,\n result: PaginateResult<PaginateDocument<UserType, TMethods, TQueryHelpers, O>>\n ) => void\n ): Promise<PaginateResult<PaginateDocument<UserType, TMethods, TQueryHelpers, O>>>;\n\n <UserType = T>(\n query?: FilterQuery<T>,\n options?: PaginateOptions,\n callback?: (\n err: any,\n result: PaginateResult<PaginateDocument<UserType, TMethods, TQueryHelpers, PaginateOptions>>\n ) => void\n ): Promise<PaginateResult<PaginateDocument<UserType, TMethods, TQueryHelpers, PaginateOptions>>>;\n };\n }\n\n interface PaginateOptions {\n allowDiskUse?: boolean;\n collation?: mongo.CollationOptions;\n customLabels?: PaginateCustomLabels;\n forceCountFn?: boolean;\n lean?: boolean;\n leanWithId?: boolean;\n leanWithVirtuals?: boolean;\n limit?: number;\n offset?: number;\n options?: QueryOptions;\n page?: number;\n\n /**\n * If pagination is set to `false`, it will return all docs without adding limit condition.\n *\n * @default true\n */\n pagination?: boolean;\n populate?: PopulateOptions | PopulateOptions[] | string | string[];\n projection?: any;\n read?: PaginateReadOptions;\n select?: object | string;\n sort?: object | string;\n useCustomCountFn?: () => Promise<number>;\n useEstimatedCount?: boolean;\n }\n\n interface PaginateReadOptions {\n pref: string;\n tags?: any[];\n }\n\n interface PaginateResult<T> {\n [customLabel: string]: boolean | null | number | T[] | undefined;\n docs: T[];\n hasNextPage: boolean;\n hasPrevPage: boolean;\n limit: number;\n meta?: any;\n nextPage?: null | number;\n offset: number;\n page?: number;\n pagingCounter: number;\n prevPage?: null | number;\n totalDocs: number;\n totalPages: number;\n }\n\n // @ts-expect-error Ignore this error.\n interface Query<\n ResultType,\n DocType,\n THelpers = NonNullable<unknown>,\n RawDocType = DocType,\n _QueryOp = 'find',\n TInstanceMethods = Record<string, never>,\n > {\n paginate: {\n <O extends PaginateOptions>(options?: O): Promise<\n PaginateResult<PaginateDocument<RawDocType, TInstanceMethods, THelpers, O>>\n >;\n\n <UserType = ResultType, O extends PaginateOptions = PaginateOptions>(options?: O): Promise<\n PaginateResult<PaginateDocument<UserType, TInstanceMethods, THelpers, O>>\n >;\n\n <UserType = ResultType>(options?: PaginateOptions): Promise<\n PaginateResult<PaginateDocument<UserType, TInstanceMethods, THelpers, PaginateOptions>>\n >;\n };\n }\n\n interface SubDocumentPagingOptions {\n limit?: number;\n page?: number;\n populate?: PopulateOptions | PopulateOptions[] | string | string[];\n }\n\n interface SubPaginateOptions {\n pagination?: boolean;\n pagingOptions?: SubDocumentPagingOptions;\n populate?: PopulateOptions | PopulateOptions[] | string | string[];\n read?: PaginateReadOptions;\n select?: object | string;\n }\n}\n\ndeclare function _(schema: Schema): void;\n\n// eslint-disable-next-line ts/no-namespace\ndeclare namespace _ {\n const paginate: { options: PaginateOptions };\n const paginateSubDocs: { options: PaginateOptions };\n\n class PaginationParameters<T, O extends PaginateOptions> {\n constructor(request: { query?: Record<string, any> });\n get: () => [FilterQuery<T>, O];\n getOptions: () => O;\n getQuery: () => FilterQuery<T>;\n }\n}\n\nexport default _;\n"],"names":[],"mappings":"AA8KA,YAAe,CAAC;;;;"}
@@ -0,0 +1,3 @@
1
+ 'use strict';
2
+
3
+ //# sourceMappingURL=utils.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;"}
@@ -0,0 +1,2 @@
1
+
2
+ //# sourceMappingURL=utils.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":""}
package/dist/utils.cjs CHANGED
@@ -30,10 +30,10 @@ const mongoosePaginate__default = /*#__PURE__*/_interopDefaultCompat(mongoosePag
30
30
  async function assertMongooseUpdateSuccess(updatePromise, expectedModifiedCount = 1) {
31
31
  const updateResult = await updatePromise;
32
32
  if (!updateResult.acknowledged)
33
- throw new Error('Update was not acknowledged.');
33
+ throw new Error('Update was not acknowledged');
34
34
  if (updateResult.modifiedCount < expectedModifiedCount) {
35
35
  // eslint-disable-next-line style/max-len
36
- throw new Error(`Expected to modify at least ${expectedModifiedCount} document(s), but modified ${updateResult.modifiedCount}.`);
36
+ throw new Error(`Expected to modify at least ${expectedModifiedCount} document(s), but modified ${updateResult.modifiedCount}`);
37
37
  }
38
38
  }
39
39
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"utils.cjs","sources":["../src/utils.ts"],"sourcesContent":["import mongoose, { Types } from 'mongoose';\nimport type {\n mongo,\n Schema,\n} from 'mongoose';\nimport mongooseAggregatePaginate from 'mongoose-aggregate-paginate-v2';\nimport mongoosePaginate from 'mongoose-paginate-v2';\n\nimport { customMongooseOptions } from './_internals';\nimport { mongooseConnections } from './constants';\nimport type { setCustomMongooseOptions } from './options';\nimport { mongooseNormalizePlugin } from './plugins/normalize';\nimport type { BuildMongooseModelOptions } from './types/options';\n\nexport type DoNotRemoveOrUseThisType = typeof setCustomMongooseOptions;\n\n/**\n * Asserts that a Mongoose update operation was successful by checking that it was acknowledged\n * and that at least the expected number of documents were modified.\n *\n * This function is intended for use with `updateOne`, `updateMany`, or any update operation\n * that returns a MongoDB `UpdateResult`.\n *\n * @template T - The shape of the document being updated.\n * @param updatePromise - A promise returned from a Mongoose update operation.\n * @param expectedModifiedCount - The minimum number of documents expected to be modified. Defaults to 1.\n * @throws Will throw an error if:\n * - The operation was not acknowledged by MongoDB,\n * - Fewer documents were modified than expected.\n */\nexport async function assertMongooseUpdateSuccess<T extends mongo.BSON.Document>(\n updatePromise: Promise<mongo.UpdateResult<T>>,\n expectedModifiedCount = 1,\n) {\n const updateResult = await updatePromise;\n if (!updateResult.acknowledged) throw new Error('Update was not acknowledged.');\n if (updateResult.modifiedCount < expectedModifiedCount) {\n // eslint-disable-next-line style/max-len\n throw new Error(`Expected to modify at least ${expectedModifiedCount} document(s), but modified ${updateResult.modifiedCount}.`);\n }\n}\n\n/**\n * Builds and returns a Mongoose model with specified schema and options.\n *\n * This function sets up a Mongoose model with optional plugins and timestamp settings.\n * It can optionally use a specified connection or the default connection.\n *\n * Determine the connection to use:\n * 1. Use the connection provided in the options, if available.\n * 2. Otherwise, use the default connection stored in `mongooseConnections`.\n * 3. If there's no default connection, create one using the MongoDB URI from the environment variables,\n * or fallback to a local MongoDB instance.\n *\n * You can set a function to be executed before the final build by using {@link setCustomMongooseOptions}.\n * However, ensure that this option is configured before calling {@link buildMongooseModel}.\n *\n * @template DocType - The type of the document.\n * @template Model - The type of the model, which extends `BaseMongoosePaginateModel`.\n * @template InstanceMethodsAndOverrides - The type for instance methods and overrides.\n * @template QueryHelpers - The type for additional query helpers.\n *\n * @param collection - The name of the collection.\n * @param name - The name of the model.\n * @param schema - The Mongoose schema to use for the model.\n * @param options - Optional settings for building the model.\n *\n * @returns The created Mongoose model.\n */\nexport function buildMongooseModel<\n DocType,\n Model extends BaseMongoosePaginateModel<DocType, InstanceMethodsAndOverrides, QueryHelpers>,\n InstanceMethodsAndOverrides = object,\n QueryHelpers = object,\n>(\n collection: string,\n name: string,\n schema: Schema<DocType, Model, InstanceMethodsAndOverrides, QueryHelpers>,\n options?: BuildMongooseModelOptions,\n) {\n if (options?.enableNormalizePlugin !== false) {\n schema.plugin(mongooseNormalizePlugin, options?.normalizePluginOptions);\n }\n\n schema.plugin(mongooseAggregatePaginate);\n schema.plugin(mongoosePaginate);\n schema.set('timestamps', options?.timestamps ?? schema.get('timestamps') ?? true);\n customMongooseOptions.beforeModelBuild?.(schema);\n // eslint-disable-next-line style/max-len\n const connection = options?.connection || (mongooseConnections.default ||= mongoose.createConnection(process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017'));\n return connection.model<DocType, Model, QueryHelpers>(name, schema, collection);\n}\n\n/**\n * Converts a document or ObjectId to a document using the specified model.\n *\n * This function takes a document or ObjectId and returns the corresponding document\n * from the database using the specified model. Optionally, specific fields can be selected.\n *\n * @template D - The type of the hydrated document.\n * @template DocType - The type of the document.\n * @template InstanceMethodsAndOverrides - The type for instance methods and overrides.\n * @template QueryHelpers - The type for additional query helpers.\n *\n * @param documentOrObjectId - The document or ObjectId to convert.\n * @param model - The Mongoose model to use for the conversion.\n * @param selectFields - Optional fields to select from the document.\n *\n * @returns The corresponding document or null if not found.\n */\nexport async function mongooseDocumentOrObjectIdToDocument<\n D extends MongooseHydratedDocument<DocType, InstanceMethodsAndOverrides, QueryHelpers>,\n DocType,\n InstanceMethodsAndOverrides,\n QueryHelpers,\n>(\n documentOrObjectId: MongooseDocumentOrObjectId<D>,\n model: BaseMongoosePaginateModel<DocType, InstanceMethodsAndOverrides, QueryHelpers>,\n selectFields?: string[],\n) {\n if (typeof documentOrObjectId === 'string' || documentOrObjectId instanceof Types.ObjectId) {\n return (await model.findById(documentOrObjectId).select(selectFields || [])) as D | null;\n }\n\n return documentOrObjectId;\n}\n"],"names":["mongooseNormalizePlugin","mongooseAggregatePaginate","mongoosePaginate","customMong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AU,CAAC;AACnF;AAEA;;;;;;;;;;;;;;;;AAgBG;AACI,eAAe,oCAAoC,CAMtD,kBAAiD,EACjD,KAAoF,EACpF,YAAuB,EAAA;IAEvB,IAAI,OAAO,kBAAkB,KAAK,QAAQ,IAAI,kBAAkB,YAAYC,cAAK,CAAC,QAAQ,EAAE;AACxF,QAAA,QAAQ,MAAM,KAAK,CAAC,QAAQ,CAAC,kBAAkB,CAAC,CAAC,MAAM,CAAC,YAAY,IAAI,EAAE,CAAC;;AAG/E,IAAA,OAAO,kBAAkB;AAC7B;;;;;;"}
1
+ {"version":3,"file":"utils.cjs","sources":["../src/utils.ts"],"sourcesContent":["import mongoose, { Types } from 'mongoose';\nimport type {\n mongo,\n Schema,\n} from 'mongoose';\nimport mongooseAggregatePaginate from 'mongoose-aggregate-paginate-v2';\nimport mongoosePaginate from 'mongoose-paginate-v2';\n\nimport { customMongooseOptions } from './_internals';\nimport { mongooseConnections } from './constants';\nimport type { setCustomMongooseOptions } from './options';\nimport { mongooseNormalizePlugin } from './plugins/normalize';\nimport type { BuildMongooseModelOptions } from './types/options';\n\nexport type DoNotRemoveOrUseThisType = typeof setCustomMongooseOptions;\n\n/**\n * Asserts that a Mongoose update operation was successful by checking that it was acknowledged\n * and that at least the expected number of documents were modified.\n *\n * This function is intended for use with `updateOne`, `updateMany`, or any update operation\n * that returns a MongoDB `UpdateResult`.\n *\n * @template T - The shape of the document being updated.\n * @param updatePromise - A promise returned from a Mongoose update operation.\n * @param expectedModifiedCount - The minimum number of documents expected to be modified. Defaults to 1.\n * @throws Will throw an error if:\n * - The operation was not acknowledged by MongoDB,\n * - Fewer documents were modified than expected.\n */\nexport async function assertMongooseUpdateSuccess<T extends mongo.BSON.Document>(\n updatePromise: Promise<mongo.UpdateResult<T>>,\n expectedModifiedCount = 1,\n) {\n const updateResult = await updatePromise;\n if (!updateResult.acknowledged) throw new Error('Update was not acknowledged');\n if (updateResult.modifiedCount < expectedModifiedCount) {\n // eslint-disable-next-line style/max-len\n throw new Error(`Expected to modify at least ${expectedModifiedCount} document(s), but modified ${updateResult.modifiedCount}`);\n }\n}\n\n/**\n * Builds and returns a Mongoose model with specified schema and options.\n *\n * This function sets up a Mongoose model with optional plugins and timestamp settings.\n * It can optionally use a specified connection or the default connection.\n *\n * Determine the connection to use:\n * 1. Use the connection provided in the options, if available.\n * 2. Otherwise, use the default connection stored in `mongooseConnections`.\n * 3. If there's no default connection, create one using the MongoDB URI from the environment variables,\n * or fallback to a local MongoDB instance.\n *\n * You can set a function to be executed before the final build by using {@link setCustomMongooseOptions}.\n * However, ensure that this option is configured before calling {@link buildMongooseModel}.\n *\n * @template DocType - The type of the document.\n * @template Model - The type of the model, which extends `BaseMongoosePaginateModel`.\n * @template InstanceMethodsAndOverrides - The type for instance methods and overrides.\n * @template QueryHelpers - The type for additional query helpers.\n *\n * @param collection - The name of the collection.\n * @param name - The name of the model.\n * @param schema - The Mongoose schema to use for the model.\n * @param options - Optional settings for building the model.\n *\n * @returns The created Mongoose model.\n */\nexport function buildMongooseModel<\n DocType,\n Model extends BaseMongoosePaginateModel<DocType, InstanceMethodsAndOverrides, QueryHelpers>,\n InstanceMethodsAndOverrides = object,\n QueryHelpers = object,\n>(\n collection: string,\n name: string,\n schema: Schema<DocType, Model, InstanceMethodsAndOverrides, QueryHelpers>,\n options?: BuildMongooseModelOptions,\n) {\n if (options?.enableNormalizePlugin !== false) {\n schema.plugin(mongooseNormalizePlugin, options?.normalizePluginOptions);\n }\n\n schema.plugin(mongooseAggregatePaginate);\n schema.plugin(mongoosePaginate);\n schema.set('timestamps', options?.timestamps ?? schema.get('timestamps') ?? true);\n customMongooseOptions.beforeModelBuild?.(schema);\n // eslint-disable-next-line style/max-len\n const connection = options?.connection || (mongooseConnections.default ||= mongoose.createConnection(process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017'));\n return connection.model<DocType, Model, QueryHelpers>(name, schema, collection);\n}\n\n/**\n * Converts a document or ObjectId to a document using the specified model.\n *\n * This function takes a document or ObjectId and returns the corresponding document\n * from the database using the specified model. Optionally, specific fields can be selected.\n *\n * @template D - The type of the hydrated document.\n * @template DocType - The type of the document.\n * @template InstanceMethodsAndOverrides - The type for instance methods and overrides.\n * @template QueryHelpers - The type for additional query helpers.\n *\n * @param documentOrObjectId - The document or ObjectId to convert.\n * @param model - The Mongoose model to use for the conversion.\n * @param selectFields - Optional fields to select from the document.\n *\n * @returns The corresponding document or null if not found.\n */\nexport async function mongooseDocumentOrObjectIdToDocument<\n D extends MongooseHydratedDocument<DocType, InstanceMethodsAndOverrides, QueryHelpers>,\n DocType,\n InstanceMethodsAndOverrides,\n QueryHelpers,\n>(\n documentOrObjectId: MongooseDocumentOrObjectId<D>,\n model: BaseMongoosePaginateModel<DocType, InstanceMethodsAndOverrides, QueryHelpers>,\n selectFields?: string[],\n) {\n if (typeof documentOrObjectId === 'string' || documentOrObjectId instanceof Types.ObjectId) {\n return (await model.findById(documentOrObjectId).select(selectFields || [])) as D | null;\n }\n\n return documentOrObjectId;\n}\n"],"names":["mongooseNormalizePlugin","mongooseAggregatePaginate","mongoosePaginate","customMongooseOptions","mongooseConnections","mongoose","Types"],"mappings":";;;;;;;;;;;;;;;AAgBA;;;;;;;;;;;;;AAaG;AACI,eAAe,2BAA2B,CAC7C,aAA6C,EAC7C,qBAAqB,GAAG,CAAC,EAAA;AAEzB,IAAA,MAAM,YAAY,GAAG,MAAM,aAAa;IACxC,IAAI,CAAC,YAAY,CAAC,YAAY;AAAE,QAAA,MAAM,IAAI,KAAK,CAAC,6BAA6B,CAAC;AAC9E,IAAA,IAAI,YAAY,CAAC,aAAa,GAAG,qBAAqB,EAAE;;QAEpD,MAAM,IAAI,KAAK,CAAC,CAAA,4BAAA,EAA+B,qBAAqB,CAAA,2BAAA,EAA8B,YAAY,CAAC,aAAa,CAAA,CAAE,CAAC;IACnI;AACJ;AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;AA0BG;AACG,SAAU,kBAAkB,CAM9B,UAAkB,EAClB,IAAY,EACZ,MAAyE,EACzE,OAAmC,EAAA;AAEnC,IAAA,IAAI,OAAO,EAAE,qBAAqB,KAAK,KAAK,EAAE;QAC1C,MAAM,CAAC,MAAM,CAACA,iCAAuB,EAAE,OAAO,EAAE,sBAAsB,CAAC;IAC3E;AAEA,IAAA,MAAM,CAAC,MAAM,CAACC,kCAAyB,CAAC;AACxC,IAAA,MAAM,CAAC,MAAM,CAACC,yBAAgB,CAAC;AAC/B,IAAA,MAAM,CAAC,GAAG,CAAC,YAAY,EAAE,OAAO,EAAE,UAAU,IAAI,MAAM,CAAC,GAAG,CAAC,YAAY,CAAC,IAAI,IAAI,CAAC;AACjF,IAAAC,gCAAqB,CAAC,gBAAgB,GAAG,MAAM,CAAC;;IAEhD,MAAM,UAAU,GAAG,OAAO,EAAE,UAAU,KAAKC,6BAAmB,CAAC,OAAO,KAAKC,iBAAQ,CAAC,gBAAgB,CAAC,OAAO,CAAC,GAAG,CAAC,WAAW,IAAI,2BAA2B,CAAC,CAAC;IAC7J,OAAO,UAAU,CAAC,KAAK,CAA+B,IAAI,EAAE,MAAM,EAAE,UAAU,CAAC;AACnF;AAEA;;;;;;;;;;;;;;;;AAgBG;AACI,eAAe,oCAAoC,CAMtD,kBAAiD,EACjD,KAAoF,EACpF,YAAuB,EAAA;IAEvB,IAAI,OAAO,kBAAkB,KAAK,QAAQ,IAAI,kBAAkB,YAAYC,cAAK,CAAC,QAAQ,EAAE;AACxF,QAAA,QAAQ,MAAM,KAAK,CAAC,QAAQ,CAAC,kBAAkB,CAAC,CAAC,MAAM,CAAC,YAAY,IAAI,EAAE,CAAC;IAC/E;AAEA,IAAA,OAAO,kBAAkB;AAC7B;;;;;;"}
package/dist/utils.mjs CHANGED
@@ -22,10 +22,10 @@ import { mongooseNormalizePlugin } from './plugins/normalize.mjs';
22
22
  async function assertMongooseUpdateSuccess(updatePromise, expectedModifiedCount = 1) {
23
23
  const updateResult = await updatePromise;
24
24
  if (!updateResult.acknowledged)
25
- throw new Error('Update was not acknowledged.');
25
+ throw new Error('Update was not acknowledged');
26
26
  if (updateResult.modifiedCount < expectedModifiedCount) {
27
27
  // eslint-disable-next-line style/max-len
28
- throw new Error(`Expected to modify at least ${expectedModifiedCount} document(s), but modified ${updateResult.modifiedCount}.`);
28
+ throw new Error(`Expected to modify at least ${expectedModifiedCount} document(s), but modified ${updateResult.modifiedCount}`);
29
29
  }
30
30
  }
31
31
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"utils.mjs","sources":["../src/utils.ts"],"sourcesContent":["import mongoose, { Types } from 'mongoose';\nimport type {\n mongo,\n Schema,\n} from 'mongoose';\nimport mongooseAggregatePaginate from 'mongoose-aggregate-paginate-v2';\nimport mongoosePaginate from 'mongoose-paginate-v2';\n\nimport { customMongooseOptions } from './_internals';\nimport { mongooseConnections } from './constants';\nimport type { setCustomMongooseOptions } from './options';\nimport { mongooseNormalizePlugin } from './plugins/normalize';\nimport type { BuildMongooseModelOptions } from './types/options';\n\nexport type DoNotRemoveOrUseThisType = typeof setCustomMongooseOptions;\n\n/**\n * Asserts that a Mongoose update operation was successful by checking that it was acknowledged\n * and that at least the expected number of documents were modified.\n *\n * This function is intended for use with `updateOne`, `updateMany`, or any update operation\n * that returns a MongoDB `UpdateResult`.\n *\n * @template T - The shape of the document being updated.\n * @param updatePromise - A promise returned from a Mongoose update operation.\n * @param expectedModifiedCount - The minimum number of documents expected to be modified. Defaults to 1.\n * @throws Will throw an error if:\n * - The operation was not acknowledged by MongoDB,\n * - Fewer documents were modified than expected.\n */\nexport async function assertMongooseUpdateSuccess<T extends mongo.BSON.Document>(\n updatePromise: Promise<mongo.UpdateResult<T>>,\n expectedModifiedCount = 1,\n) {\n const updateResult = await updatePromise;\n if (!updateResult.acknowledged) throw new Error('Update was not acknowledged.');\n if (updateResult.modifiedCount < expectedModifiedCount) {\n // eslint-disable-next-line style/max-len\n throw new Error(`Expected to modify at least ${expectedModifiedCount} document(s), but modified ${updateResult.modifiedCount}.`);\n }\n}\n\n/**\n * Builds and returns a Mongoose model with specified schema and options.\n *\n * This function sets up a Mongoose model with optional plugins and timestamp settings.\n * It can optionally use a specified connection or the default connection.\n *\n * Determine the connection to use:\n * 1. Use the connection provided in the options, if available.\n * 2. Otherwise, use the default connection stored in `mongooseConnections`.\n * 3. If there's no default connection, create one using the MongoDB URI from the environment variables,\n * or fallback to a local MongoDB instance.\n *\n * You can set a function to be executed before the final build by using {@link setCustomMongooseOptions}.\n * However, ensure that this option is configured before calling {@link buildMongooseModel}.\n *\n * @template DocType - The type of the document.\n * @template Model - The type of the model, which extends `BaseMongoosePaginateModel`.\n * @template InstanceMethodsAndOverrides - The type for instance methods and overrides.\n * @template QueryHelpers - The type for additional query helpers.\n *\n * @param collection - The name of the collection.\n * @param name - The name of the model.\n * @param schema - The Mongoose schema to use for the model.\n * @param options - Optional settings for building the model.\n *\n * @returns The created Mongoose model.\n */\nexport function buildMongooseModel<\n DocType,\n Model extends BaseMongoosePaginateModel<DocType, InstanceMethodsAndOverrides, QueryHelpers>,\n InstanceMethodsAndOverrides = object,\n QueryHelpers = object,\n>(\n collection: string,\n name: string,\n schema: Schema<DocType, Model, InstanceMethodsAndOverrides, QueryHelpers>,\n options?: BuildMongooseModelOptions,\n) {\n if (options?.enableNormalizePlugin !== false) {\n schema.plugin(mongooseNormalizePlugin, options?.normalizePluginOptions);\n }\n\n schema.plugin(mongooseAggregatePaginate);\n schema.plugin(mongoosePaginate);\n schema.set('timestamps', options?.timestamps ?? schema.get('timestamps') ?? true);\n customMongooseOptions.beforeModelBuild?.(schema);\n // eslint-disable-next-line style/max-len\n const connection = options?.connection || (mongooseConnections.default ||= mongoose.createConnection(process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017'));\n return connection.model<DocType, Model, QueryHelpers>(name, schema, collection);\n}\n\n/**\n * Converts a document or ObjectId to a document using the specified model.\n *\n * This function takes a document or ObjectId and returns the corresponding document\n * from the database using the specified model. Optionally, specific fields can be selected.\n *\n * @template D - The type of the hydrated document.\n * @template DocType - The type of the document.\n * @template InstanceMethodsAndOverrides - The type for instance methods and overrides.\n * @template QueryHelpers - The type for additional query helpers.\n *\n * @param documentOrObjectId - The document or ObjectId to convert.\n * @param model - The Mongoose model to use for the conversion.\n * @param selectFields - Optional fields to select from the document.\n *\n * @returns The corresponding document or null if not found.\n */\nexport async function mongooseDocumentOrObjectIdToDocument<\n D extends MongooseHydratedDocument<DocType, InstanceMethodsAndOverrides, QueryHelpers>,\n DocType,\n InstanceMethodsAndOverrides,\n QueryHelpers,\n>(\n documentOrObjectId: MongooseDocumentOrObjectId<D>,\n model: BaseMongoosePaginateModel<DocType, InstanceMethodsAndOverrides, QueryHelpers>,\n selectFields?: string[],\n) {\n if (typeof documentOrObjectId === 'string' || documentOrObjectId instanceof Types.ObjectId) {\n return (await model.findById(documentOrObjectId).select(selectFields || [])) as D | null;\n }\n\n return documentOrObjectId;\n}\n"],"names":[],"mappings":";;;;;;;AAgBA;;;;;;;;;;;;;AAaG;AACI,eAAe,2BAA2B,CAC7C,aAA6C,EAC7C,qBAAqB,GAAG,CAAC,EAAA;AAEzB,IAAA,MAAM,YAAY,GAAG,MAAM,aAAa;IACxC,IAAI,CAAC,YAAY,CAAC,YAAY;AAAE,QAAA,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC;AAC/E,IAAA,IAAI,YAAY,CAAC,aAAa,GAAG,qBAAqB,EAAE;;QAEpD,MAAM,IAAI,KAAK,CAAC,CAA+B,4BAAA,EAAA,qBAAqB,CAA8B,2BAAA,EAAA,YAAY,CAAC,aAAa,CAAG,CAAA,CAAA,CAAC;;AAExI;AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;AA0BG;AACG,SAAU,kBAAkB,CAM9B,UAAkB,EAClB,IAAY,EACZ,MAAyE,EACzE,OAAmC,EAAA;AAEnC,IAAA,IAAI,OAAO,EAAE,qBAAqB,KAAK,KAAK,EAAE;QAC1C,MAAM,CAAC,MAAM,CAAC,uBAAuB,EAAE,OAAO,EAAE,sBAAsB,CAAC;;AAG3E,IAAA,MAAM,CAAC,MAAM,CAAC,yBAAyB,CAAC;AACxC,IAAA,MAAM,CAAC,MAAM,CAAC,gBAAgB,CAAC;AAC/B,IAAA,MAAM,CAAC,GAAG,CAAC,YAAY,EAAE,OAAO,EAAE,UAAU,IAAI,MAAM,CAAC,GAAG,CAAC,YAAY,CAAC,IAAI,IAAI,CAAC;AACjF,IAAA,qBAAqB,CAAC,gBAAgB,GAAG,MAAM,CAAC;;IAEhD,MAAM,UAAU,GAAG,OAAO,EAAE,UAAU,KAAK,mBAAmB,CAAC,OAAO,KAAK,QAAQ,CAAC,gBAAgB,CAAC,OAAO,CAAC,GAAG,CAAC,WAAW,IAAI,2BAA2B,CAAC,CAAC;IAC7J,OAAO,UAAU,CAAC,KAAK,CAA+B,IAAI,EAAE,MAAM,EAAE,UAAU,CAAC;AACnF;AAEA;;;;;;;;;;;;;;;;AAgBG;AACI,eAAe,oCAAoC,CAMtD,kBAAiD,EACjD,KAAoF,EACpF,YAAuB,EAAA;IAEvB,IAAI,OAAO,kBAAkB,KAAK,QAAQ,IAAI,kBAAkB,YAAY,KAAK,CAAC,QAAQ,EAAE;AACxF,QAAA,QAAQ,MAAM,KAAK,CAAC,QAAQ,CAAC,kBAAkB,CAAC,CAAC,MAAM,CAAC,YAAY,IAAI,EAAE,CAAC;;AAG/E,IAAA,OAAO,kBAAkB;AAC7B;;;;"}
1
+ {"version":3,"file":"utils.mjs","sources":["../src/utils.ts"],"sourcesContent":["import mongoose, { Types } from 'mongoose';\nimport type {\n mongo,\n Schema,\n} from 'mongoose';\nimport mongooseAggregatePaginate from 'mongoose-aggregate-paginate-v2';\nimport mongoosePaginate from 'mongoose-paginate-v2';\n\nimport { customMongooseOptions } from './_internals';\nimport { mongooseConnections } from './constants';\nimport type { setCustomMongooseOptions } from './options';\nimport { mongooseNormalizePlugin } from './plugins/normalize';\nimport type { BuildMongooseModelOptions } from './types/options';\n\nexport type DoNotRemoveOrUseThisType = typeof setCustomMongooseOptions;\n\n/**\n * Asserts that a Mongoose update operation was successful by checking that it was acknowledged\n * and that at least the expected number of documents were modified.\n *\n * This function is intended for use with `updateOne`, `updateMany`, or any update operation\n * that returns a MongoDB `UpdateResult`.\n *\n * @template T - The shape of the document being updated.\n * @param updatePromise - A promise returned from a Mongoose update operation.\n * @param expectedModifiedCount - The minimum number of documents expected to be modified. Defaults to 1.\n * @throws Will throw an error if:\n * - The operation was not acknowledged by MongoDB,\n * - Fewer documents were modified than expected.\n */\nexport async function assertMongooseUpdateSuccess<T extends mongo.BSON.Document>(\n updatePromise: Promise<mongo.UpdateResult<T>>,\n expectedModifiedCount = 1,\n) {\n const updateResult = await updatePromise;\n if (!updateResult.acknowledged) throw new Error('Update was not acknowledged');\n if (updateResult.modifiedCount < expectedModifiedCount) {\n // eslint-disable-next-line style/max-len\n throw new Error(`Expected to modify at least ${expectedModifiedCount} document(s), but modified ${updateResult.modifiedCount}`);\n }\n}\n\n/**\n * Builds and returns a Mongoose model with specified schema and options.\n *\n * This function sets up a Mongoose model with optional plugins and timestamp settings.\n * It can optionally use a specified connection or the default connection.\n *\n * Determine the connection to use:\n * 1. Use the connection provided in the options, if available.\n * 2. Otherwise, use the default connection stored in `mongooseConnections`.\n * 3. If there's no default connection, create one using the MongoDB URI from the environment variables,\n * or fallback to a local MongoDB instance.\n *\n * You can set a function to be executed before the final build by using {@link setCustomMongooseOptions}.\n * However, ensure that this option is configured before calling {@link buildMongooseModel}.\n *\n * @template DocType - The type of the document.\n * @template Model - The type of the model, which extends `BaseMongoosePaginateModel`.\n * @template InstanceMethodsAndOverrides - The type for instance methods and overrides.\n * @template QueryHelpers - The type for additional query helpers.\n *\n * @param collection - The name of the collection.\n * @param name - The name of the model.\n * @param schema - The Mongoose schema to use for the model.\n * @param options - Optional settings for building the model.\n *\n * @returns The created Mongoose model.\n */\nexport function buildMongooseModel<\n DocType,\n Model extends BaseMongoosePaginateModel<DocType, InstanceMethodsAndOverrides, QueryHelpers>,\n InstanceMethodsAndOverrides = object,\n QueryHelpers = object,\n>(\n collection: string,\n name: string,\n schema: Schema<DocType, Model, InstanceMethodsAndOverrides, QueryHelpers>,\n options?: BuildMongooseModelOptions,\n) {\n if (options?.enableNormalizePlugin !== false) {\n schema.plugin(mongooseNormalizePlugin, options?.normalizePluginOptions);\n }\n\n schema.plugin(mongooseAggregatePaginate);\n schema.plugin(mongoosePaginate);\n schema.set('timestamps', options?.timestamps ?? schema.get('timestamps') ?? true);\n customMongooseOptions.beforeModelBuild?.(schema);\n // eslint-disable-next-line style/max-len\n const connection = options?.connection || (mongooseConnections.default ||= mongoose.createConnection(process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017'));\n return connection.model<DocType, Model, QueryHelpers>(name, schema, collection);\n}\n\n/**\n * Converts a document or ObjectId to a document using the specified model.\n *\n * This function takes a document or ObjectId and returns the corresponding document\n * from the database using the specified model. Optionally, specific fields can be selected.\n *\n * @template D - The type of the hydrated document.\n * @template DocType - The type of the document.\n * @template InstanceMethodsAndOverrides - The type for instance methods and overrides.\n * @template QueryHelpers - The type for additional query helpers.\n *\n * @param documentOrObjectId - The document or ObjectId to convert.\n * @param model - The Mongoose model to use for the conversion.\n * @param selectFields - Optional fields to select from the document.\n *\n * @returns The corresponding document or null if not found.\n */\nexport async function mongooseDocumentOrObjectIdToDocument<\n D extends MongooseHydratedDocument<DocType, InstanceMethodsAndOverrides, QueryHelpers>,\n DocType,\n InstanceMethodsAndOverrides,\n QueryHelpers,\n>(\n documentOrObjectId: MongooseDocumentOrObjectId<D>,\n model: BaseMongoosePaginateModel<DocType, InstanceMethodsAndOverrides, QueryHelpers>,\n selectFields?: string[],\n) {\n if (typeof documentOrObjectId === 'string' || documentOrObjectId instanceof Types.ObjectId) {\n return (await model.findById(documentOrObjectId).select(selectFields || [])) as D | null;\n }\n\n return documentOrObjectId;\n}\n"],"names":[],"mappings":";;;;;;;AAgBA;;;;;;;;;;;;;AAaG;AACI,eAAe,2BAA2B,CAC7C,aAA6C,EAC7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B,IAAI,OAAO,kBAAkB,KAAK,QAAQ,IAAI,kBAAkB,YAAY,KAAK,CAAC,QAAQ,EAAE;AACxF,QAAA,QAAQ,MAAM,KAAK,CAAC,QAAQ,CAAC,kBAAkB,CAAC,CAAC,MAAM,CAAC,YAAY,IAAI,EAAE,CAAC;IAC/E;AAEA,IAAA,OAAO,kBAAkB;AAC7B;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kikiutils/mongoose",
3
- "version": "3.1.0",
3
+ "version": "3.1.2",
4
4
  "description": "A Mongoose plugin for enhanced JSON normalization and common schema creation, with built-in support for pagination and automatic Decimal128 conversion.",
5
5
  "author": "kiki-kanri",
6
6
  "license": "MIT",
@@ -50,35 +50,43 @@
50
50
  "node": ">=18.12.1"
51
51
  },
52
52
  "scripts": {
53
- "build": "ts-project-builder ./src/constants.ts ./src/options.ts ./src/schema-builders/index.ts ./src/utils.ts --clean --preserve-modules --sourcemaps",
54
- "bumplog": "changelogen --bump",
55
- "lint": "eslint",
56
- "lint:fix": "eslint --fix",
57
- "release": "pnpm run lint && pnpm run build && pnpm run test && changelogen --hideAuthorEmail --push --release && npm publish",
58
- "test": "jest --coverage"
53
+ "build": "ts-project-builder './src/**/*.ts' --clean --preserve-modules --sourcemaps",
54
+ "bumplog": "changelogen --bump --hideAuthorEmail",
55
+ "depcheck": "depcheck",
56
+ "lint": "eslint --max-warnings 0",
57
+ "lint:fix": "eslint --fix --max-warnings 0",
58
+ "prepack": "pnpm run build",
59
+ "release": "pnpm run lint && pnpm run typecheck && pnpm run test && pnpm run build && changelogen --hideAuthorEmail --push --release && npm publish",
60
+ "test": "vitest run --coverage",
61
+ "test:watch": "vitest watch --coverage",
62
+ "typecheck": "tsc --noEmit",
63
+ "unused-exports": "ts-unused-exports ./tsconfig.json"
59
64
  },
60
65
  "dependencies": {
61
- "decimal.js": "^10.5.0",
66
+ "decimal.js": "^10.6.0",
62
67
  "lodash": "^4.17.21",
63
- "mongoose": "^8.13.2",
68
+ "mongoose": "^8.16.3",
64
69
  "mongoose-aggregate-paginate-v2": "^1.1.4",
65
- "mongoose-paginate-v2": "^1.9.0",
66
- "type-fest": "^4.39.1"
70
+ "mongoose-paginate-v2": "^1.9.1",
71
+ "type-fest": "^4.41.0"
67
72
  },
68
73
  "devDependencies": {
69
- "@kikiutils/changelogen": "^0.8.0",
70
- "@kikiutils/eslint-config": "^0.12.2",
71
- "@kikiutils/tsconfigs": "^4.1.0",
72
- "@types/jest": "^29.5.14",
73
- "@types/lodash": "^4.17.16",
74
- "@types/node": "^22.14.1",
75
- "jest": "^29.7.0",
76
- "ts-jest": "^29.3.2",
77
- "ts-project-builder": "^4.0.1"
74
+ "@kikiutils/changelogen": "^0.8.1",
75
+ "@kikiutils/eslint-config": "^1.2.7",
76
+ "@kikiutils/tsconfigs": "^5.0.4",
77
+ "@types/lodash": "^4.17.20",
78
+ "@types/node": "^24.0.14",
79
+ "@vitest/coverage-v8": "^3.2.4",
80
+ "depcheck": "^1.4.7",
81
+ "ts-project-builder": "^5.0.2",
82
+ "ts-unused-exports": "^11.0.1",
83
+ "typescript": "^5.8.3",
84
+ "vitest": "^3.2.4"
78
85
  },
79
86
  "pnpm": {
80
87
  "onlyBuiltDependencies": [
81
- "esbuild"
88
+ "esbuild",
89
+ "unrs-resolver"
82
90
  ]
83
91
  }
84
92
  }
@@ -45,7 +45,7 @@ export function mongooseNormalizePlugin<S extends Schema>(schema: S, pluginOptio
45
45
  'toJSON',
46
46
  {
47
47
  ...toJson,
48
- transform(doc: any, ret: any, options: any) {
48
+ transform(doc, ret, options) {
49
49
  // eslint-disable-next-line style/object-curly-newline
50
50
  let { __v, _id, ...copiedRet } = ret;
51
51
  if (pluginOptions?.toHexIdIfObjectId !== false) {
@@ -63,7 +63,7 @@ export function mongooseNormalizePlugin<S extends Schema>(schema: S, pluginOptio
63
63
  }
64
64
 
65
65
  if (toJsonTransform && typeof toJsonTransform !== 'boolean') {
66
- return toJsonTransform(doc, copiedRet, options);
66
+ return toJsonTransform(doc as any, copiedRet as any, options as any);
67
67
  }
68
68
 
69
69
  return copiedRet;
@@ -24,14 +24,6 @@ interface BaseProps {
24
24
  type: typeof Schema.Types.Decimal128;
25
25
  }
26
26
 
27
- interface ToStringGetterSchema {
28
- get: (value?: Types.Decimal128) => string | undefined;
29
- }
30
-
31
- interface ToStringSetterSchema {
32
- set: (value?: { toString: () => string }) => string;
33
- }
34
-
35
27
  export interface Decimal128SchemaBuilder<
36
28
  Props extends { type: typeof Schema.Types.Decimal128 } = { type: typeof Schema.Types.Decimal128 },
37
29
  ExtraOmitFields extends string = never,
@@ -98,6 +90,14 @@ export interface Decimal128SchemaBuilder<
98
90
  unique: ExtendDecimal128SchemaBuilder<Merge<Props, { unique: true }>, ExtraOmitFields>;
99
91
  }
100
92
 
93
+ interface ToStringGetterSchema {
94
+ get: (value?: Types.Decimal128) => string | undefined;
95
+ }
96
+
97
+ interface ToStringSetterSchema {
98
+ set: (value?: { toString: () => string }) => string;
99
+ }
100
+
101
101
  const baseBuilderFactory = createBaseSchemaBuilderFactory(Schema.Types.Decimal128);
102
102
 
103
103
  export function decimal128SchemaBuilder() {
@@ -41,9 +41,10 @@ export interface NumberSchemaBuilder<
41
41
  | { message?: M; values: Readonlyable<Array<N | null>> },
42
42
  M extends string,
43
43
  N extends number,
44
- >(
45
- value: T
46
- ) => ExtendNumberSchemaBuilder<Merge<Props, { enum: T }>, ExtraOmitFields>;
44
+ >(value: T) => ExtendNumberSchemaBuilder<
45
+ Merge<Props, { enum: T }>,
46
+ ExtraOmitFields
47
+ >;
47
48
 
48
49
  index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendNumberSchemaBuilder<
49
50
  Merge<Props, { index: T }>,
@@ -136,8 +136,8 @@ export interface StringSchemaBuilder<
136
136
  }
137
137
 
138
138
  const baseBuilderFactory = createBaseSchemaBuilderFactory(String);
139
- const defaultIpv4ValidateMessage = '`{VALUE}` is not a valid IPv4 address for path `{PATH}`.';
140
- const defaultIpv6ValidateMessage = '`{VALUE}` is not a valid IPv6 address for path `{PATH}`.';
139
+ const defaultIpv4ValidateMessage = '`{VALUE}` is not a valid IPv4 address for path `{PATH}`';
140
+ const defaultIpv6ValidateMessage = '`{VALUE}` is not a valid IPv6 address for path `{PATH}`';
141
141
 
142
142
  export function stringSchemaBuilder() {
143
143
  const schema: Record<string, any> = {};
@@ -27,6 +27,18 @@ declare module 'mongoose' {
27
27
  totalPages?: T;
28
28
  }
29
29
 
30
+ interface AggregatePaginateModel<
31
+ RawDocType,
32
+ QueryHelpers = object,
33
+ InstanceMethodsAndOverrides = object,
34
+ > extends Model<RawDocType, QueryHelpers, InstanceMethodsAndOverrides> {
35
+ aggregatePaginate: <T>(
36
+ query?: Aggregate<T[]> | PrePaginatePipelineStage[],
37
+ options?: AggregatePaginateOptions,
38
+ callback?: (err: any, result: AggregatePaginateResult<T>) => void,
39
+ ) => Promise<AggregatePaginateResult<T>>;
40
+ }
41
+
30
42
  interface AggregatePaginateOptions {
31
43
  allowDiskUse?: boolean;
32
44
  countQuery?: object;
@@ -74,18 +86,6 @@ declare module 'mongoose' {
74
86
  totalDocs: number;
75
87
  totalPages: number;
76
88
  }
77
-
78
- interface AggregatePaginateModel<
79
- RawDocType,
80
- QueryHelpers = object,
81
- InstanceMethodsAndOverrides = object,
82
- > extends Model<RawDocType, QueryHelpers, InstanceMethodsAndOverrides> {
83
- aggregatePaginate: <T>(
84
- query?: Aggregate<T[]> | PrePaginatePipelineStage[],
85
- options?: AggregatePaginateOptions,
86
- callback?: (err: any, result: AggregatePaginateResult<T>) => void,
87
- ) => Promise<AggregatePaginateResult<T>>;
88
- }
89
89
  }
90
90
 
91
91
  declare function mongooseAggregatePaginate(schema: Schema): void;