@kikiutils/mongoose 1.7.0 → 1.7.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kikiutils/mongoose",
3
- "version": "1.7.0",
3
+ "version": "1.7.1",
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": {
@@ -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,4 @@
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
- }
5
2
  /**
6
3
  * Interface for options used when building a Mongoose model.
7
4
  *
@@ -37,3 +34,6 @@ export interface BuildMongooseModelOptions {
37
34
  */
38
35
  timestamps?: boolean | SchemaTimestampsConfig;
39
36
  }
37
+ export interface CustomMongooseOptions {
38
+ beforeModelBuild?: <DocType, Model extends BaseMongoosePaginateModel<DocType, InstanceMethodsAndOverrides, QueryHelpers>, InstanceMethodsAndOverrides = {}, QueryHelpers = {}>(schema: Schema<DocType, Model, InstanceMethodsAndOverrides, QueryHelpers>) => void;
39
+ }