@kikiutils/mongoose 7.0.3 → 8.0.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.
Files changed (111) hide show
  1. package/README.md +8 -8
  2. package/dist/_internals.d.ts +5 -2
  3. package/dist/_internals.d.ts.map +1 -1
  4. package/dist/_internals.js +2 -1
  5. package/dist/_internals.js.map +1 -1
  6. package/dist/assertions.d.ts +1 -2
  7. package/dist/assertions.d.ts.map +1 -1
  8. package/dist/builders.d.ts +3 -4
  9. package/dist/builders.d.ts.map +1 -1
  10. package/dist/builders.js +2 -3
  11. package/dist/builders.js.map +1 -1
  12. package/dist/connection.d.ts +5 -0
  13. package/dist/connection.d.ts.map +1 -0
  14. package/dist/connection.js +10 -0
  15. package/dist/connection.js.map +1 -0
  16. package/dist/options.d.ts +1 -2
  17. package/dist/options.d.ts.map +1 -1
  18. package/dist/options.js +2 -1
  19. package/dist/options.js.map +1 -1
  20. package/dist/plugins/assertions.d.ts +1 -2
  21. package/dist/plugins/assertions.d.ts.map +1 -1
  22. package/dist/plugins/normalize.d.ts +23 -17
  23. package/dist/plugins/normalize.d.ts.map +1 -1
  24. package/dist/plugins/normalize.js +19 -12
  25. package/dist/plugins/normalize.js.map +1 -1
  26. package/dist/schema-builders/base.d.ts +2 -3
  27. package/dist/schema-builders/base.d.ts.map +1 -1
  28. package/dist/schema-builders/base.js.map +1 -1
  29. package/dist/schema-builders/bigint.d.ts +48 -0
  30. package/dist/schema-builders/bigint.d.ts.map +1 -0
  31. package/dist/schema-builders/bigint.js +36 -0
  32. package/dist/schema-builders/bigint.js.map +1 -0
  33. package/dist/schema-builders/boolean.d.ts +2 -3
  34. package/dist/schema-builders/boolean.d.ts.map +1 -1
  35. package/dist/schema-builders/boolean.js.map +1 -1
  36. package/dist/schema-builders/date.d.ts +2 -3
  37. package/dist/schema-builders/date.d.ts.map +1 -1
  38. package/dist/schema-builders/decimal128.d.ts +33 -15
  39. package/dist/schema-builders/decimal128.d.ts.map +1 -1
  40. package/dist/schema-builders/decimal128.js +23 -0
  41. package/dist/schema-builders/decimal128.js.map +1 -1
  42. package/dist/schema-builders/double.d.ts +48 -0
  43. package/dist/schema-builders/double.d.ts.map +1 -0
  44. package/dist/schema-builders/double.js +37 -0
  45. package/dist/schema-builders/double.js.map +1 -0
  46. package/dist/schema-builders/index.d.ts +6 -1
  47. package/dist/schema-builders/index.js +6 -1
  48. package/dist/schema-builders/int32.d.ts +48 -0
  49. package/dist/schema-builders/int32.d.ts.map +1 -0
  50. package/dist/schema-builders/int32.js +36 -0
  51. package/dist/schema-builders/int32.js.map +1 -0
  52. package/dist/schema-builders/mixed.d.ts +34 -0
  53. package/dist/schema-builders/mixed.d.ts.map +1 -0
  54. package/dist/schema-builders/mixed.js +8 -0
  55. package/dist/schema-builders/mixed.js.map +1 -0
  56. package/dist/schema-builders/number.d.ts +2 -3
  57. package/dist/schema-builders/number.d.ts.map +1 -1
  58. package/dist/schema-builders/object-id.d.ts +2 -3
  59. package/dist/schema-builders/object-id.d.ts.map +1 -1
  60. package/dist/schema-builders/ref.d.ts +2 -3
  61. package/dist/schema-builders/ref.d.ts.map +1 -1
  62. package/dist/schema-builders/string.d.ts +56 -19
  63. package/dist/schema-builders/string.d.ts.map +1 -1
  64. package/dist/schema-builders/string.js +13 -1
  65. package/dist/schema-builders/string.js.map +1 -1
  66. package/dist/schema-builders/uuid.d.ts +34 -0
  67. package/dist/schema-builders/uuid.d.ts.map +1 -0
  68. package/dist/schema-builders/uuid.js +8 -0
  69. package/dist/schema-builders/uuid.js.map +1 -0
  70. package/dist/types/_internals.d.ts +1 -2
  71. package/dist/types/_internals.d.ts.map +1 -1
  72. package/dist/types/aggregate-paginate.d.ts +30 -14
  73. package/dist/types/aggregate-paginate.d.ts.map +1 -1
  74. package/dist/types/data.d.ts +2 -3
  75. package/dist/types/data.d.ts.map +1 -1
  76. package/dist/types/index.d.ts +10 -14
  77. package/dist/types/index.d.ts.map +1 -1
  78. package/dist/types/options.d.ts +4 -4
  79. package/dist/types/options.d.ts.map +1 -1
  80. package/dist/types/paginate.d.ts +15 -11
  81. package/dist/types/paginate.d.ts.map +1 -1
  82. package/package.json +23 -18
  83. package/src/_internals.ts +3 -0
  84. package/src/builders.ts +3 -10
  85. package/src/connection.ts +14 -0
  86. package/src/options.ts +8 -1
  87. package/src/plugins/normalize.ts +23 -15
  88. package/src/schema-builders/base.ts +8 -1
  89. package/src/schema-builders/bigint.ts +134 -0
  90. package/src/schema-builders/boolean.ts +1 -4
  91. package/src/schema-builders/decimal128.ts +92 -12
  92. package/src/schema-builders/double.ts +135 -0
  93. package/src/schema-builders/index.ts +15 -0
  94. package/src/schema-builders/int32.ts +135 -0
  95. package/src/schema-builders/mixed.ts +37 -0
  96. package/src/schema-builders/string.ts +90 -17
  97. package/src/schema-builders/uuid.ts +48 -0
  98. package/src/types/aggregate-paginate.ts +29 -8
  99. package/src/types/index.ts +0 -5
  100. package/src/types/options.ts +3 -0
  101. package/src/types/paginate.ts +6 -2
  102. package/dist/constants.d.ts +0 -6
  103. package/dist/constants.d.ts.map +0 -1
  104. package/dist/constants.js +0 -6
  105. package/dist/constants.js.map +0 -1
  106. package/dist/helpers.d.ts +0 -6
  107. package/dist/helpers.d.ts.map +0 -1
  108. package/dist/helpers.js +0 -9
  109. package/dist/helpers.js.map +0 -1
  110. package/src/constants.ts +0 -3
  111. package/src/helpers.ts +0 -5
