@kikiutils/mongoose 1.7.0 → 2.0.0-rc.0

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/README.md CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  [![npm version][npm-version-src]][npm-version-href]
4
4
  [![npm downloads][npm-downloads-src]][npm-downloads-href]
5
+ [![codecov][codecov-src]][codecov-href]
5
6
  [![License][license-src]][license-href]
6
7
 
7
8
  A Mongoose plugin for enhanced JSON normalization and common schema creation, with built-in support for pagination and automatic Decimal128 conversion.
@@ -46,11 +47,14 @@ Please refer to the [examples](./examples/README.md) folder for usage instructio
46
47
  [MIT License](./LICENSE)
47
48
 
48
49
  <!-- Badges -->
49
- [npm-version-src]: https://img.shields.io/npm/v/@kikiutils/mongoose/latest.svg?style=flat&colorA=18181B&colorB=28CF8D
50
50
  [npm-version-href]: https://npmjs.com/package/@kikiutils/mongoose
51
+ [npm-version-src]: https://img.shields.io/npm/v/@kikiutils/mongoose/latest.svg?style=flat&colorA=18181B&colorB=28CF8D
51
52
 
52
- [npm-downloads-src]: https://img.shields.io/npm/dm/@kikiutils/mongoose.svg?style=flat&colorA=18181B&colorB=28CF8D
53
53
  [npm-downloads-href]: https://npmjs.com/package/@kikiutils/mongoose
54
+ [npm-downloads-src]: https://img.shields.io/npm/dm/@kikiutils/mongoose.svg?style=flat&colorA=18181B&colorB=28CF8D
55
+
56
+ [codecov-href]: https://codecov.io/github/kiki-kanri/kikiutils-node-mongoose
57
+ [codecov-src]: https://codecov.io/github/kiki-kanri/kikiutils-node-mongoose/graph/badge.svg?token=GRSQ7JO39E
54
58
 
55
- [license-src]: https://img.shields.io/npm/l/@kikiutils/mongoose.svg?style=flat&colorA=18181B&colorB=28CF8D
56
59
  [license-href]: https://github.com/kiki-kanri/kikiutils-node-mongoose/blob/main/LICENSE
60
+ [license-src]: https://img.shields.io/npm/l/@kikiutils/mongoose.svg?style=flat&colorA=18181B&colorB=28CF8D
package/_options.cjs ADDED
@@ -0,0 +1,5 @@
1
+ 'use strict';
2
+
3
+ const customMongooseOptions = {};
4
+
5
+ exports.customMongooseOptions = customMongooseOptions;
package/_options.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import type { CustomMongooseOptions } from './types/options';
2
+ export declare const customMongooseOptions: CustomMongooseOptions;
package/_options.mjs ADDED
@@ -0,0 +1,3 @@
1
+ const customMongooseOptions = {};
2
+
3
+ export { customMongooseOptions };
package/options.cjs CHANGED
@@ -1,5 +1,8 @@
1
1
  'use strict';
2
2
 
3
+ /**
4
+ * @deprecated - Will be removed in the next major version. Use {@link setCustomMongooseOptions} instead.
5
+ */
3
6
  const customMongooseOptions = {};
4
7
 
5
8
  exports.customMongooseOptions = customMongooseOptions;
package/options.d.ts CHANGED
@@ -1,2 +1,15 @@
1
1
  import type { CustomMongooseOptions } from './types/options';
2
+ import type { buildMongooseModel } from './utils';
3
+ export type DoNotUseOrRemoveThisType = typeof buildMongooseModel;
4
+ /**
5
+ * @deprecated - Will be removed in the next major version. Use {@link setCustomMongooseOptions} instead.
6
+ */
2
7
  export declare const customMongooseOptions: CustomMongooseOptions;
8
+ /**
9
+ * Sets custom options.
10
+ *
11
+ * For example, you can set {@link CustomMongooseOptions.beforeModelBuild} to define a function that will be called
12
+ * every time {@link buildMongooseModel} is used. Inside this function, you can modify
13
+ * the schema, allowing you to apply common settings to all global schemas.
14
+ */
15
+ export declare const setCustomMongooseOptions: <K extends keyof CustomMongooseOptions>(key: K, value: CustomMongooseOptions[K]) => CustomMongooseOptions[K];
package/options.mjs CHANGED
@@ -1,3 +1,6 @@
1
+ /**
2
+ * @deprecated - Will be removed in the next major version. Use {@link setCustomMongooseOptions} instead.
3
+ */
1
4
  const customMongooseOptions = {};
