@kikiutils/mongoose 2.1.3 → 2.1.5

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 (118) hide show
  1. package/{_internals.cjs → dist/_internals.cjs} +1 -0
  2. package/dist/_internals.cjs.map +1 -0
  3. package/{_internals.d.ts → dist/_internals.d.ts} +1 -0
  4. package/dist/_internals.d.ts.map +1 -0
  5. package/{_internals.mjs → dist/_internals.mjs} +1 -0
  6. package/dist/_internals.mjs.map +1 -0
  7. package/{constants.cjs → dist/constants.cjs} +1 -0
  8. package/dist/constants.cjs.map +1 -0
  9. package/{constants.d.ts → dist/constants.d.ts} +1 -0
  10. package/dist/constants.d.ts.map +1 -0
  11. package/{constants.mjs → dist/constants.mjs} +1 -0
  12. package/dist/constants.mjs.map +1 -0
  13. package/{options.cjs → dist/options.cjs} +1 -0
  14. package/dist/options.cjs.map +1 -0
  15. package/{options.d.ts → dist/options.d.ts} +1 -0
  16. package/dist/options.d.ts.map +1 -0
  17. package/{options.mjs → dist/options.mjs} +1 -0
  18. package/dist/options.mjs.map +1 -0
  19. package/{plugins → dist/plugins}/normalize.cjs +1 -0
  20. package/dist/plugins/normalize.cjs.map +1 -0
  21. package/{plugins → dist/plugins}/normalize.d.ts +1 -0
  22. package/dist/plugins/normalize.d.ts.map +1 -0
  23. package/{plugins → dist/plugins}/normalize.mjs +1 -0
  24. package/dist/plugins/normalize.mjs.map +1 -0
  25. package/{schema-builders → dist/schema-builders}/base.cjs +1 -0
  26. package/dist/schema-builders/base.cjs.map +1 -0
  27. package/{schema-builders → dist/schema-builders}/base.d.ts +1 -0
  28. package/dist/schema-builders/base.d.ts.map +1 -0
  29. package/{schema-builders → dist/schema-builders}/base.mjs +1 -0
  30. package/dist/schema-builders/base.mjs.map +1 -0
  31. package/{schema-builders → dist/schema-builders}/boolean.cjs +1 -0
  32. package/dist/schema-builders/boolean.cjs.map +1 -0
  33. package/{schema-builders → dist/schema-builders}/boolean.d.ts +1 -0
  34. package/dist/schema-builders/boolean.d.ts.map +1 -0
  35. package/{schema-builders → dist/schema-builders}/boolean.mjs +1 -0
  36. package/dist/schema-builders/boolean.mjs.map +1 -0
  37. package/{schema-builders → dist/schema-builders}/date.cjs +1 -0
  38. package/dist/schema-builders/date.cjs.map +1 -0
  39. package/{schema-builders → dist/schema-builders}/date.d.ts +1 -0
  40. package/dist/schema-builders/date.d.ts.map +1 -0
  41. package/{schema-builders → dist/schema-builders}/date.mjs +1 -0
  42. package/dist/schema-builders/date.mjs.map +1 -0
  43. package/{schema-builders → dist/schema-builders}/decimal128.cjs +1 -0
  44. package/dist/schema-builders/decimal128.cjs.map +1 -0
  45. package/{schema-builders → dist/schema-builders}/decimal128.d.ts +4 -3
  46. package/dist/schema-builders/decimal128.d.ts.map +1 -0
  47. package/{schema-builders → dist/schema-builders}/decimal128.mjs +1 -0
  48. package/dist/schema-builders/decimal128.mjs.map +1 -0
  49. package/{schema-builders → dist/schema-builders}/index.cjs +1 -0
  50. package/dist/schema-builders/index.cjs.map +1 -0
  51. package/{schema-builders → dist/schema-builders}/index.d.ts +3 -2
  52. package/dist/schema-builders/index.d.ts.map +1 -0
  53. package/{schema-builders → dist/schema-builders}/index.mjs +1 -0
  54. package/dist/schema-builders/index.mjs.map +1 -0
  55. package/{schema-builders → dist/schema-builders}/number.cjs +1 -0
  56. package/dist/schema-builders/number.cjs.map +1 -0
  57. package/{schema-builders → dist/schema-builders}/number.d.ts +1 -0
  58. package/dist/schema-builders/number.d.ts.map +1 -0
  59. package/{schema-builders → dist/schema-builders}/number.mjs +1 -0
  60. package/dist/schema-builders/number.mjs.map +1 -0
  61. package/{schema-builders → dist/schema-builders}/object-id.cjs +1 -0
  62. package/dist/schema-builders/object-id.cjs.map +1 -0
  63. package/{schema-builders → dist/schema-builders}/object-id.d.ts +7 -5
  64. package/dist/schema-builders/object-id.d.ts.map +1 -0
  65. package/{schema-builders → dist/schema-builders}/object-id.mjs +1 -0
  66. package/dist/schema-builders/object-id.mjs.map +1 -0
  67. package/{schema-builders → dist/schema-builders}/ref.cjs +1 -0
  68. package/dist/schema-builders/ref.cjs.map +1 -0
  69. package/{schema-builders → dist/schema-builders}/ref.d.ts +7 -5
  70. package/dist/schema-builders/ref.d.ts.map +1 -0
  71. package/{schema-builders → dist/schema-builders}/ref.mjs +1 -0
  72. package/dist/schema-builders/ref.mjs.map +1 -0
  73. package/{schema-builders → dist/schema-builders}/string.cjs +1 -0
  74. package/dist/schema-builders/string.cjs.map +1 -0
  75. package/{schema-builders → dist/schema-builders}/string.d.ts +1 -0
  76. package/dist/schema-builders/string.d.ts.map +1 -0
  77. package/{schema-builders → dist/schema-builders}/string.mjs +1 -0
  78. package/dist/schema-builders/string.mjs.map +1 -0
  79. package/{types → dist/types}/aggregate-paginate.d.ts +1 -0
  80. package/dist/types/aggregate-paginate.d.ts.map +1 -0
  81. package/dist/types/data.d.ts +79 -0
  82. package/dist/types/data.d.ts.map +1 -0
  83. package/{types → dist/types}/index.d.ts +5 -3
  84. package/dist/types/index.d.ts.map +1 -0
  85. package/{types → dist/types}/options.d.ts +1 -0
  86. package/dist/types/options.d.ts.map +1 -0
  87. package/{types → dist/types}/paginate.d.ts +8 -3
  88. package/dist/types/paginate.d.ts.map +1 -0
  89. package/dist/types/utils.d.ts +3 -0
  90. package/dist/types/utils.d.ts.map +1 -0
  91. package/{utils.cjs → dist/utils.cjs} +1 -0
  92. package/dist/utils.cjs.map +1 -0
  93. package/{utils.d.ts → dist/utils.d.ts} +1 -0
  94. package/dist/utils.d.ts.map +1 -0
  95. package/{utils.mjs → dist/utils.mjs} +1 -0
  96. package/dist/utils.mjs.map +1 -0
  97. package/package.json +22 -18
  98. package/src/_internals.ts +3 -0
  99. package/src/constants.ts +10 -0
  100. package/src/options.ts +14 -0
  101. package/src/plugins/normalize.ts +54 -0
  102. package/src/schema-builders/base.ts +43 -0
  103. package/src/schema-builders/boolean.ts +27 -0
  104. package/src/schema-builders/date.ts +42 -0
  105. package/src/schema-builders/decimal128.ts +98 -0
  106. package/src/schema-builders/index.ts +26 -0
  107. package/src/schema-builders/number.ts +42 -0
  108. package/src/schema-builders/object-id.ts +41 -0
  109. package/src/schema-builders/ref.ts +43 -0
  110. package/src/schema-builders/string.ts +128 -0
  111. package/src/types/aggregate-paginate.ts +89 -0
  112. package/{types/data.d.ts → src/types/data.ts} +12 -5
  113. package/src/types/index.ts +107 -0
  114. package/src/types/options.ts +61 -0
  115. package/src/types/paginate.ts +154 -0
  116. package/src/types/utils.ts +2 -0
  117. package/src/utils.ts +88 -0
  118. package/types/utils.d.ts +0 -1