@@ -0,0 +1,135 @@
1
+ import { Schema } from 'mongoose';
2
+ import type {
3
+ IndexDirection,
4
+ IndexOptions,
5
+ Types,
6
+ } from 'mongoose';
7
+ import type { Merge } from 'type-fest';
8
+
9
+ import type { Readonlyable } from '../types/_internals';
10
+
11
+ import { createBaseSchemaBuilderFactory } from './base';
12
+
13
+ type DoubleLimit = number | Types.Double;
14
+ type ExtendSchemaBuilder<
15
+ Props extends BaseProps,
16
+ ExtraOmitFields extends string,
17
+ > = Omit<
18
+ DoubleSchemaBuilder<Props, ExtraOmitFields>,
19
+ ExtraOmitFields | keyof Props
20
+ >;
21
+
22
+ interface BaseProps {
23
+ type: typeof Schema.Types.Double;
24
+ }
25
+
26
+ interface DoubleLimitValidator {
27
+ max?: number;
28
+ message: string;
29
+ min?: number;
30
+ type: 'max' | 'min';
31
+ validator: (value: null | Types.Double | undefined) => boolean;
32
+ }
33
+
34
+ export interface DoubleSchemaBuilder<Props extends BaseProps = BaseProps, ExtraOmitFields extends string = never> {
35
+ default: <
36
+ T extends ((this: any, doc: any) => D | null | undefined) | D | null | undefined,
37
+ D extends Types.Double,
38
+ >(value: T) => ExtendSchemaBuilder<
39
+ Merge<Props, { default: T }>,
40
+ ExtraOmitFields
41
+ >;
42
+
43
+ immutable: ExtendSchemaBuilder<Merge<Props, { immutable: true }>, ExtraOmitFields>;
44
+ index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendSchemaBuilder<
45
+ Merge<Props, { index: T }>,
46
+ ExtraOmitFields
47
+ >;
48
+
49
+ max: <
50
+ T extends L | Readonlyable<[L, S]>,
51
+ L extends DoubleLimit,
52
+ S extends string,
53
+ >(value: T) => ExtendSchemaBuilder<
54
+ Merge<Props, DoubleValidationSchema>,
55
+ 'max' | ExtraOmitFields
56
+ >;
57
+
58
+ min: <
59
+ T extends L | Readonlyable<[L, S]>,
60
+ L extends DoubleLimit,
61
+ S extends string,
62
+ >(value: T) => ExtendSchemaBuilder<
63
+ Merge<Props, DoubleValidationSchema>,
64
+ 'min' | ExtraOmitFields
65
+ >;
66
+
67
+ nonRequired: Props;
68
+ private: ExtendSchemaBuilder<Merge<Props, { private: true }>, ExtraOmitFields>;
69
+ required: Merge<Props, { required: true }>;
70
+ sparse: ExtendSchemaBuilder<Merge<Props, { sparse: true }>, ExtraOmitFields>;
71
+ unique: ExtendSchemaBuilder<Merge<Props, { unique: true }>, ExtraOmitFields>;
72
+ }
73
+
74
+ interface DoubleValidationSchema {
75
+ validate: DoubleLimitValidator[];
76
+ }
77
+
78
+ // Constants/Variables
79
+ const defaultMinValidateMessage = 'Path `{PATH}` ({VALUE}) is less than minimum allowed value ({MIN}).';
80
+ const defaultMaxValidateMessage = 'Path `{PATH}` ({VALUE}) is more than maximum allowed value ({MAX}).';
81
+
82
+ // Functions
83
+ const baseBuilderFactory = createBaseSchemaBuilderFactory(Schema.Types.Double);
84
+
85
+ function createLimitValidator(
86
+ type: 'max' | 'min',
87
+ value: DoubleLimit | Readonlyable<[DoubleLimit, string]>,
88
+ ): DoubleLimitValidator {
89
+ const [limit, message] = Array.isArray(value)
90
+ ? value as Readonly<[DoubleLimit, string]>
91
+ : [
92
+ value as DoubleLimit,
93
+ undefined,
94
+ ];
95
+
96
+ const numericLimit = typeof limit === 'number' ? limit : limit.valueOf();
97
+ if (!Number.isFinite(numericLimit)) throw new RangeError(`Double ${type} limit must be finite`);
98
+
99
+ return {
100
+ message: message ?? (type === 'min' ? defaultMinValidateMessage : defaultMaxValidateMessage),
101
+ [type]: numericLimit,
102
+ type,
103
+ validator: (value) => {
104
+ if (value === null || value === undefined) return true;
105
+
106
+ return type === 'min' ? value.valueOf() >= numericLimit : value.valueOf() <= numericLimit;
107
+ },
108
+ };
109
+ }
110
+
111
+ export function doubleSchemaBuilder() {
112
+ const schema: Record<string, any> = {};
113
+ const baseBuilder = baseBuilderFactory(schema);
114
+ return new Proxy(
115
+ baseBuilder,
116
+ {
117
+ get(target, key, receiver) {
118
+ if (key === 'max' || key === 'min') {
119
+ return (value: DoubleLimit | Readonlyable<[DoubleLimit, string]>) => {
120
+ const validator = createLimitValidator(key, value);
121
+ const validators = Array.isArray(schema.validate) ? schema.validate : [];
122
+ schema.validate = [
123
+ ...validators.filter(({ type }) => type !== key),
124
+ validator,
125
+ ];
126
+
127
+ return receiver;
128
+ };
129
+ }
130
+
131
+ return Reflect.get(target, key, receiver);
132
+ },
133
+ },
134
+ ) as DoubleSchemaBuilder;
135
+ }
@@ -1,3 +1,6 @@
1
+ export { bigintSchemaBuilder as bigint } from './bigint';
2
+ export * from './bigint';
3
+
1
4
  export { booleanSchemaBuilder as boolean } from './boolean';