2
5
 
3
6
  export { customMongooseOptions };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kikiutils/mongoose",
3
- "version": "1.7.0",
3
+ "version": "2.0.0-rc.0",
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
  "license": "MIT",
6
6
  "author": "kiki-kanri",
@@ -11,7 +11,7 @@
11
11
  "scripts": {
12
12
  "build": "ts-project-builder ./src/constants.ts ./src/schema-builders/index.ts ./src/utils.ts --clean --preserve-modules",
13
13
  "bumplog": "changelogen --bump",
14
- "release": "pnpm run build && changelogen --push --release && sh ./build-and-publish.sh",
14
+ "release": "pnpm run build && pnpm run test && changelogen --push --release && sh ./build-and-publish.sh",
15
15
  "test": "jest --coverage"
16
16
  },
17
17
  "dependencies": {
@@ -57,6 +57,11 @@
57
57
  "require": null,
58
58
  "types": null
59
59
  },
60
+ "./_options": {
61
+ "import": null,
62
+ "require": null,
63
+ "types": null
64
+ },
60
65
  "./schema-builders": {
61
66
  "import": "./schema-builders/index.mjs",
62
67
  "require": "./schema-builders/index.cjs",
@@ -1,2 +1,2 @@
1
1
  import type { Schema } from 'mongoose';
2
- export declare const createBaseSchemaBuilderFactory: <Builder = Readonly<{}>>(type: BooleanConstructor | DateConstructor | NumberConstructor | Schema.Types.ObjectId["constructor"] | StringConstructor) => (schema?: Record<string, any>) => Builder;
2
+ export declare const createBaseSchemaBuilderFactory: <Builder = Readonly<Record<string, any>>>(type: BooleanConstructor | DateConstructor | NumberConstructor | Schema.Types.ObjectId["constructor"] | StringConstructor) => (schema?: Record<string, any>) => Builder;
@@ -6,9 +6,7 @@ export interface BooleanSchemaBuilder<Props extends {
6
6
  type: BooleanSchemaDefinition;
7
7
  } = {
8
8
  type: BooleanSchemaDefinition;
9
- }, ExtraOmitFields extends string = never, PropsWithRequired = Props & {
10
- required: true;
11
- }> {
9
+ }, ExtraOmitFields extends string = never> {
12
10
  default: <T extends DefaultType<D> | ((this: any, doc: any) => DefaultType<D>) | null, D extends boolean>(value: T) => ExtendBooleanSchemaBuilder<{
13
11
  [key in keyof (Props & {
14
12
  default: T;
@@ -27,7 +25,11 @@ export interface BooleanSchemaBuilder<Props extends {
27
25
  })[key];
28
26
  }, ExtraOmitFields>;
29
27
  required: {
30
- [key in keyof PropsWithRequired]: PropsWithRequired[key];
28
+ [key in keyof (Props & {
29
+ required: true;
30
+ })]: (Props & {
31
+ required: true;
32
+ })[key];
31
33
  };
32
34
  sparse: ExtendBooleanSchemaBuilder<{
33
35
  [key in keyof (Props & {
@@ -46,9 +48,5 @@ export interface BooleanSchemaBuilder<Props extends {
46
48
  }
47
49
  export declare const booleanSchemaBuilder: (schema?: Record<string, any>) => BooleanSchemaBuilder<{
48
50
  type: BooleanSchemaDefinition;
49
- }, never, {
50
- type: BooleanSchemaDefinition;
51
- } & {
52
- required: true;
53
- }>;
51
+ }, never>;
54
52
  export {};
@@ -7,9 +7,7 @@ export interface DateSchemaBuilder<Props extends {
7
7
  type: DateSchemaDefinition;
8
8
  } = {
9
9
  type: DateSchemaDefinition;
10
- }, ExtraOmitFields extends string = never, PropsWithRequired = Props & {
11
- required: true;
12
- }> {
10
+ }, ExtraOmitFields extends string = never> {
13
11
  default: <T extends DefaultType<D> | ((this: any, doc: any) => DefaultType<D>) | null, D extends NativeDate>(value: T) => ExtendDateSchemaBuilder<{
14
12
  [key in keyof (Props & {
15
13
  default: T;
@@ -54,7 +52,11 @@ export interface DateSchemaBuilder<Props extends {
54
52
  })[key];
55
53
  }, ExtraOmitFields>;
56
54
  required: {
57
- [key in keyof PropsWithRequired]: PropsWithRequired[key];
55
+ [key in keyof (Props & {
56
+ required: true;
57
+ })]: (Props & {
58
+ required: true;
59
+ })[key];
58
60
  };
59
61
  sparse: ExtendDateSchemaBuilder<{
60
62
  [key in keyof (Props & {
@@ -73,9 +75,5 @@ export interface DateSchemaBuilder<Props extends {
73
75
  }
74
76
  export declare const dateSchemaBuilder: (schema?: Record<string, any>) => DateSchemaBuilder<{
75
77
  type: DateSchemaDefinition;
76
- }, never, {
77
- type: DateSchemaDefinition;
78
- } & {
79
- required: true;
80
- }>;
78
+ }, never>;
81
79
  export {};
@@ -54,9 +54,27 @@ export interface Decimal128SchemaBuilder<Props extends {
54
54
  required: true;
55
55
  })[key];
56
56
  };
57
+ /**
58
+ * Sets the rounding and toFixed behavior for Decimal128 fields.
59
+ * This method allows specifying the number of decimal places and the rounding strategy to be used when
60
+ * saving the field to the database.
61
+ *
62
+ * @param places - The number of decimal places to round to (default is 2).
63
+ * @param rounding - The rounding strategy from the Decimal.js library (default is Decimal.ROUND_DOWN).
64
+ * @returns A schema builder with the rounding and toFixed behavior applied to the `set` option in Mongoose.
65
+ */
57
66
  setRoundAndToFixedSetter: (places?: number, rounding?: Decimal.Rounding) => ExtendDecimal128SchemaBuilder<{
58
67
  [key in keyof (Props & ToStringSetterSchema)]: (Props & ToStringSetterSchema)[key];
59
68
  }, ExtraOmitFields | 'setRoundAndToFixedSetter'>;
69
+ /**
70
+ * Sets the toString behavior for Decimal128 fields.
71
+ * This method defines how the Decimal128 field should be converted to a string when processing or
72
+ * retrieving the value in the application (e.g., via getters), while still storing the value as Decimal128
73
+ * in the database.
74
+ *
75
+ * @returns A schema builder with the custom `get` behavior applied to convert the Decimal128 field to a string
76
+ * when accessed in the application.
77
+ */
60
78
  setToStringGetter: ExtendDecimal128SchemaBuilder<{
61
79
  [key in keyof (Props & ToStringGetterSchema)]: (Props & ToStringGetterSchema)[key];
62
80
  }, ExtraOmitFields | 'setToStringGetter'>;
@@ -7,33 +7,17 @@ export * from './string';
7
7
  export declare const schemaBuilders: {
8
8
  date: (schema?: Record<string, any>) => import("./date").DateSchemaBuilder<{
9
9
  type: import("mongoose").DateSchemaDefinition;
10
- }, never, {
11
- type: import("mongoose").DateSchemaDefinition;
12
- } & {
13
- required: true;
14
- }>;
10
+ }, never>;
15
11
  decimal128: () => import("./decimal128").Decimal128SchemaBuilder;
16
12
  boolean: (schema?: Record<string, any>) => import("./boolean").BooleanSchemaBuilder<{
17
13
  type: import("mongoose").BooleanSchemaDefinition;
18
- }, never, {
19
- type: import("mongoose").BooleanSchemaDefinition;
20
- } & {
21
- required: true;
22
- }>;
14
+ }, never>;
23
15
  number: (schema?: Record<string, any>) => import("./number").NumberSchemaBuilder<{
24
16
  type: import("mongoose").NumberSchemaDefinition;
25
- }, never, {
26
- type: import("mongoose").NumberSchemaDefinition;
27
- } & {
28
- required: true;
29
- }>;
17
+ }, never>;
30
18
  objectId: (schema?: Record<string, any>) => import("./object-id").ObjectIdSchemaBuilder<{
31
19
  type: import("mongoose").ObjectIdSchemaDefinition;
32
- }, never, {
33
- type: import("mongoose").ObjectIdSchemaDefinition;
34
- } & {
35
- required: true;
36
- }>;
20
+ }, never>;
37
21
  ref: <T extends string | import("mongoose").Model<any> | ((this: any, doc: any) => string | import("mongoose").Model<any>)>(ref: T) => import("./ref").RefSchemaBuilder<{
38
22
  ref: T;
39
23
  type: import("mongoose").ObjectIdSchemaDefinition;
@@ -7,9 +7,7 @@ export interface NumberSchemaBuilder<Props extends {
7
7
  type: NumberSchemaDefinition;
8
8
  } = {
9
9
  type: NumberSchemaDefinition;
10
- }, ExtraOmitFields extends string = never, PropsWithRequired = Props & {
11
- required: true;
12
- }> {
10
+ }, ExtraOmitFields extends string = never> {
13
11
  default: <T extends DefaultType<D> | ((this: any, doc: any) => DefaultType<D>) | null, D extends number>(value: T) => ExtendNumberSchemaBuilder<{
14
12
  [key in keyof (Props & {
15
13
  default: T;
@@ -54,7 +52,11 @@ export interface NumberSchemaBuilder<Props extends {
54
52
  })[key];
55
53
  }, ExtraOmitFields>;
56
54
  required: {
57
- [key in keyof PropsWithRequired]: PropsWithRequired[key];
55
+ [key in keyof (Props & {
56
+ required: true;
57
+ })]: (Props & {
58
+ required: true;
59
+ })[key];
58
60
  };
59
61
  sparse: ExtendNumberSchemaBuilder<{
60
62
  [key in keyof (Props & {
@@ -73,9 +75,5 @@ export interface NumberSchemaBuilder<Props extends {
73
75
  }
74
76
  export declare const numberSchemaBuilder: (schema?: Record<string, any>) => NumberSchemaBuilder<{
75
77
  type: NumberSchemaDefinition;
76
- }, never, {
77
- type: NumberSchemaDefinition;
78
- } & {
79
- required: true;
80
- }>;
78
+ }, never>;
81
79
  export {};
@@ -7,9 +7,7 @@ export interface ObjectIdSchemaBuilder<Props extends {
7
7
  type: ObjectIdSchemaDefinition;
8
8
  } = {
9
9
  type: ObjectIdSchemaDefinition;
10
- }, ExtraOmitFields extends string = never, PropsWithRequired = Props & {
11
- required: true;
12
- }> {
10
+ }, ExtraOmitFields extends string = never> {
13
11
  default: <T extends DefaultType<D> | ((this: any, doc: any) => DefaultType<D>) | null, D extends Types.ObjectId>(value: T) => ExtendObjectIdSchemaBuilder<{
14
12
  [key in keyof (Props & {
15
13
  default: T;
@@ -40,7 +38,11 @@ export interface ObjectIdSchemaBuilder<Props extends {
40
38
  })[key];
41
39
  }, ExtraOmitFields>;
42
40
  required: {
43
- [key in keyof PropsWithRequired]: PropsWithRequired[key];
41
+ [key in keyof (Props & {
42
+ required: true;
43
+ })]: (Props & {
44
+ required: true;
45
+ })[key];
44
46
  };
45
47
  sparse: ExtendObjectIdSchemaBuilder<{
46
48
  [key in keyof (Props & {
@@ -59,9 +61,5 @@ export interface ObjectIdSchemaBuilder<Props extends {
59
61
  }
60
62
  export declare const objectIdSchemaBuilder: (schema?: Record<string, any>) => ObjectIdSchemaBuilder<{
61
63
  type: ObjectIdSchemaDefinition;
62
- }, never, {
63
- type: ObjectIdSchemaDefinition;
64
- } & {
65
- required: true;
66
- }>;
64
+ }, never>;
67
65
  export {};
@@ -7,9 +7,7 @@ export interface RefSchemaBuilder<Props extends {
7
7
  type: ObjectIdSchemaDefinition;
8
8
  } = {
9
9
  type: ObjectIdSchemaDefinition;
10
- }, ExtraOmitFields extends string = never, PropsWithRequired = Props & {
11
- required: true;
12
- }> {
10
+ }, ExtraOmitFields extends string = never> {
13
11
  default: <T extends DefaultType<D> | ((this: any, doc: any) => DefaultType<D>) | null, D extends Types.ObjectId>(value: T) => ExtendRefSchemaBuilder<{
14
12
  [key in keyof (Props & {
15
13
  default: T;
@@ -40,7 +38,11 @@ export interface RefSchemaBuilder<Props extends {
40
38
  })[key];
41
39
  }, ExtraOmitFields>;
42
40
  required: {
43
- [key in keyof PropsWithRequired]: PropsWithRequired[key];
41
+ [key in keyof (Props & {
42
+ required: true;
43
+ })]: (Props & {
44
+ required: true;
45
+ })[key];
44
46
  };
45
47
  sparse: ExtendRefSchemaBuilder<{
46
48
  [key in keyof (Props & {
@@ -14,9 +14,7 @@ export interface StringSchemaBuilder<Props extends {
14
14
  type: StringSchemaDefinition;
15
15
  } = {
16
16
  type: StringSchemaDefinition;
17
- }, ExtraOmitFields extends string = never, PropsWithRequired = Props & {
18
- required: true;
19
- }> {
17
+ }, ExtraOmitFields extends string = never> {
20
18
  default: <T extends DefaultType<D> | ((this: any, doc: any) => DefaultType<D>) | null, D extends string>(value: T) => ExtendStringSchemaBuilder<{
21
19
  [key in keyof (Props & {
22
20
  default: T;
@@ -36,12 +34,33 @@ export interface StringSchemaBuilder<Props extends {
36
34
  enum: T;
37
35
  })[key];
38
36
  }, ExtraOmitFields>;
37
+ /**
38
+ * Adds IPv4 validation to the string schema.
39
+ * Ensures the string is a valid IPv4 address and trims the input. The validation message can be customized.
40
+ *
41
+ * @param message - The custom error message to return if validation fails. Defaults to a standard IPv4 validation message.
42
+ * @returns A schema builder with IPv4 validation and the `trim` option enabled.
43
+ */
39
44
  ipv4: <T extends string = typeof defaultIPv4ValidateMessage>(message?: T) => ExtendStringSchemaBuilder<{
40
45
  [key in keyof (Props & IPSchema<T>)]: (Props & IPSchema<T>)[key];
41
46
  }, ExtraOmitFields | 'ipv4' | 'ipv6'>;
47
+ /**
48
+ * Adds IPv6 validation to the string schema.
49
+ * Ensures the string is a valid IPv6 address and trims the input. The validation message can be customized.
50
+ *
51
+ * @param message - The custom error message to return if validation fails. Defaults to a standard IPv6 validation message.
52
+ * @returns A schema builder with IPv6 validation and the `trim` option enabled.
53
+ */
42
54
  ipv6: <T extends string = typeof defaultIPv6ValidateMessage>(message?: T) => ExtendStringSchemaBuilder<{
43
55
  [key in keyof (Props & IPSchema<T>)]: (Props & IPSchema<T>)[key];
44
56
  }, ExtraOmitFields | 'ipv4' | 'ipv6'>;
57
+ /**
58
+ * Sets both the maximum and minimum length of the string.
59
+ * This method ensures the string length is exactly or within a specified range.
60
+ *
61
+ * @param value - A number representing the exact length or an array specifying [minimum, maximum] length.
62
+ * @returns A schema builder with both `maxlength` and `minlength` options applied.
63
+ */
45
64
  length: <T extends L | MaybeReadonly<[L, S]>, L extends number, S extends string>(value: T) => ExtendStringSchemaBuilder<{
46
65
  [key in keyof (Props & {
47
66
  maxlength: T;
@@ -83,7 +102,11 @@ export interface StringSchemaBuilder<Props extends {
83
102
  })[key];
84
103
  }, ExtraOmitFields>;
85
104
  required: {
86
- [key in keyof PropsWithRequired]: PropsWithRequired[key];
105
+ [key in keyof (Props & {
106
+ required: true;
107
+ })]: (Props & {
108
+ required: true;
109
+ })[key];
87
110
  };
88
111
  sparse: ExtendStringSchemaBuilder<{
89
112
  [key in keyof (Props & {
@@ -1,7 +1,6 @@
1
1
  import type { Connection, Schema, SchemaTimestampsConfig } from 'mongoose';
2
- export interface CustomMongooseOptions {
3
- beforeModelBuild?: <DocType, Model extends BaseMongoosePaginateModel<DocType, InstanceMethodsAndOverrides, QueryHelpers>, InstanceMethodsAndOverrides = {}, QueryHelpers = {}>(schema: Schema<DocType, Model, InstanceMethodsAndOverrides, QueryHelpers>) => void;
4
- }
2
+ import type { buildMongooseModel } from '../utils';
3
+ export type DoNotUseOrRemoveThisType = typeof buildMongooseModel;
5
4
  /**
6
5
  * Interface for options used when building a Mongoose model.
7
6
  *
@@ -37,3 +36,9 @@ export interface BuildMongooseModelOptions {
37
36
  */
38
37
  timestamps?: boolean | SchemaTimestampsConfig;
39
38
  }
39
+ export interface CustomMongooseOptions {
40
+ /**
41
+ * A function that will be executed before the final build when using {@link buildMongooseModel}.
42
+ */
43
+ beforeModelBuild?: <DocType, Model extends BaseMongoosePaginateModel<DocType, InstanceMethodsAndOverrides, QueryHelpers>, InstanceMethodsAndOverrides = {}, QueryHelpers = {}>(schema: Schema<DocType, Model, InstanceMethodsAndOverrides, QueryHelpers>) => void;
44
+ }
package/types/schema.d.ts CHANGED
@@ -6,6 +6,8 @@ export type DoNotUseOrRemoveThisType = typeof schemaBuilders;
6
6
  * Base attributes for Mongoose schemas.
7
7
  *
8
8
  * This type defines common attributes that can be applied to any schema field.
9
+ *
10
+ * @deprecated - Will be removed in the next major version.
9
11
  */
10
12
  export type BaseSchemaAttribute = 'private' | 'required' | 'unique';
11
13
  /**
@@ -13,6 +15,8 @@ export type BaseSchemaAttribute = 'private' | 'required' | 'unique';
13
15
  *
14
16
  * This type extends the base schema attributes with string-specific attributes
15
17
  * like `lowercase`, `short`, `trim`, and `uppercase`.
18
+ *
19
+ * @deprecated - Will be removed in the next major version.
16
20
  */
17
21
  export type MongooseStringSchemaAttribute = BaseSchemaAttribute | 'lowercase' | 'short' | 'trim' | 'uppercase';
18
22
  /**
@@ -49,6 +53,8 @@ export interface CreateCommonMongooseSchemasOptions {
49
53
  * including whether the field is private, the reference model name, and standard
50
54
  * schema options like `required` and `unique`.
51
55
  *
56
+ * @deprecated - Will be removed in the next major version.
57
+ *
52
58
  * @template R - The type of the reference model name.
53
59
  * @template T - An array of attributes to apply to the schema field.
54
60
  */
@@ -89,6 +95,8 @@ export interface MongooseObjectIdRefSchema<R extends string, T extends BaseSchem
89
95
  * whether the field should be lowercased, trimmed, or uppercased, as well as standard
90
96
  * schema options like `required` and `unique`.
91
97
  *
98
+ * @deprecated - Will be removed in the next major version.
99
+ *
92
100
  * @template T - An array of attributes to apply to the schema field.
93
101
  */
94
102
  export interface MongooseStringSchema<T extends MongooseStringSchemaAttribute[]> {
package/utils.cjs CHANGED
@@ -7,6 +7,7 @@ const mongooseAggregatePaginate = require('mongoose-aggregate-paginate-v2');
7
7
  const mongoosePaginate = require('mongoose-paginate-v2');
8
8
  const net = require('node:net');
9
9
  const _connections = require('./_connections.cjs');
10
+ const _options = require('./_options.cjs');
10
11
  const options = require('./options.cjs');
11
12
  const normalize = require('./plugins/normalize.cjs');
12
13
 
@@ -32,6 +33,9 @@ const { merge } = lodash__default;
32
33
  * 3. If there's no default connection, create one using the MongoDB URI from the environment variables,
33
34
  * or fallback to a local MongoDB instance.
34
35
  *
36
+ * You can set a function to be executed before the final build by using {@link setCustomMongooseOptions}.
37
+ * However, ensure that this option is configured before calling {@link buildMongooseModel}.
38
+ *
35
39
  * @template DocType - The type of the document.
36
40
  * @template Model - The type of the model, which extends `BaseMongoosePaginateModel`.
37
41
  * @template InstanceMethodsAndOverrides - The type for instance methods and overrides.
@@ -50,7 +54,7 @@ function buildMongooseModel(collection, name, schema, options$1) {
50
54
  schema.plugin(mongooseAggregatePaginate__default);
51
55
  schema.plugin(mongoosePaginate__default);
52
56
  schema.set('timestamps', options$1?.timestamps === undefined ? true : options$1.timestamps);
53
- options.customMongooseOptions.beforeModelBuild?.(schema);
57
+ (_options.customMongooseOptions.beforeModelBuild || options.customMongooseOptions.beforeModelBuild)?.(schema);
54
58
  return (options$1?.connection || _connections.mongooseConnections.default || (_connections.mongooseConnections.default = mongoose__default.createConnection(process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017'))).model(name, schema, collection);
55
59
  }
56
60
  /**
package/utils.d.ts CHANGED
@@ -1,8 +1,9 @@
1
1
  import mongoose from 'mongoose';
2
+ import { setCustomMongooseOptions } from './options';
2
3
  import type { schemaBuilders } from './schema-builders';
3
4
  import type { BuildMongooseModelOptions } from './types/options';
4
5
  import type { BaseSchemaAttribute, CreateCommonMongooseSchemasOptions, MongooseObjectIdRefSchema, MongooseStringSchema, MongooseStringSchemaAttribute } from './types/schema';
5
- export type DoNotUseOrRemoveThisType = typeof schemaBuilders;
6
+ export type DoNotUseOrRemoveThisType = typeof schemaBuilders | typeof setCustomMongooseOptions;
6
7
  /**
7
8
  * Builds and returns a Mongoose model with specified schema and options.
8
9
  *
@@ -15,6 +16,9 @@ export type DoNotUseOrRemoveThisType = typeof schemaBuilders;
15
16
  * 3. If there's no default connection, create one using the MongoDB URI from the environment variables,
16
17
  * or fallback to a local MongoDB instance.
17
18
  *
19
+ * You can set a function to be executed before the final build by using {@link setCustomMongooseOptions}.
20
+ * However, ensure that this option is configured before calling {@link buildMongooseModel}.
21
+ *
18
22
  * @template DocType - The type of the document.
19
23
  * @template Model - The type of the model, which extends `BaseMongoosePaginateModel`.
20
24
  * @template InstanceMethodsAndOverrides - The type for instance methods and overrides.
package/utils.mjs CHANGED
@@ -5,7 +5,8 @@ import mongooseAggregatePaginate from 'mongoose-aggregate-paginate-v2';
5
5
  import mongoosePaginate from 'mongoose-paginate-v2';
6
6
  import net from 'node:net';
7
7
  import { mongooseConnections } from './_connections.mjs';
8
- import { customMongooseOptions } from './options.mjs';
8
+ import { customMongooseOptions } from './_options.mjs';
9
+ import { customMongooseOptions as customMongooseOptions$1 } from './options.mjs';
9
10
  import { mongooseNormalizePlugin } from './plugins/normalize.mjs';
10
11
 
11
12
  const { merge } = lodash;
@@ -21,6 +22,9 @@ const { merge } = lodash;
21
22
  * 3. If there's no default connection, create one using the MongoDB URI from the environment variables,
22
23
  * or fallback to a local MongoDB instance.
23
24
  *
25
+ * You can set a function to be executed before the final build by using {@link setCustomMongooseOptions}.
26
+ * However, ensure that this option is configured before calling {@link buildMongooseModel}.
27
+ *
24
28
  * @template DocType - The type of the document.
25
29
  * @template Model - The type of the model, which extends `BaseMongoosePaginateModel`.
26
30
  * @template InstanceMethodsAndOverrides - The type for instance methods and overrides.
@@ -39,7 +43,7 @@ function buildMongooseModel(collection, name, schema, options) {
39
43
  schema.plugin(mongooseAggregatePaginate);
40
44
  schema.plugin(mongoosePaginate);
41
45
  schema.set('timestamps', options?.timestamps === undefined ? true : options.timestamps);
42
- customMongooseOptions.beforeModelBuild?.(schema);
46
+ (customMongooseOptions.beforeModelBuild || customMongooseOptions$1.beforeModelBuild)?.(schema);
43
47
  return (options?.connection || mongooseConnections.default || (mongooseConnections.default = mongoose.createConnection(process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017'))).model(name, schema, collection);
44
48
  }
45
49
  /**