@@ -0,0 +1,98 @@
1
+ import Decimal from 'decimal.js';
2
+ import { Schema } from 'mongoose';
3
+ import type {
4
+ DefaultType,
5
+ IndexDirection,
6
+ IndexOptions,
7
+ Types,
8
+ } from 'mongoose';
9
+ import type { Merge } from 'type-fest';
10
+
11
+ import type { Readonlyable } from '../types/utils';
12
+
13
+ import { createBaseSchemaBuilderFactory } from './base';
14
+
15
+ export type ExtendDecimal128SchemaBuilder<Props extends BaseProps, ExtraOmitFields extends string> = Omit<Decimal128SchemaBuilder<Props, ExtraOmitFields>, ExtraOmitFields | keyof Props>;
16
+
17
+ interface BaseProps {
18
+ type: typeof Schema.Types.Decimal128;
19
+ }
20
+
21
+ interface ToStringGetterSchema {
22
+ get: (value?: Types.Decimal128) => string | undefined;
23
+ }
24
+
25
+ interface ToStringSetterSchema {
26
+ set: (value: { toString: () => string }) => string;
27
+ }
28
+
29
+ export interface Decimal128SchemaBuilder<Props extends { type: typeof Schema.Types.Decimal128 } = { type: typeof Schema.Types.Decimal128 }, ExtraOmitFields extends string = never> {
30
+ default: <T extends ((this: any, doc: any) => DefaultType<D>) | DefaultType<D> | null, D extends Types.Decimal128>(value: T) => ExtendDecimal128SchemaBuilder<Merge<Props, { default: T }>, ExtraOmitFields>;
31
+ enum: <
32
+ T extends
33
+ | Readonlyable<Array<D | null>>
34
+ | { [path: string]: D | null }
35
+ | { message?: M; values: Readonlyable<Array<D | null>> },
36
+ D extends Types.Decimal128,
37
+ M extends string,
38
+ >(
39
+ value: T
40
+ ) => ExtendDecimal128SchemaBuilder<Merge<Props, { enum: T }>, ExtraOmitFields>;
41
+
42
+ index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendDecimal128SchemaBuilder<Merge<Props, { index: T }>, ExtraOmitFields>;
43
+ nonRequired: Props;
44
+ private: ExtendDecimal128SchemaBuilder<Merge<Props, { private: true }>, ExtraOmitFields>;
45
+ required: Merge<Props, { required: true }>;
46
+
47
+ /**
48
+ * Sets the rounding and toFixed behavior for Decimal128 fields.
49
+ * This method allows specifying the number of decimal places and the rounding strategy to be used when
50
+ * saving the field to the database.
51
+ *
52
+ * @param places - The number of decimal places to round to (default is 2).
53
+ * @param rounding - The rounding strategy from the Decimal.js library (default is Decimal.ROUND_DOWN).
54
+ *
55
+ * @returns A schema builder with the rounding and toFixed behavior applied to the `set` option in Mongoose.
56
+ */
57
+ setRoundAndToFixedSetter: (places?: number, rounding?: Decimal.Rounding) => ExtendDecimal128SchemaBuilder<Merge<Props, ToStringSetterSchema>, 'setRoundAndToFixedSetter' | ExtraOmitFields>;
58
+
59
+ /**
60
+ * Sets the toString behavior for Decimal128 fields.
61
+ * This method defines how the Decimal128 field should be converted to a string when processing or
62
+ * retrieving the value in the application (e.g., via getters), while still storing the value as Decimal128
63
+ * in the database.
64
+ *
65
+ * @returns A schema builder with the custom `get` behavior applied to convert the Decimal128 field to a string
66
+ * when accessed in the application.
67
+ */
68
+ setToStringGetter: ExtendDecimal128SchemaBuilder<Merge<Props, ToStringGetterSchema>, 'setToStringGetter' | ExtraOmitFields>;
69
+ sparse: ExtendDecimal128SchemaBuilder<Merge<Props, { sparse: true }>, ExtraOmitFields>;
70
+ unique: ExtendDecimal128SchemaBuilder<Merge<Props, { unique: true }>, ExtraOmitFields>;
71
+ }
72
+
73
+ const baseBuilderFactory = createBaseSchemaBuilderFactory(Schema.Types.Decimal128);
74
+
75
+ export function decimal128SchemaBuilder() {
76
+ const schema: Record<string, any> = {};
77
+ const baseBuilder = baseBuilderFactory(schema);
78
+ return new Proxy(
79
+ baseBuilder,
80
+ {
81
+ get(target, key, receiver) {
82
+ if (key === 'setRoundAndToFixedSetter') {
83
+ return (places: number = 2, rounding: Decimal.Rounding = Decimal.ROUND_DOWN) => {
84
+ schema.set = (value: { toString: () => string }) => new Decimal(value.toString()).toFixed(places, rounding);
85
+ return receiver;
86
+ };
87
+ }
88
+
89
+ if (key === 'setToStringGetter') {
90
+ schema.get = (value?: Types.Decimal128) => value?.toString();
91
+ return receiver;
92
+ }
93
+
94
+ return Reflect.get(target, key, receiver);
95
+ },
96
+ },
97
+ ) as Decimal128SchemaBuilder;
98
+ }
@@ -0,0 +1,26 @@
1
+ import { booleanSchemaBuilder } from './boolean';
2
+ import { dateSchemaBuilder } from './date';
3
+ import { decimal128SchemaBuilder } from './decimal128';
4
+ import { numberSchemaBuilder } from './number';
5
+ import { objectIdSchemaBuilder } from './object-id';
6
+ import { refSchemaBuilder } from './ref';
7
+ import { stringSchemaBuilder } from './string';
8
+
9
+ export * from './boolean';
10
+ export * from './date';
11
+ export * from './decimal128';
12
+ export * from './number';
13
+ export * from './ref';
14
+ export * from './string';
15
+
16
+ export const schemaBuilders = {
17
+ boolean: booleanSchemaBuilder,
18
+ date: dateSchemaBuilder,
19
+ decimal128: decimal128SchemaBuilder,
20
+ number: numberSchemaBuilder,
21
+ objectId: objectIdSchemaBuilder,
22
+ ref: refSchemaBuilder,
23
+ string: stringSchemaBuilder,
24
+ };
25
+
26
+ export default schemaBuilders;
@@ -0,0 +1,42 @@
1
+ import type {
2
+ DefaultType,
3
+ IndexDirection,
4
+ IndexOptions,
5
+ NumberSchemaDefinition,
6
+ } from 'mongoose';
7
+ import type { Merge } from 'type-fest';
8
+
9
+ import type { Readonlyable } from '../types/utils';
10
+
11
+ import { createBaseSchemaBuilderFactory } from './base';
12
+
13
+ export type ExtendNumberSchemaBuilder<Props extends BaseProps, ExtraOmitFields extends string> = Omit<NumberSchemaBuilder<Props, ExtraOmitFields>, ExtraOmitFields | keyof Props>;
14
+
15
+ interface BaseProps {
16
+ type: NumberSchemaDefinition;
17
+ }
18
+
19
+ export interface NumberSchemaBuilder<Props extends { type: NumberSchemaDefinition } = { type: NumberSchemaDefinition }, ExtraOmitFields extends string = never> {
20
+ default: <T extends ((this: any, doc: any) => DefaultType<D>) | DefaultType<D> | null, D extends number>(value: T) => ExtendNumberSchemaBuilder<Merge<Props, { default: T }>, ExtraOmitFields>;
21
+ enum: <
22
+ T extends
23
+ | Readonlyable<Array<N | null>>
24
+ | { [path: string]: N | null }
25
+ | { message?: M; values: Readonlyable<Array<N | null>> },
26
+ M extends string,
27
+ N extends number,
28
+ >(
29
+ value: T
30
+ ) => ExtendNumberSchemaBuilder<Merge<Props, { enum: T }>, ExtraOmitFields>;
31
+
32
+ index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendNumberSchemaBuilder<Merge<Props, { index: T }>, ExtraOmitFields>;
33
+ max: <T extends N | Readonlyable<[N, S]>, N extends number, S extends string>(value: T) => ExtendNumberSchemaBuilder<Merge<Props, { max: T }>, ExtraOmitFields>;
34
+ min: <T extends N | Readonlyable<[N, S]>, N extends number, S extends string>(value: T) => ExtendNumberSchemaBuilder<Merge<Props, { min: T }>, ExtraOmitFields>;
35
+ nonRequired: Props;
36
+ private: ExtendNumberSchemaBuilder<Merge<Props, { private: true }>, ExtraOmitFields>;
37
+ required: Merge<Props, { required: true }>;
38
+ sparse: ExtendNumberSchemaBuilder<Merge<Props, { sparse: true }>, ExtraOmitFields>;
39
+ unique: ExtendNumberSchemaBuilder<Merge<Props, { unique: true }>, ExtraOmitFields>;
40
+ }
41
+
42
+ export const numberSchemaBuilder = createBaseSchemaBuilderFactory<NumberSchemaBuilder>(Number);
@@ -0,0 +1,41 @@
1
+ import { Schema } from 'mongoose';
2
+ import type {
3
+ DefaultType,
4
+ IndexDirection,
5
+ IndexOptions,
6
+ Types,
7
+ } from 'mongoose';
8
+ import type { Merge } from 'type-fest';
9
+
10
+ import type { Readonlyable } from '../types/utils';
11
+
12
+ import { createBaseSchemaBuilderFactory } from './base';
13
+
14
+ export type ExtendObjectIdSchemaBuilder<Props extends BaseProps, ExtraOmitFields extends string> = Omit<ObjectIdSchemaBuilder<Props, ExtraOmitFields>, ExtraOmitFields | keyof Props>;
15
+
16
+ interface BaseProps {
17
+ type: typeof Schema.Types.ObjectId;
18
+ }
19
+
20
+ export interface ObjectIdSchemaBuilder<Props extends { type: typeof Schema.Types.ObjectId } = { type: typeof Schema.Types.ObjectId }, ExtraOmitFields extends string = never> {
21
+ default: <T extends ((this: any, doc: any) => DefaultType<D>) | DefaultType<D> | null, D extends Types.ObjectId>(value: T) => ExtendObjectIdSchemaBuilder<Merge<Props, { default: T }>, ExtraOmitFields>;
22
+ enum: <
23
+ T extends
24
+ | Readonlyable<Array<null | O>>
25
+ | { [path: string]: null | O }
26
+ | { message?: M; values: Readonlyable<Array<null | O>> },
27
+ M extends string,
28
+ O extends Types.ObjectId,
29
+ >(
30
+ value: T
31
+ ) => ExtendObjectIdSchemaBuilder<Merge<Props, { enum: T }>, ExtraOmitFields>;
32
+
33
+ index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendObjectIdSchemaBuilder<Merge<Props, { index: T }>, ExtraOmitFields>;
34
+ nonRequired: Props;
35
+ private: ExtendObjectIdSchemaBuilder<Merge<Props, { private: true }>, ExtraOmitFields>;
36
+ required: Merge<Props, { required: true }>;
37
+ sparse: ExtendObjectIdSchemaBuilder<Merge<Props, { sparse: true }>, ExtraOmitFields>;
38
+ unique: ExtendObjectIdSchemaBuilder<Merge<Props, { unique: true }>, ExtraOmitFields>;
39
+ }
40
+
41
+ export const objectIdSchemaBuilder = createBaseSchemaBuilderFactory<ObjectIdSchemaBuilder>(Schema.Types.ObjectId);
@@ -0,0 +1,43 @@
1
+ import { Schema } from 'mongoose';
2
+ import type {
3
+ DefaultType,
4
+ IndexDirection,
5
+ IndexOptions,
6
+ Model,
7
+ Types,
8
+ } from 'mongoose';
9
+ import type { Merge } from 'type-fest';
10
+
11
+ import type { Readonlyable } from '../types/utils';
12
+
13
+ import { createBaseSchemaBuilderFactory } from './base';
14
+
15
+ export type ExtendRefSchemaBuilder<Props extends BaseProps, ExtraOmitFields extends string> = Omit<RefSchemaBuilder<Props, ExtraOmitFields>, ExtraOmitFields | keyof Props>;
16
+
17
+ interface BaseProps {
18
+ type: typeof Schema.Types.ObjectId;
19
+ }
20
+
21
+ export interface RefSchemaBuilder<Props extends { type: typeof Schema.Types.ObjectId } = { type: typeof Schema.Types.ObjectId }, ExtraOmitFields extends string = never> {
22
+ default: <T extends ((this: any, doc: any) => DefaultType<D>) | DefaultType<D> | null, D extends Types.ObjectId>(value: T) => ExtendRefSchemaBuilder<Merge<Props, { default: T }>, ExtraOmitFields>;
23
+ enum: <
24
+ T extends
25
+ | Readonlyable<Array<null | O>>
26
+ | { [path: string]: null | O }
27
+ | { message?: M; values: Readonlyable<Array<null | O>> },
28
+ M extends string,
29
+ O extends Types.ObjectId,
30
+ >(
31
+ value: T
32
+ ) => ExtendRefSchemaBuilder<Merge<Props, { enum: T }>, ExtraOmitFields>;
33
+
34
+ index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendRefSchemaBuilder<Merge<Props, { index: T }>, ExtraOmitFields>;
35
+ nonRequired: Props;
36
+ private: ExtendRefSchemaBuilder<Merge<Props, { private: true }>, ExtraOmitFields>;
37
+ required: Merge<Props, { required: true }>;
38
+ sparse: ExtendRefSchemaBuilder<Merge<Props, { sparse: true }>, ExtraOmitFields>;
39
+ unique: ExtendRefSchemaBuilder<Merge<Props, { unique: true }>, ExtraOmitFields>;
40
+ }
41
+
42
+ const baseBuilderFactory = createBaseSchemaBuilderFactory(Schema.Types.ObjectId);
43
+ export const refSchemaBuilder = <T extends ((this: any, doc: any) => Model<any> | string) | Model<any> | string>(ref: T) => baseBuilderFactory({ ref }) as RefSchemaBuilder<{ ref: T; type: typeof Schema.Types.ObjectId }>;
@@ -0,0 +1,128 @@
1
+ import type {
2
+ DefaultType,
3
+ IndexDirection,
4
+ IndexOptions,
5
+ StringSchemaDefinition,
6
+ } from 'mongoose';
7
+ import net from 'node:net';
8
+ import type { Merge } from 'type-fest';
9
+
10
+ import type { Readonlyable } from '../types/utils';
11
+
12
+ import { createBaseSchemaBuilderFactory } from './base';
13
+
14
+ export type ExtendStringSchemaBuilder<Props extends BaseProps, ExtraOmitFields extends string> = Omit<StringSchemaBuilder<Props, ExtraOmitFields>, ExtraOmitFields | keyof Props>;
15
+
16
+ interface BaseProps {
17
+ type: StringSchemaDefinition;
18
+ }
19
+
20
+ interface IPSchema<T extends string> {
21
+ trim: true;
22
+ validate: { message: T; validator: (value: string) => boolean };
23
+ }
24
+
25
+ export interface StringSchemaBuilder<Props extends { type: StringSchemaDefinition } = { type: StringSchemaDefinition }, ExtraOmitFields extends string = never> {
26
+ default: <T extends ((this: any, doc: any) => DefaultType<D>) | DefaultType<D> | null, D extends string>(value: T) => ExtendStringSchemaBuilder<Merge<Props, { default: T }>, ExtraOmitFields>;
27
+ enum: <
28
+ T extends
29
+ | Readonlyable<Array<null | S>>
30
+ | { [path: string]: null | S }
31
+ | { message?: M; values: Readonlyable<Array<null | S>> },
32
+ M extends string,
33
+ S extends string,
34
+ >(
35
+ value: T
36
+ ) => ExtendStringSchemaBuilder<Merge<Props, { enum: T }>, ExtraOmitFields>;
37
+
38
+ index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendStringSchemaBuilder<Merge<Props, { index: T }>, ExtraOmitFields>;
39
+
40
+ /**
41
+ * Adds IPv4 validation to the string schema.
42
+ * Ensures the string is a valid IPv4 address and trims the input. The validation message can be customized.
43
+ *
44
+ * @param message - The custom error message to return if validation fails. Defaults to a standard IPv4 validation message.
45
+ *
46
+ * @returns A schema builder with IPv4 validation and the `trim` option enabled.
47
+ */
48
+ ipv4: <T extends string = typeof defaultIPv4ValidateMessage>(message?: T) => ExtendStringSchemaBuilder<Merge<Props, IPSchema<T>>, 'ipv4' | 'ipv6' | ExtraOmitFields>;
49
+
50
+ /**
51
+ * Adds IPv6 validation to the string schema.
52
+ * Ensures the string is a valid IPv6 address and trims the input. The validation message can be customized.
53
+ *
54
+ * @param message - The custom error message to return if validation fails. Defaults to a standard IPv6 validation message.
55
+ *
56
+ * @returns A schema builder with IPv6 validation and the `trim` option enabled.
57
+ */
58
+ ipv6: <T extends string = typeof defaultIPv6ValidateMessage>(message?: T) => ExtendStringSchemaBuilder<Merge<Props, IPSchema<T>>, 'ipv4' | 'ipv6' | ExtraOmitFields>;
59
+
60
+ /**
61
+ * Sets both the maximum and minimum length of the string.
62
+ * This method ensures the string length is exactly or within a specified range.
63
+ *
64
+ * @param value - A number representing the exact length or an array specifying [minimum, maximum] length.
65
+ *
66
+ * @returns A schema builder with both `maxlength` and `minlength` options applied.
67
+ */
68
+ length: <T extends L | Readonlyable<[L, S]>, L extends number, S extends string>(value: T) => ExtendStringSchemaBuilder<Merge<Props, { maxlength: T; minlength: T }>, ExtraOmitFields>;
69
+ lowercase: ExtendStringSchemaBuilder<Merge<Props, { lowercase: true }>, ExtraOmitFields>;
70
+ maxlength: <T extends L | Readonlyable<[L, S]>, L extends number, S extends string>(value: T) => ExtendStringSchemaBuilder<Merge<Props, { maxlength: T }>, ExtraOmitFields>;
71
+ minlength: <T extends L | Readonlyable<[L, S]>, L extends number, S extends string>(value: T) => ExtendStringSchemaBuilder<Merge<Props, { minlength: T }>, ExtraOmitFields>;
72
+ nonRequired: Props;
73
+ private: ExtendStringSchemaBuilder<Merge<Props, { private: true }>, ExtraOmitFields>;
74
+ required: Merge<Props, { required: true }>;
75
+ sparse: ExtendStringSchemaBuilder<Merge<Props, { sparse: true }>, ExtraOmitFields>;
76
+ text: ExtendStringSchemaBuilder<Merge<Props, { text: true }>, ExtraOmitFields>;
77
+ trim: ExtendStringSchemaBuilder<Merge<Props, { trim: true }>, ExtraOmitFields>;
78
+ unique: ExtendStringSchemaBuilder<Merge<Props, { unique: true }>, ExtraOmitFields>;
79
+ uppercase: ExtendStringSchemaBuilder<Merge<Props, { uppercase: true }>, ExtraOmitFields>;
80
+ }
81
+
82
+ const baseBuilderFactory = createBaseSchemaBuilderFactory(String);
83
+ const defaultIPv4ValidateMessage = '`{VALUE}` is not a valid IPv4 address for path `{PATH}`.';
84
+ const defaultIPv6ValidateMessage = '`{VALUE}` is not a valid IPv6 address for path `{PATH}`.';
85
+
86
+ export function stringSchemaBuilder() {
87
+ const schema: Record<string, any> = {};
88
+ const baseBuilder = baseBuilderFactory(schema);
89
+ return new Proxy(
90
+ baseBuilder,
91
+ {
92
+ get(target, key, receiver) {
93
+ if (key === 'ipv4') {
94
+ return (message: string = defaultIPv4ValidateMessage) => {
95
+ schema.trim = true;
96
+ schema.validate = {
97
+ message,
98
+ validator: (value: string) => net.isIPv4(value),
99
+ };
100
+
101
+ return receiver;
102
+ };
103
+ }
104
+
105
+ if (key === 'ipv6') {
106
+ return (message: string = defaultIPv6ValidateMessage) => {
107
+ schema.trim = true;
108
+ schema.validate = {
109
+ message,
110
+ validator: (value: string) => net.isIPv6(value),
111
+ };
112
+
113
+ return receiver;
114
+ };
115
+ }
116
+
117
+ if (key === 'length') {
118
+ return (value: any) => {
119
+ schema.maxlength = schema.minlength = value;
120
+ return receiver;
121
+ };
122
+ }
123
+
124
+ return Reflect.get(target, key, receiver);
125
+ },
126
+ },
127
+ ) as StringSchemaBuilder;
128
+ }
@@ -0,0 +1,89 @@
1
+ //
2
+ // Based on type declarations for mongoose-paginate-v2 1.3.
3
+ //
4
+ // Thanks to knyuwork <https://github.com/knyuwork>
5
+ // and LiRen Tu <https://github.com/tuliren> for their contribution
6
+ // Used with mongoose-paginate, redefined and renamed here to avoid conflicts.
7
+
8
+ import type {
9
+ AggregatePaginateOptions,
10
+ Schema,
11
+ } from 'mongoose';
12
+
13
+ declare module 'mongoose' {
14
+ interface AggregateCustomLabels<T = boolean | string | undefined> {
15
+ docs?: T;
16
+ hasNextPage?: T;
17
+ hasPrevPage?: T;
18
+ limit?: T;
19
+ meta?: T;
20
+ nextPage?: T;
21
+ page?: T;
22
+ pagingCounter?: T;
23
+ prevPage?: T;
24
+ totalDocs?: T;
25
+ totalPages?: T;
26
+ }
27
+
28
+ interface AggregatePaginateOptions {
29
+ allowDiskUse?: boolean;
30
+ countQuery?: object;
31
+ customLabels?: AggregateCustomLabels;
32
+ limit?: number;
33
+ offset?: number;
34
+ page?: number;
35
+
36
+ /**
37
+ * If pagination is set to `false`, it will return all docs without adding limit condition.
38
+ *
39
+ * @default true
40
+ */
41
+ pagination?: boolean;
42
+ sort?: object | string;
43
+ useFacet?: boolean;
44
+ }
45
+
46
+ interface AggregatePaginateQueryPopulateOptions {
47
+ /** optional query conditions to match */
48
+ match?: any;
49
+ /** optional model to use for population */
50
+ model?: Model<any> | string;
51
+ /** optional query options like sort, limit, etc */
52
+ options?: any;
53
+ /** space delimited path(s) to populate */
54
+ path: string;
55
+ /** deep populate */
56
+ populate?: AggregatePaginateQueryPopulateOptions | AggregatePaginateQueryPopulateOptions[];
57
+ /** optional fields to select */
58
+ select?: any;
59
+ }
60
+
61
+ interface AggregatePaginateResult<T> {
62
+ [customLabel: string]: boolean | null | number | T[] | undefined;
63
+ docs: T[];
64
+ hasNextPage: boolean;
65
+ hasPrevPage: boolean;
66
+ limit: number;
67
+ meta?: any;
68
+ nextPage?: null | number;
69
+ page?: number;
70
+ pagingCounter: number;
71
+ prevPage?: null | number;
72
+ totalDocs: number;
73
+ totalPages: number;
74
+ }
75
+
76
+ interface AggregatePaginateModel<RawDocType, QueryHelpers = object, InstanceMethodsAndOverrides = object> extends Model<RawDocType, QueryHelpers, InstanceMethodsAndOverrides> {
77
+ aggregatePaginate: <T>(query?: Aggregate<T[]>, options?: AggregatePaginateOptions, callback?: (err: any, result: AggregatePaginateResult<T>) => void) => Promise<AggregatePaginateResult<T>>;
78
+ }
79
+ }
80
+
81
+ declare function mongooseAggregatePaginate(schema: Schema): void;
82
+
83
+ // eslint-disable-next-line ts/no-namespace
84
+ declare namespace mongooseAggregatePaginate {
85
+ const PREPAGINATION_PLACEHOLDER: string;
86
+ const aggregatePaginate: { options: AggregatePaginateOptions };
87
+ }
88
+
89
+ export default mongooseAggregatePaginate;
@@ -1,3 +1,5 @@
1
+ import type { IfElse } from './utils';
2
+
1
3
  /**
2
4
  * Interface for the base data of a Mongoose model, optionally including timestamp fields.
3
5
  *
@@ -11,6 +13,7 @@
11
13
  interface LocalBaseMongooseModelData<CreatedAtField extends boolean = true, UpdatedAtField extends boolean = true> extends LocalWithTimestampFieldsData<CreatedAtField, UpdatedAtField> {
12
14
  id: string;
13
15
  }
16
+
14
17
  /**
15
18
  * Interface for including timestamp fields conditionally based on the provided flags.
16
19
  *
@@ -22,9 +25,10 @@ interface LocalBaseMongooseModelData<CreatedAtField extends boolean = true, Upda
22
25
  * @template UpdatedAt - A boolean flag indicating whether the `updatedAt` field should be included.
23
26
  */