2
5
  export * from './boolean';
3
6
 
@@ -7,6 +10,15 @@ export * from './date';
7
10
  export { decimal128SchemaBuilder as decimal128 } from './decimal128';
8
11
  export * from './decimal128';
9
12
 
13
+ export { doubleSchemaBuilder as double } from './double';
14
+ export * from './double';
15
+
16
+ export { int32SchemaBuilder as int32 } from './int32';
17
+ export * from './int32';
18
+
19
+ export { mixedSchemaBuilder as mixed } from './mixed';
20
+ export * from './mixed';
21
+
10
22
  export { numberSchemaBuilder as number } from './number';
11
23
  export * from './number';
12
24
 
@@ -18,3 +30,6 @@ export * from './ref';
18
30
 
19
31
  export { stringSchemaBuilder as string } from './string';
20
32
  export * from './string';
33
+
34
+ export { uuidSchemaBuilder as uuid } from './uuid';
35
+ export * from './uuid';
@@ -0,0 +1,135 @@
1
+ import { Schema } from 'mongoose';
2
+ import type {
3
+ DefaultType,
4
+ IndexDirection,
5
+ IndexOptions,
6
+ } from 'mongoose';
7
+ import type { Merge } from 'type-fest';
8
+
9
+ import type { Readonlyable } from '../types/_internals';
10
+
11
+ import { createBaseSchemaBuilderFactory } from './base';
12
+
13
+ type ExtendSchemaBuilder<
14
+ Props extends BaseProps,
15
+ ExtraOmitFields extends string,
16
+ > = Omit<
17
+ Int32SchemaBuilder<Props, ExtraOmitFields>,
18
+ ExtraOmitFields | keyof Props
19
+ >;
20
+
21
+ type Int32Limit = number;
22
+
23
+ interface BaseProps {
24
+ type: typeof Schema.Types.Int32;
25
+ }
26
+
27
+ interface Int32LimitValidator {
28
+ max?: number;
29
+ message: string;
30
+ min?: number;
31
+ type: 'max' | 'min';
32
+ validator: (value: null | number | undefined) => boolean;
33
+ }
34
+
35
+ export interface Int32SchemaBuilder<Props extends BaseProps = BaseProps, ExtraOmitFields extends string = never> {
36
+ default: <
37
+ T extends ((this: any, doc: any) => DefaultType<D> | null | undefined) | DefaultType<D> | null | undefined,
38
+ D extends number,
39
+ >(value: T) => ExtendSchemaBuilder<
40
+ Merge<Props, { default: T }>,
41
+ ExtraOmitFields
42
+ >;
43
+
44
+ immutable: ExtendSchemaBuilder<Merge<Props, { immutable: true }>, ExtraOmitFields>;
45
+ index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendSchemaBuilder<
46
+ Merge<Props, { index: T }>,
47
+ ExtraOmitFields
48
+ >;
49
+
50
+ max: <
51
+ T extends L | Readonlyable<[L, S]>,
52
+ L extends Int32Limit,
53
+ S extends string,
54
+ >(value: T) => ExtendSchemaBuilder<
55
+ Merge<Props, Int32ValidationSchema>,
56
+ 'max' | ExtraOmitFields
57
+ >;
58
+
59
+ min: <
60
+ T extends L | Readonlyable<[L, S]>,
61
+ L extends Int32Limit,
62
+ S extends string,
63
+ >(value: T) => ExtendSchemaBuilder<
64
+ Merge<Props, Int32ValidationSchema>,
65
+ 'min' | ExtraOmitFields
66
+ >;
67
+
68
+ nonRequired: Props;
69
+ private: ExtendSchemaBuilder<Merge<Props, { private: true }>, ExtraOmitFields>;
70
+ required: Merge<Props, { required: true }>;
71
+ sparse: ExtendSchemaBuilder<Merge<Props, { sparse: true }>, ExtraOmitFields>;
72
+ unique: ExtendSchemaBuilder<Merge<Props, { unique: true }>, ExtraOmitFields>;
73
+ }
74
+
75
+ interface Int32ValidationSchema {
76
+ validate: Int32LimitValidator[];
77
+ }
78
+
79
+ // Constants/Variables
80
+ const defaultMinValidateMessage = 'Path `{PATH}` ({VALUE}) is less than minimum allowed value ({MIN}).';
81
+ const defaultMaxValidateMessage = 'Path `{PATH}` ({VALUE}) is more than maximum allowed value ({MAX}).';
82
+
83
+ // Functions
84
+ const baseBuilderFactory = createBaseSchemaBuilderFactory(Schema.Types.Int32);
85
+
86
+ function createLimitValidator(
87
+ type: 'max' | 'min',
88
+ value: Int32Limit | Readonlyable<[Int32Limit, string]>,
89
+ ): Int32LimitValidator {
90
+ const [numericLimit, message] = Array.isArray(value)
91
+ ? value as Readonly<[Int32Limit, string]>
92
+ : [
93
+ value as Int32Limit,
94
+ undefined,
95
+ ];
96
+
97
+ if (!Number.isFinite(numericLimit)) throw new RangeError(`Int32 ${type} limit must be finite`);
98
+
99
+ return {
100
+ message: message ?? (type === 'min' ? defaultMinValidateMessage : defaultMaxValidateMessage),
101
+ [type]: numericLimit,
102
+ type,
103
+ validator: (value) => {
104
+ if (value === null || value === undefined) return true;
105
+
106
+ return type === 'min' ? value >= numericLimit : value <= numericLimit;
107
+ },
108
+ };
109
+ }
110
+
111
+ export function int32SchemaBuilder() {
112
+ const schema: Record<string, any> = {};
113
+ const baseBuilder = baseBuilderFactory(schema);
114
+ return new Proxy(
115
+ baseBuilder,
116
+ {
117
+ get(target, key, receiver) {
118
+ if (key === 'max' || key === 'min') {
119
+ return (value: Int32Limit | Readonlyable<[Int32Limit, string]>) => {
120
+ const validator = createLimitValidator(key, value);
121
+ const validators = Array.isArray(schema.validate) ? schema.validate : [];
122
+ schema.validate = [
123
+ ...validators.filter(({ type }) => type !== key),
124
+ validator,
125
+ ];
126
+
127
+ return receiver;
128
+ };
129
+ }
130
+
131
+ return Reflect.get(target, key, receiver);
132
+ },
133
+ },
134
+ ) as Int32SchemaBuilder;
135
+ }
@@ -0,0 +1,37 @@
1
+ import { Schema } from 'mongoose';
2
+ import type {
3
+ IndexDirection,
4
+ IndexOptions,
5
+ } from 'mongoose';
6
+ import type { Merge } from 'type-fest';
7
+
8
+ import { createBaseSchemaBuilderFactory } from './base';
9
+
10
+ type ExtendSchemaBuilder<
11
+ Props extends BaseProps,
12
+ ExtraOmitFields extends string,
13
+ > = Omit<
14
+ MixedSchemaBuilder<Props, ExtraOmitFields>,
15
+ ExtraOmitFields | keyof Props
16
+ >;
17
+
18
+ interface BaseProps {
19
+ type: typeof Schema.Types.Mixed;
20
+ }
21
+
22
+ export interface MixedSchemaBuilder<Props extends BaseProps = BaseProps, ExtraOmitFields extends string = never> {
23
+ default: <T>(value: T) => ExtendSchemaBuilder<Merge<Props, { default: T }>, ExtraOmitFields>;
24
+ immutable: ExtendSchemaBuilder<Merge<Props, { immutable: true }>, ExtraOmitFields>;
25
+ index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendSchemaBuilder<
26
+ Merge<Props, { index: T }>,
27
+ ExtraOmitFields
28
+ >;
29
+
30
+ nonRequired: Props;
31
+ private: ExtendSchemaBuilder<Merge<Props, { private: true }>, ExtraOmitFields>;
32
+ required: Merge<Props, { required: true }>;
33
+ sparse: ExtendSchemaBuilder<Merge<Props, { sparse: true }>, ExtraOmitFields>;
34
+ unique: ExtendSchemaBuilder<Merge<Props, { unique: true }>, ExtraOmitFields>;
35
+ }
36
+
37
+ export const mixedSchemaBuilder = createBaseSchemaBuilderFactory<MixedSchemaBuilder>(Schema.Types.Mixed);
@@ -63,40 +63,45 @@ export interface StringSchemaBuilder<
63
63
  >;