24
27
  interface LocalWithTimestampFieldsData<CreatedAt extends boolean = true, UpdatedAt extends boolean = true> {
25
- createdAt: CreatedAt extends true ? string : never;
26
- updatedAt: UpdatedAt extends true ? string : never;
28
+ createdAt: IfElse<CreatedAt, string, never>;
29
+ updatedAt: IfElse<UpdatedAt, string, never>;
27
30
  }
31
+
28
32
  /**
29
33
  * Utility type to omit properties that are set to `never` in a given type.
30
34
  *
@@ -33,9 +37,8 @@ interface LocalWithTimestampFieldsData<CreatedAt extends boolean = true, Updated
33
37
  *
34
38
  * @template T - The type from which to omit `never` properties.
35
39
  */
36
- type OmitNever<T> = {
37
- [P in keyof T as T[P] extends never ? never : P]: T[P];
38
- };
40
+ type OmitNever<T> = { [P in keyof T as IfElse<T[P], never, P, never>]: T[P] };
41
+
39
42
  declare global {
40
43
  /**
41
44
  * Type definition for the base data of a Mongoose model, optionally including timestamp fields.
@@ -47,6 +50,7 @@ declare global {
47
50
  * @template UpdatedAtField - A boolean flag indicating whether the `updatedAt` field should be included.
48
51
  */
49
52
  type BaseMongooseModelData<CreatedAtField extends boolean = true, UpdatedAtField extends boolean = true> = OmitNever<LocalBaseMongooseModelData<CreatedAtField, UpdatedAtField>>;
53
+
50
54
  /**
51
55
  * Type definition to omit timestamp fields (`createdAt`, `updatedAt`)
52
56
  * and other specified fields (`OT`) from a given type `T`.
@@ -55,6 +59,7 @@ declare global {
55
59
  * @template OT - Additional fields to omit, specified as a union of keys in `T`.
56
60
  */
57
61
  type ExceptMongooseTimestampAndOtherFields<T, OT extends keyof T> = Omit<T, 'createdAt' | 'updatedAt' | OT>;
62
+
58
63
  /**
59
64
  * Type definition to omit timestamp fields (`createdAt`, `updatedAt`)
60
65
  * and other specified fields (`OT`) from a given type `T`.
@@ -63,6 +68,7 @@ declare global {
63
68
  * @template OT - Additional fields to omit, specified as a union of string, number, or symbol keys.
64
69
  */
65
70
  type OmitMongooseTimestampAndOtherFields<T, OT extends number | string | symbol = never> = Omit<T, 'createdAt' | 'updatedAt' | OT>;
71
+
66
72
  /**
67
73
  * Type definition for including timestamp fields conditionally based on the provided flags.
68
74
  *
@@ -74,4 +80,5 @@ declare global {
74
80
  */
75
81
  type WithMongooseTimestampFieldsData<CreatedAt extends boolean = true, UpdatedAt extends boolean = true> = OmitNever<LocalWithTimestampFieldsData<CreatedAt, UpdatedAt>>;
76
82
  }