64
64
 
65
65
  /**
66
- * Adds IPv4 validation to the string schema.
67
- * Ensures the string is a valid IPv4 address and trims the input. The validation message can be customized.
66
+ * Adds IPv4 address validation and enables input trimming.
68
67
  *
69
- * @param message - The custom error message to return if validation fails. Defaults to a standard
70
- * IPv4 validation message.
68
+ * @remarks
69
+ * Replaces the configured custom validator and sets `trim` to `true` on this builder.
71
70
  *
72
- * @returns A schema builder with IPv4 validation and the `trim` option enabled.
71
+ * @param message - The validation error message. Defaults to the built-in IPv4 validation message.
72
+ *
73
+ * @returns This instance for chaining.
73
74
  */
74
75
  ipv4: <T extends string = typeof defaultIpv4ValidateMessage>(message?: T) => ExtendSchemaBuilder<
75
76
  Merge<Props, IpSchema<T>>,
76
- 'ipv4' | 'ipv6' | ExtraOmitFields
77
+ 'ipv4' | 'ipv6' | 'url' | ExtraOmitFields
77
78
  >;
78
79
 
79
80
  /**
80
- * Adds IPv6 validation to the string schema.
81
- * Ensures the string is a valid IPv6 address and trims the input. The validation message can be customized.
81
+ * Adds IPv6 address validation and enables input trimming.
82
+ *
83
+ * @remarks
84
+ * Replaces the configured custom validator and sets `trim` to `true` on this builder.
82
85
  *
83
- * @param message - The custom error message to return if validation fails. Defaults to a standard
84
- * IPv6 validation message.
86
+ * @param message - The validation error message. Defaults to the built-in IPv6 validation message.
85
87
  *
86
- * @returns A schema builder with IPv6 validation and the `trim` option enabled.
88
+ * @returns This instance for chaining.
87
89
  */