83
+
77
84
  export {};
@@ -0,0 +1,107 @@
1
+ import type {
2
+ AggregatePaginateModel,
3
+ Connection,
4
+ HydratedDocument,
5
+ PaginateModel,
6
+ QueryWithHelpers,
7
+ Types,
8
+ } from 'mongoose';
9
+
10
+ import type { IfElse } from './utils';
11
+
12
+ export type {} from './aggregate-paginate';
13
+ export type {} from './paginate';
14
+
15
+ // @ts-expect-error Ignore this error.
16
+ // eslint-disable-next-line unused-imports/no-unused-vars
17
+ export interface BaseModelStatics<RawDocType, InstanceMethodsAndOverrides = object, QueryHelpers = object> {}
18
+
19
+ export interface MongooseConnections {
20
+ default?: Connection;
21
+ }
22
+
23
+ declare global {
24
+ /**
25
+ * Type definition for a base Mongoose document type.
26
+ *
27
+ * This type alias omits the `createdAt`, `id`, and `updatedAt` fields from type `T`,
28
+ * and conditionally includes `createdAt` and `updatedAt` fields as `Date` based on
29
+ * the `CreatedAtField` and `UpdatedAtField` flags.
30
+ *
31
+ * @template T - The original document type.
32
+ * @template CreatedAtField - A boolean flag indicating whether the `createdAt` field should be included.
33
+ * @template UpdatedAtField - A boolean flag indicating whether the `updatedAt` field should be included.
34
+ */
35
+ type BaseMongooseDocType<T, CreatedAtField extends boolean = true, UpdatedAtField extends boolean = true> =
36
+ & IfElse<CreatedAtField, { createdAt: Date }, object>
37
+ & IfElse<UpdatedAtField, { updatedAt: Date }, object>
38
+ & Omit<T, 'createdAt' | 'id' | 'updatedAt'>;
39
+
40
+ /**
41
+ * Type definition for a Mongoose model with pagination and aggregation capabilities.
42
+ *
43
+ * This type alias combines the `AggregatePaginateModel` and `PaginateModel` interfaces,
44
+ * along with custom static methods defined in `BaseModelStatics`.
45
+ *
46
+ * @template RawDocType - The raw document type as stored in MongoDB.
47
+ * @template InstanceMethodsAndOverrides - Optional type parameter for instance methods and overrides.
48
+ * @template QueryHelpers - Optional type parameter for additional query helper methods.
49
+ */
50
+ type BaseMongoosePaginateModel<RawDocType, InstanceMethodsAndOverrides = object, QueryHelpers = object> =
51
+ & AggregatePaginateModel<RawDocType, QueryHelpers, InstanceMethodsAndOverrides>
52
+ & BaseModelStatics<RawDocType, InstanceMethodsAndOverrides, QueryHelpers>
53
+ & PaginateModel<RawDocType, QueryHelpers, InstanceMethodsAndOverrides>;
54
+
55
+ /**
56
+ * Type definition for a Mongoose document or ObjectId.
57
+ *
58
+ * This type alias represents a value that can be either a Mongoose document, a string,
59
+ * or an ObjectId. It is useful when working with Mongoose models where you might have
60
+ * references to documents that are stored either as objects or as ObjectIds.
61
+ *
62
+ * @template D - The Mongoose document type.
63
+ */
64
+ type MongooseDocumentOrObjectId<D> = D | string | Types.ObjectId;
65
+
66
+ /**
67
+ * Type definition for the return type of a Mongoose `findOne` query.
68
+ *
69
+ * This type alias represents the return type of a `findOne` query in Mongoose.
70
+ * It extends the `QueryWithHelpers` type, indicating that the query can return
71
+ * either a document of type `DocType` or `null` if no document is found. The
72
+ * `QueryHelpers` type parameter allows for the inclusion of additional query
73
+ * helper methods.
74
+ *
75
+ * @template RawDocType - The raw document type as stored in MongoDB.
76
+ * @template DocType - The Mongoose document type after applying schema transformations.
77
+ * @template QueryHelpers - Optional type parameter for additional query helper methods.
78
+ * @template InstanceMethodsAndOverrides - Optional type parameter for specifying custom instance methods and overrides on the document.
79
+ */
80
+ type MongooseFindOneReturnType<
81
+ RawDocType,
82
+ DocType,
83
+ QueryHelpers = object,
84
+ InstanceMethodsAndOverrides = object,
85
+ > = QueryWithHelpers<
86
+ DocType | null,
87
+ DocType,
88
+ QueryHelpers,
89
+ RawDocType,
90
+ 'findOne',
91
+ InstanceMethodsAndOverrides
92
+ >;
93
+
94
+ /**
95
+ * Type definition for a hydrated Mongoose document.
96
+ *
97
+ * This type alias represents a hydrated document in Mongoose, which is a fully
98
+ * populated document that includes instance methods and query helpers. It extends
99
+ * the `HydratedDocument` type from Mongoose, allowing for additional instance
100
+ * methods and query helpers to be specified.
101
+ *
102
+ * @template DocType - The Mongoose document type after applying schema transformations.
103
+ * @template InstanceMethodsAndOverrides - Optional type parameter for instance methods and overrides.
104
+ * @template QueryHelpers - Optional type parameter for additional query helper methods.
105
+ */
106
+ type MongooseHydratedDocument<DocType, InstanceMethodsAndOverrides = object, QueryHelpers = object> = HydratedDocument<DocType, InstanceMethodsAndOverrides, QueryHelpers>;
107
+ }