88
90
  ipv6: <T extends string = typeof defaultIpv6ValidateMessage>(message?: T) => ExtendSchemaBuilder<
89
91
  Merge<Props, IpSchema<T>>,
90
- 'ipv4' | 'ipv6' | ExtraOmitFields
92
+ 'ipv4' | 'ipv6' | 'url' | ExtraOmitFields
91
93
  >;
92
94
 
93
95
  /**
94
- * Sets both the maximum and minimum length of the string.
95
- * This method ensures the string length is exactly or within a specified range.
96
+ * Sets equal minimum and maximum string lengths.
97
+ *
98
+ * @remarks
99
+ * Assigns the same value to `minlength` and `maxlength`, replacing any previously configured limits.
96
100
  *
97
- * @param value - A number representing the exact length or an array specifying [minimum, maximum] length.
101
+ * @param value - The exact length in UTF-16 code units, or a tuple containing the length and a validation
102
+ * error message.
98
103
  *
99
- * @returns A schema builder with both `maxlength` and `minlength` options applied.
104
+ * @returns This instance for chaining.
100
105
  */
101
106
  length: <
102
107
  T extends L | Readonlyable<[L, S]>,
@@ -108,6 +113,26 @@ export interface StringSchemaBuilder<
108
113
  >;
109
114
 
110
115
  lowercase: ExtendSchemaBuilder<Merge<Props, { lowercase: true }>, ExtraOmitFields>;
116
+
117
+ /**
118
+ * Configures regular expression validation for the string field.
119
+ *
120
+ * @remarks
121
+ * Replaces any previously configured `match` option.
122
+ *
123
+ * @param regex - The regular expression used by Mongoose to validate the string.
124
+ * @param message - The validation error message. If omitted, Mongoose uses its default message.
125
+ *
126
+ * @returns This instance for chaining.
127
+ */
128
+ match: <
129
+ T extends RegExp,
130
+ M extends string | undefined = undefined,
131
+ >(regex: T,
132
+ message?: M) => ExtendSchemaBuilder<
133
+ Merge<Props, { match: M extends string ? Readonly<[T, M]> : T }>,
134
+ ExtraOmitFields
135
+ >;
111
136
  maxlength: <
112
137
  T extends L | Readonlyable<[L, S]>,
113
138
  L extends number,
@@ -134,11 +159,35 @@ export interface StringSchemaBuilder<
134
159
  trim: ExtendSchemaBuilder<Merge<Props, { trim: true }>, ExtraOmitFields>;
135
160
  unique: ExtendSchemaBuilder<Merge<Props, { unique: true }>, ExtraOmitFields>;
136
161
  uppercase: ExtendSchemaBuilder<Merge<Props, { uppercase: true }>, ExtraOmitFields>;
162
+
163
+ /**
164
+ * Adds WHATWG URL parseability validation.
165
+ *
166
+ * @remarks
167
+ * Accepts URLs with credentials and non-HTTP schemes. Performs no DNS lookup or stored-value normalization,
168
+ * and does not enable trimming. Replaces the configured custom validator on this builder.
169
+ *
170
+ * @param message - The validation error message. Defaults to the built-in URL validation message.
171
+ *
172
+ * @returns This instance for chaining.
173
+ */
174
+ url: <T extends string = typeof defaultUrlValidateMessage>(message?: T) => ExtendSchemaBuilder<
175
+ Merge<Props, UrlSchema<T>>,
176
+ 'ipv4' | 'ipv6' | 'url' | ExtraOmitFields
177
+ >;
137
178
  }
138
179
 
139
- const baseBuilderFactory = createBaseSchemaBuilderFactory(String);
180
+ interface UrlSchema<T extends string> {
181
+ validate: { message: T; validator: (value: string) => boolean };
182
+ }
183
+
184
+ // Constants/Variables
140
185
  const defaultIpv4ValidateMessage = '`{VALUE}` is not a valid IPv4 address for path `{PATH}`';
141
186
  const defaultIpv6ValidateMessage = '`{VALUE}` is not a valid IPv6 address for path `{PATH}`';
187
+ const defaultUrlValidateMessage = '`{VALUE}` is not a valid URL for path `{PATH}`';
188
+
189
+ // Functions
190
+ const baseBuilderFactory = createBaseSchemaBuilderFactory(String);
142
191
 
143
192
  export function stringSchemaBuilder() {
144
193
  const schema: Record<string, any> = {};
@@ -178,6 +227,30 @@ export function stringSchemaBuilder() {
178
227
  };
179
228
  }
180
229
 
230
+ if (key === 'match') {
231
+ return (regex: RegExp, message?: string) => {
232
+ schema.match = message === undefined
233
+ ? regex
234
+ : [
235
+ regex,
236
+ message,
237
+ ];
238
+
239
+ return receiver;
240
+ };
241
+ }
242
+
243
+ if (key === 'url') {
244
+ return (message: string = defaultUrlValidateMessage) => {
245
+ schema.validate = {
246
+ message,
247
+ validator: (value: string) => URL.canParse(value),
248
+ };
249
+
250
+ return receiver;
251
+ };
252
+ }
253
+
181
254
  return Reflect.get(target, key, receiver);
182
255
  },
183
256
  },
@@ -0,0 +1,48 @@
1
+ import { Schema } from 'mongoose';
2
+ import type {
3
+ IndexDirection,
4
+ IndexOptions,
5
+ Types,
6
+ } from 'mongoose';
7
+ import type { Merge } from 'type-fest';
8
+
9
+ import { createBaseSchemaBuilderFactory } from './base';
10
+
11
+ type ExtendSchemaBuilder<
12
+ Props extends BaseProps,
13
+ ExtraOmitFields extends string,
14
+ > = Omit<
15
+ UUIDSchemaBuilder<Props, ExtraOmitFields>,
16
+ ExtraOmitFields | keyof Props
17
+ >;
18
+
19
+ interface BaseProps {
20
+ type: typeof Schema.Types.UUID;
21
+ }
22
+
23
+ export interface UUIDSchemaBuilder<Props extends BaseProps = BaseProps, ExtraOmitFields extends string = never> {
24
+ default: <
25
+ T extends
26
+ | ((this: any, doc: any) => null | string | Types.UUID | undefined)
27
+ | null
28
+ | string
29
+ | Types.UUID
30
+ | undefined,
31
+ >(
32
+ value: T,
33
+ ) => ExtendSchemaBuilder<Merge<Props, { default: T }>, ExtraOmitFields>;
34
+
35
+ immutable: ExtendSchemaBuilder<Merge<Props, { immutable: true }>, ExtraOmitFields>;
36
+ index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendSchemaBuilder<
37
+ Merge<Props, { index: T }>,
38
+ ExtraOmitFields
39
+ >;
40
+
41
+ nonRequired: Props;
42
+ private: ExtendSchemaBuilder<Merge<Props, { private: true }>, ExtraOmitFields>;
43
+ required: Merge<Props, { required: true }>;
44
+ sparse: ExtendSchemaBuilder<Merge<Props, { sparse: true }>, ExtraOmitFields>;
45
+ unique: ExtendSchemaBuilder<Merge<Props, { unique: true }>, ExtraOmitFields>;
46
+ }
47
+
48
+ export const uuidSchemaBuilder = createBaseSchemaBuilderFactory<UUIDSchemaBuilder>(Schema.Types.UUID);
@@ -49,9 +49,13 @@ export interface AggregatePaginateOptions {
49
49
  page?: number;
50
50
 
51
51
  /**
52
- * If pagination is set to `false`, it will return all docs without adding limit condition.
52
+ * Whether to apply pagination to the aggregation results.
53
53
  *
54
- * @default true
54
+ * @remarks
55
+ * If `false`, the pagination plugin adds no pagination `$skip` or `$limit` stages. Existing pipeline stages
56
+ * still apply.
57
+ *
58
+ * @defaultValue `true`, unless overridden by plugin-wide defaults.
55
59
  */
56
60
  pagination?: boolean;
57
61
  sort?: object | string;
@@ -59,17 +63,34 @@ export interface AggregatePaginateOptions {
59
63
  }
60
64
 
61
65
  export interface AggregatePaginateQueryPopulateOptions {
62
- /** optional query conditions to match */
66
+ /**
67
+ * The query conditions used to filter populated documents.
68
+ */
63
69
  match?: any;
64
- /** optional model to use for population */
70
+
71
+ /**
72
+ * The model or model name used to populate the referenced documents.
73
+ */
65
74
  model?: Model<any> | string;
66
- /** optional query options like sort, limit, etc */
75
+
76
+ /**
77
+ * The query options applied when retrieving populated documents, such as sorting and limits.
78
+ */
67
79
  options?: any;
68
- /** space delimited path(s) to populate */
80
+
81
+ /**
82
+ * The path or space-delimited paths to populate.
83
+ */
69
84
  path: string;
70
- /** deep populate */
85
+
86
+ /**
87
+ * The nested population options applied to the populated documents.
88
+ */
71
89
  populate?: AggregatePaginateQueryPopulateOptions | AggregatePaginateQueryPopulateOptions[];
72
- /** optional fields to select */
90
+
91
+ /**
92
+ * The field projection used when retrieving populated documents.
93
+ */
73
94
  select?: any;
74
95
  }
75
96
 
@@ -1,5 +1,4 @@
1
1
  import type {
2
- Connection,
3
2
  HydratedDocument,
4
3
  QueryWithHelpers,
5
4
  Types,
@@ -55,7 +54,3 @@ export type SetFieldsToObjectId<T, K extends keyof T> = SetFieldType<T, K, Types
55
54
  // @ts-expect-error Ignore this error.
56
55
  // eslint-disable-next-line unused-imports/no-unused-vars
57
56
  export interface BaseMongooseModelStatics<RawDocType, InstanceMethodsAndOverrides = object, QueryHelpers = object> {}
58
-
59
- export interface MongooseConnections {
60
- default?: Connection;
61
- }
@@ -1,5 +1,6 @@
1
1
  import type {
2
2
  Connection,
3
+ ConnectOptions,
3
4
  Schema,
4
5
  SchemaTimestampsConfig,
5
6
  } from 'mongoose';
@@ -30,4 +31,6 @@ export interface CustomMongooseOptions {
30
31
  QueryHelpers
31
32
  >,
32
33
  ) => void;
34
+
35
+ defaultConnectionOptions?: ConnectOptions;
33
36
  }
@@ -91,9 +91,13 @@ export interface PaginateOptions {
91
91
  page?: number;
92
92
 
93
93
  /**
94
- * If pagination is set to `false`, it will return all docs without adding limit condition.
94
+ * Whether to apply pagination to the query results.
95
95
  *
96
- * @default true
96
+ * @remarks
97
+ * If `false`, the pagination plugin does not apply its page-based `skip` or `limit`. Explicit query options
98
+ * and other query conditions still apply.
99
+ *
100
+ * @defaultValue `true`, unless overridden by plugin-wide defaults.
97
101
  */
98
102
  pagination?: boolean;
99
103
  populate?: PopulateOptions | PopulateOptions[] | string | string[];
@@ -1,6 +0,0 @@
1
- import { MongooseConnections } from "./types/index.js";
2
- //#region src/constants.d.ts
3
- declare const mongooseConnections: MongooseConnections;
4
- //#endregion
5
- export { mongooseConnections };
6
- //# sourceMappingURL=constants.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"constants.d.ts","names":[],"sources":["../src/constants.ts"],"mappings":";;cAEa,qBAAqB"}
package/dist/constants.js DELETED
@@ -1,6 +0,0 @@
1
- //#region src/constants.ts
2
- const mongooseConnections = {};
3
- //#endregion
4
- export { mongooseConnections };
5
-
6
- //# sourceMappingURL=constants.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"constants.js","names":[],"sources":["../src/constants.ts"],"sourcesContent":["import type { MongooseConnections } from './types';\n\nexport const mongooseConnections: MongooseConnections = {};\n"],"mappings":";AAEA,MAAa,sBAA2C,CAAC"}
package/dist/helpers.d.ts DELETED
@@ -1,6 +0,0 @@
1
- import { Types } from "mongoose";
2
- //#region src/helpers.d.ts
3
- declare function toObjectIdHexString(id: string | Types.ObjectId): string;
4
- //#endregion
5
- export { toObjectIdHexString };
6
- //# sourceMappingURL=helpers.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"helpers.d.ts","names":[],"sources":["../src/helpers.ts"],"mappings":";;iBAEgB,oBAAoB,aAAa,MAAM"}