@kikiutils/mongoose 2.1.8 → 2.3.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/dist/options.cjs +3 -1
- package/dist/options.cjs.map +1 -1
- package/dist/options.d.ts +1 -1
- package/dist/options.d.ts.map +1 -1
- package/dist/options.mjs +3 -1
- package/dist/options.mjs.map +1 -1
- package/dist/plugins/normalize.cjs +2 -1
- package/dist/plugins/normalize.cjs.map +1 -1
- package/dist/plugins/normalize.d.ts.map +1 -1
- package/dist/plugins/normalize.mjs +2 -1
- package/dist/plugins/normalize.mjs.map +1 -1
- package/dist/schema-builders/base.cjs.map +1 -1
- package/dist/schema-builders/base.d.ts +3 -1
- package/dist/schema-builders/base.d.ts.map +1 -1
- package/dist/schema-builders/base.mjs.map +1 -1
- package/dist/schema-builders/boolean.cjs.map +1 -1
- package/dist/schema-builders/boolean.d.ts.map +1 -1
- package/dist/schema-builders/boolean.mjs.map +1 -1
- package/dist/schema-builders/date.cjs.map +1 -1
- package/dist/schema-builders/date.d.ts.map +1 -1
- package/dist/schema-builders/date.mjs.map +1 -1
- package/dist/schema-builders/decimal128.cjs +4 -1
- package/dist/schema-builders/decimal128.cjs.map +1 -1
- package/dist/schema-builders/decimal128.d.ts.map +1 -1
- package/dist/schema-builders/decimal128.mjs +4 -1
- package/dist/schema-builders/decimal128.mjs.map +1 -1
- package/dist/schema-builders/index.d.ts +2 -4
- package/dist/schema-builders/index.d.ts.map +1 -1
- package/dist/schema-builders/number.cjs.map +1 -1
- package/dist/schema-builders/number.d.ts.map +1 -1
- package/dist/schema-builders/number.mjs.map +1 -1
- package/dist/schema-builders/object-id.cjs.map +1 -1
- package/dist/schema-builders/object-id.d.ts.map +1 -1
- package/dist/schema-builders/object-id.mjs.map +1 -1
- package/dist/schema-builders/ref.cjs +3 -1
- package/dist/schema-builders/ref.cjs.map +1 -1
- package/dist/schema-builders/ref.d.ts +1 -1
- package/dist/schema-builders/ref.d.ts.map +1 -1
- package/dist/schema-builders/ref.mjs +3 -1
- package/dist/schema-builders/ref.mjs.map +1 -1
- package/dist/schema-builders/string.cjs.map +1 -1
- package/dist/schema-builders/string.d.ts +4 -2
- package/dist/schema-builders/string.d.ts.map +1 -1
- package/dist/schema-builders/string.mjs.map +1 -1
- package/dist/types/aggregate-paginate.d.ts.map +1 -1
- package/dist/types/data.d.ts.map +1 -1
- package/dist/types/index.d.ts +2 -1
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/options.d.ts +7 -4
- package/dist/types/options.d.ts.map +1 -1
- package/dist/types/paginate.d.ts.map +1 -1
- package/dist/types/utils.d.ts.map +1 -1
- package/dist/utils.cjs +30 -3
- package/dist/utils.cjs.map +1 -1
- package/dist/utils.d.ts +16 -0
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.mjs +30 -4
- package/dist/utils.mjs.map +1 -1
- package/package.json +7 -7
- package/src/options.ts +6 -1
- package/src/plugins/normalize.ts +4 -1
- package/src/schema-builders/base.ts +8 -1
- package/src/schema-builders/boolean.ts +21 -4
- package/src/schema-builders/date.ts +43 -9
- package/src/schema-builders/decimal128.ts +41 -10
- package/src/schema-builders/number.ts +39 -6
- package/src/schema-builders/object-id.ts +21 -4
- package/src/schema-builders/ref.ts +27 -5
- package/src/schema-builders/string.ts +61 -12
- package/src/types/aggregate-paginate.ts +5 -1
- package/src/types/data.ts +16 -4
- package/src/types/index.ts +7 -2
- package/src/types/options.ts +7 -4
- package/src/types/paginate.ts +27 -6
- package/src/types/utils.ts +7 -1
- package/src/utils.ts +35 -3
|
@@ -10,14 +10,27 @@ import type { Readonlyable } from '../types/utils';
|
|
|
10
10
|
|
|
11
11
|
import { createBaseSchemaBuilderFactory } from './base';
|
|
12
12
|
|
|
13
|
-
export type ExtendDateSchemaBuilder<
|
|
13
|
+
export type ExtendDateSchemaBuilder<
|
|
14
|
+
Props extends BaseProps,
|
|
15
|
+
ExtraOmitFields extends string,
|
|
16
|
+
> = Omit<DateSchemaBuilder<Props, ExtraOmitFields>, ExtraOmitFields | keyof Props>;
|
|
14
17
|
|
|
15
18
|
interface BaseProps {
|
|
16
19
|
type: DateSchemaDefinition;
|
|
17
20
|
}
|
|
18
21
|
|
|
19
|
-
export interface DateSchemaBuilder<
|
|
20
|
-
|
|
22
|
+
export interface DateSchemaBuilder<
|
|
23
|
+
Props extends { type: DateSchemaDefinition } = { type: DateSchemaDefinition },
|
|
24
|
+
ExtraOmitFields extends string = never,
|
|
25
|
+
> {
|
|
26
|
+
default: <
|
|
27
|
+
T extends ((this: any, doc: any) => DefaultType<D>) | DefaultType<D> | null,
|
|
28
|
+
D extends NativeDate,
|
|
29
|
+
>(value: T) => ExtendDateSchemaBuilder<
|
|
30
|
+
Merge<Props, { default: T }>,
|
|
31
|
+
ExtraOmitFields
|
|
32
|
+
>;
|
|
33
|
+
|
|
21
34
|
enum: <
|
|
22
35
|
T extends
|
|
23
36
|
| Readonlyable<Array<D | null>>
|
|
@@ -25,13 +38,34 @@ export interface DateSchemaBuilder<Props extends { type: DateSchemaDefinition }
|
|
|
25
38
|
| { message?: M; values: Readonlyable<Array<D | null>> },
|
|
26
39
|
D extends NativeDate,
|
|
27
40
|
M extends string,
|
|
28
|
-
>(
|
|
29
|
-
|
|
30
|
-
|
|
41
|
+
>(value: T) => ExtendDateSchemaBuilder<
|
|
42
|
+
Merge<Props, { enum: T }>,
|
|
43
|
+
ExtraOmitFields
|
|
44
|
+
>;
|
|
45
|
+
|
|
46
|
+
index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendDateSchemaBuilder<
|
|
47
|
+
Merge<Props, { index: T }>,
|
|
48
|
+
ExtraOmitFields
|
|
49
|
+
>;
|
|
50
|
+
|
|
51
|
+
max: <
|
|
52
|
+
T extends D | Readonlyable<[D, S]>,
|
|
53
|
+
D extends NativeDate,
|
|
54
|
+
S extends string,
|
|
55
|
+
>(value: T) => ExtendDateSchemaBuilder<
|
|
56
|
+
Merge<Props, { max: T }>,
|
|
57
|
+
ExtraOmitFields
|
|
58
|
+
>;
|
|
59
|
+
|
|
60
|
+
min: <
|
|
61
|
+
T extends D | Readonlyable<[D, S]>,
|
|
62
|
+
D extends NativeDate,
|
|
63
|
+
S extends string,
|
|
64
|
+
>(value: T) => ExtendDateSchemaBuilder<
|
|
65
|
+
Merge<Props, { min: T }>,
|
|
66
|
+
ExtraOmitFields
|
|
67
|
+
>;
|
|
31
68
|
|
|
32
|
-
index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendDateSchemaBuilder<Merge<Props, { index: T }>, ExtraOmitFields>;
|
|
33
|
-
max: <T extends D | Readonlyable<[D, S]>, D extends NativeDate, S extends string>(value: T) => ExtendDateSchemaBuilder<Merge<Props, { max: T }>, ExtraOmitFields>;
|
|
34
|
-
min: <T extends D | Readonlyable<[D, S]>, D extends NativeDate, S extends string>(value: T) => ExtendDateSchemaBuilder<Merge<Props, { min: T }>, ExtraOmitFields>;
|
|
35
69
|
nonRequired: Props;
|
|
36
70
|
private: ExtendDateSchemaBuilder<Merge<Props, { private: true }>, ExtraOmitFields>;
|
|
37
71
|
required: Merge<Props, { required: true }>;
|
|
@@ -12,7 +12,10 @@ import type { Readonlyable } from '../types/utils';
|
|
|
12
12
|
|
|
13
13
|
import { createBaseSchemaBuilderFactory } from './base';
|
|
14
14
|
|
|
15
|
-
export type ExtendDecimal128SchemaBuilder<
|
|
15
|
+
export type ExtendDecimal128SchemaBuilder<
|
|
16
|
+
Props extends BaseProps,
|
|
17
|
+
ExtraOmitFields extends string,
|
|
18
|
+
> = Omit<Decimal128SchemaBuilder<Props, ExtraOmitFields>, ExtraOmitFields | keyof Props>;
|
|
16
19
|
|
|
17
20
|
interface BaseProps {
|
|
18
21
|
type: typeof Schema.Types.Decimal128;
|
|
@@ -26,8 +29,18 @@ interface ToStringSetterSchema {
|
|
|
26
29
|
set: (value?: { toString: () => string }) => string;
|
|
27
30
|
}
|
|
28
31
|
|
|
29
|
-
export interface Decimal128SchemaBuilder<
|
|
30
|
-
|
|
32
|
+
export interface Decimal128SchemaBuilder<
|
|
33
|
+
Props extends { type: typeof Schema.Types.Decimal128 } = { type: typeof Schema.Types.Decimal128 },
|
|
34
|
+
ExtraOmitFields extends string = never,
|
|
35
|
+
> {
|
|
36
|
+
default: <
|
|
37
|
+
T extends ((this: any, doc: any) => DefaultType<D>) | DefaultType<D> | null,
|
|
38
|
+
D extends Types.Decimal128,
|
|
39
|
+
>(value: T) => ExtendDecimal128SchemaBuilder<
|
|
40
|
+
Merge<Props, { default: T }>,
|
|
41
|
+
ExtraOmitFields
|
|
42
|
+
>;
|
|
43
|
+
|
|
31
44
|
enum: <
|
|
32
45
|
T extends
|
|
33
46
|
| Readonlyable<Array<D | null>>
|
|
@@ -35,11 +48,16 @@ export interface Decimal128SchemaBuilder<Props extends { type: typeof Schema.Typ
|
|
|
35
48
|
| { message?: M; values: Readonlyable<Array<D | null>> },
|
|
36
49
|
D extends Types.Decimal128,
|
|
37
50
|
M extends string,
|
|
38
|
-
>(
|
|
39
|
-
|
|
40
|
-
|
|
51
|
+
>(value: T) => ExtendDecimal128SchemaBuilder<
|
|
52
|
+
Merge<Props, { enum: T }>,
|
|
53
|
+
ExtraOmitFields
|
|
54
|
+
>;
|
|
55
|
+
|
|
56
|
+
index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendDecimal128SchemaBuilder<
|
|
57
|
+
Merge<Props, { index: T }>,
|
|
58
|
+
ExtraOmitFields
|
|
59
|
+
>;
|
|
41
60
|
|
|
42
|
-
index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendDecimal128SchemaBuilder<Merge<Props, { index: T }>, ExtraOmitFields>;
|
|
43
61
|
nonRequired: Props;
|
|
44
62
|
private: ExtendDecimal128SchemaBuilder<Merge<Props, { private: true }>, ExtraOmitFields>;
|
|
45
63
|
required: Merge<Props, { required: true }>;
|
|
@@ -54,7 +72,13 @@ export interface Decimal128SchemaBuilder<Props extends { type: typeof Schema.Typ
|
|
|
54
72
|
*
|
|
55
73
|
* @returns A schema builder with the rounding and toFixed behavior applied to the `set` option in Mongoose.
|
|
56
74
|
*/
|
|
57
|
-
setRoundAndToFixedSetter: (
|
|
75
|
+
setRoundAndToFixedSetter: (
|
|
76
|
+
places?: number,
|
|
77
|
+
rounding?: Decimal.Rounding
|
|
78
|
+
) => ExtendDecimal128SchemaBuilder<
|
|
79
|
+
Merge<Props, ToStringSetterSchema>,
|
|
80
|
+
'setRoundAndToFixedSetter' | ExtraOmitFields
|
|
81
|
+
>;
|
|
58
82
|
|
|
59
83
|
/**
|
|
60
84
|
* Sets the toString behavior for Decimal128 fields.
|
|
@@ -65,7 +89,11 @@ export interface Decimal128SchemaBuilder<Props extends { type: typeof Schema.Typ
|
|
|
65
89
|
* @returns A schema builder with the custom `get` behavior applied to convert the Decimal128 field to a string
|
|
66
90
|
* when accessed in the application.
|
|
67
91
|
*/
|
|
68
|
-
setToStringGetter: ExtendDecimal128SchemaBuilder<
|
|
92
|
+
setToStringGetter: ExtendDecimal128SchemaBuilder<
|
|
93
|
+
Merge<Props, ToStringGetterSchema>,
|
|
94
|
+
'setToStringGetter' | ExtraOmitFields
|
|
95
|
+
>;
|
|
96
|
+
|
|
69
97
|
sparse: ExtendDecimal128SchemaBuilder<Merge<Props, { sparse: true }>, ExtraOmitFields>;
|
|
70
98
|
unique: ExtendDecimal128SchemaBuilder<Merge<Props, { unique: true }>, ExtraOmitFields>;
|
|
71
99
|
}
|
|
@@ -81,7 +109,10 @@ export function decimal128SchemaBuilder() {
|
|
|
81
109
|
get(target, key, receiver) {
|
|
82
110
|
if (key === 'setRoundAndToFixedSetter') {
|
|
83
111
|
return (places: number = 2, rounding: Decimal.Rounding = Decimal.ROUND_DOWN) => {
|
|
84
|
-
schema.set = (value?: { toString: () => string }) =>
|
|
112
|
+
schema.set = (value?: { toString: () => string }) => {
|
|
113
|
+
if (value) return new Decimal(value.toString()).toFixed(places, rounding);
|
|
114
|
+
};
|
|
115
|
+
|
|
85
116
|
return receiver;
|
|
86
117
|
};
|
|
87
118
|
}
|
|
@@ -10,14 +10,27 @@ import type { Readonlyable } from '../types/utils';
|
|
|
10
10
|
|
|
11
11
|
import { createBaseSchemaBuilderFactory } from './base';
|
|
12
12
|
|
|
13
|
-
export type ExtendNumberSchemaBuilder<
|
|
13
|
+
export type ExtendNumberSchemaBuilder<
|
|
14
|
+
Props extends BaseProps,
|
|
15
|
+
ExtraOmitFields extends string,
|
|
16
|
+
> = Omit<NumberSchemaBuilder<Props, ExtraOmitFields>, ExtraOmitFields | keyof Props>;
|
|
14
17
|
|
|
15
18
|
interface BaseProps {
|
|
16
19
|
type: NumberSchemaDefinition;
|
|
17
20
|
}
|
|
18
21
|
|
|
19
|
-
export interface NumberSchemaBuilder<
|
|
20
|
-
|
|
22
|
+
export interface NumberSchemaBuilder<
|
|
23
|
+
Props extends { type: NumberSchemaDefinition } = { type: NumberSchemaDefinition },
|
|
24
|
+
ExtraOmitFields extends string = never,
|
|
25
|
+
> {
|
|
26
|
+
default: <
|
|
27
|
+
T extends ((this: any, doc: any) => DefaultType<D>) | DefaultType<D> | null,
|
|
28
|
+
D extends number,
|
|
29
|
+
>(value: T) => ExtendNumberSchemaBuilder<
|
|
30
|
+
Merge<Props, { default: T }>,
|
|
31
|
+
ExtraOmitFields
|
|
32
|
+
>;
|
|
33
|
+
|
|
21
34
|
enum: <
|
|
22
35
|
T extends
|
|
23
36
|
| Readonlyable<Array<N | null>>
|
|
@@ -29,9 +42,29 @@ export interface NumberSchemaBuilder<Props extends { type: NumberSchemaDefinitio
|
|
|
29
42
|
value: T
|
|
30
43
|
) => ExtendNumberSchemaBuilder<Merge<Props, { enum: T }>, ExtraOmitFields>;
|
|
31
44
|
|
|
32
|
-
index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendNumberSchemaBuilder<
|
|
33
|
-
|
|
34
|
-
|
|
45
|
+
index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendNumberSchemaBuilder<
|
|
46
|
+
Merge<Props, { index: T }>,
|
|
47
|
+
ExtraOmitFields
|
|
48
|
+
>;
|
|
49
|
+
|
|
50
|
+
max: <
|
|
51
|
+
T extends N | Readonlyable<[N, S]>,
|
|
52
|
+
N extends number,
|
|
53
|
+
S extends string,
|
|
54
|
+
>(value: T) => ExtendNumberSchemaBuilder<
|
|
55
|
+
Merge<Props, { max: T }>,
|
|
56
|
+
ExtraOmitFields
|
|
57
|
+
>;
|
|
58
|
+
|
|
59
|
+
min: <
|
|
60
|
+
T extends N | Readonlyable<[N, S]>,
|
|
61
|
+
N extends number,
|
|
62
|
+
S extends string,
|
|
63
|
+
>(value: T) => ExtendNumberSchemaBuilder<
|
|
64
|
+
Merge<Props, { min: T }>,
|
|
65
|
+
ExtraOmitFields
|
|
66
|
+
>;
|
|
67
|
+
|
|
35
68
|
nonRequired: Props;
|
|
36
69
|
private: ExtendNumberSchemaBuilder<Merge<Props, { private: true }>, ExtraOmitFields>;
|
|
37
70
|
required: Merge<Props, { required: true }>;
|
|
@@ -11,14 +11,27 @@ import type { Readonlyable } from '../types/utils';
|
|
|
11
11
|
|
|
12
12
|
import { createBaseSchemaBuilderFactory } from './base';
|
|
13
13
|
|
|
14
|
-
export type ExtendObjectIdSchemaBuilder<
|
|
14
|
+
export type ExtendObjectIdSchemaBuilder<
|
|
15
|
+
Props extends BaseProps,
|
|
16
|
+
ExtraOmitFields extends string,
|
|
17
|
+
> = Omit<ObjectIdSchemaBuilder<Props, ExtraOmitFields>, ExtraOmitFields | keyof Props>;
|
|
15
18
|
|
|
16
19
|
interface BaseProps {
|
|
17
20
|
type: typeof Schema.Types.ObjectId;
|
|
18
21
|
}
|
|
19
22
|
|
|
20
|
-
export interface ObjectIdSchemaBuilder<
|
|
21
|
-
|
|
23
|
+
export interface ObjectIdSchemaBuilder<
|
|
24
|
+
Props extends { type: typeof Schema.Types.ObjectId } = { type: typeof Schema.Types.ObjectId },
|
|
25
|
+
ExtraOmitFields extends string = never,
|
|
26
|
+
> {
|
|
27
|
+
default: <
|
|
28
|
+
T extends ((this: any, doc: any) => DefaultType<D>) | DefaultType<D> | null,
|
|
29
|
+
D extends Types.ObjectId,
|
|
30
|
+
>(value: T) => ExtendObjectIdSchemaBuilder<
|
|
31
|
+
Merge<Props, { default: T }>,
|
|
32
|
+
ExtraOmitFields
|
|
33
|
+
>;
|
|
34
|
+
|
|
22
35
|
enum: <
|
|
23
36
|
T extends
|
|
24
37
|
| Readonlyable<Array<null | O>>
|
|
@@ -30,7 +43,11 @@ export interface ObjectIdSchemaBuilder<Props extends { type: typeof Schema.Types
|
|
|
30
43
|
value: T
|
|
31
44
|
) => ExtendObjectIdSchemaBuilder<Merge<Props, { enum: T }>, ExtraOmitFields>;
|
|
32
45
|
|
|
33
|
-
index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendObjectIdSchemaBuilder<
|
|
46
|
+
index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendObjectIdSchemaBuilder<
|
|
47
|
+
Merge<Props, { index: T }>,
|
|
48
|
+
ExtraOmitFields
|
|
49
|
+
>;
|
|
50
|
+
|
|
34
51
|
nonRequired: Props;
|
|
35
52
|
private: ExtendObjectIdSchemaBuilder<Merge<Props, { private: true }>, ExtraOmitFields>;
|
|
36
53
|
required: Merge<Props, { required: true }>;
|
|
@@ -12,14 +12,27 @@ import type { Readonlyable } from '../types/utils';
|
|
|
12
12
|
|
|
13
13
|
import { createBaseSchemaBuilderFactory } from './base';
|
|
14
14
|
|
|
15
|
-
export type ExtendRefSchemaBuilder<
|
|
15
|
+
export type ExtendRefSchemaBuilder<
|
|
16
|
+
Props extends BaseProps,
|
|
17
|
+
ExtraOmitFields extends string,
|
|
18
|
+
> = Omit<RefSchemaBuilder<Props, ExtraOmitFields>, ExtraOmitFields | keyof Props>;
|
|
16
19
|
|
|
17
20
|
interface BaseProps {
|
|
18
21
|
type: typeof Schema.Types.ObjectId;
|
|
19
22
|
}
|
|
20
23
|
|
|
21
|
-
export interface RefSchemaBuilder<
|
|
22
|
-
|
|
24
|
+
export interface RefSchemaBuilder<
|
|
25
|
+
Props extends { type: typeof Schema.Types.ObjectId } = { type: typeof Schema.Types.ObjectId },
|
|
26
|
+
ExtraOmitFields extends string = never,
|
|
27
|
+
> {
|
|
28
|
+
default: <
|
|
29
|
+
T extends ((this: any, doc: any) => DefaultType<D>) | DefaultType<D> | null,
|
|
30
|
+
D extends Types.ObjectId,
|
|
31
|
+
>(value: T) => ExtendRefSchemaBuilder<
|
|
32
|
+
Merge<Props, { default: T }>,
|
|
33
|
+
ExtraOmitFields
|
|
34
|
+
>;
|
|
35
|
+
|
|
23
36
|
enum: <
|
|
24
37
|
T extends
|
|
25
38
|
| Readonlyable<Array<null | O>>
|
|
@@ -31,7 +44,11 @@ export interface RefSchemaBuilder<Props extends { type: typeof Schema.Types.Obje
|
|
|
31
44
|
value: T
|
|
32
45
|
) => ExtendRefSchemaBuilder<Merge<Props, { enum: T }>, ExtraOmitFields>;
|
|
33
46
|
|
|
34
|
-
index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendRefSchemaBuilder<
|
|
47
|
+
index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendRefSchemaBuilder<
|
|
48
|
+
Merge<Props, { index: T }>,
|
|
49
|
+
ExtraOmitFields
|
|
50
|
+
>;
|
|
51
|
+
|
|
35
52
|
nonRequired: Props;
|
|
36
53
|
private: ExtendRefSchemaBuilder<Merge<Props, { private: true }>, ExtraOmitFields>;
|
|
37
54
|
required: Merge<Props, { required: true }>;
|
|
@@ -40,4 +57,9 @@ export interface RefSchemaBuilder<Props extends { type: typeof Schema.Types.Obje
|
|
|
40
57
|
}
|
|
41
58
|
|
|
42
59
|
const baseBuilderFactory = createBaseSchemaBuilderFactory(Schema.Types.ObjectId);
|
|
43
|
-
|
|
60
|
+
|
|
61
|
+
export function refSchemaBuilder<
|
|
62
|
+
T extends ((this: any, doc: any) => Model<any> | string) | Model<any> | string,
|
|
63
|
+
>(ref: T) {
|
|
64
|
+
return baseBuilderFactory({ ref }) as RefSchemaBuilder<{ ref: T; type: typeof Schema.Types.ObjectId }>;
|
|
65
|
+
}
|
|
@@ -1,17 +1,21 @@
|
|
|
1
|
+
import net from 'node:net';
|
|
2
|
+
|
|
1
3
|
import type {
|
|
2
4
|
DefaultType,
|
|
3
5
|
IndexDirection,
|
|
4
6
|
IndexOptions,
|
|
5
7
|
StringSchemaDefinition,
|
|
6
8
|
} from 'mongoose';
|
|
7
|
-
import net from 'node:net';
|
|
8
9
|
import type { Merge } from 'type-fest';
|
|
9
10
|
|
|
10
11
|
import type { Readonlyable } from '../types/utils';
|
|
11
12
|
|
|
12
13
|
import { createBaseSchemaBuilderFactory } from './base';
|
|
13
14
|
|
|
14
|
-
export type ExtendStringSchemaBuilder<
|
|
15
|
+
export type ExtendStringSchemaBuilder<
|
|
16
|
+
Props extends BaseProps,
|
|
17
|
+
ExtraOmitFields extends string,
|
|
18
|
+
> = Omit<StringSchemaBuilder<Props, ExtraOmitFields>, ExtraOmitFields | keyof Props>;
|
|
15
19
|
|
|
16
20
|
interface BaseProps {
|
|
17
21
|
type: StringSchemaDefinition;
|
|
@@ -22,8 +26,18 @@ interface IpSchema<T extends string> {
|
|
|
22
26
|
validate: { message: T; validator: (value: string) => boolean };
|
|
23
27
|
}
|
|
24
28
|
|
|
25
|
-
export interface StringSchemaBuilder<
|
|
26
|
-
|
|
29
|
+
export interface StringSchemaBuilder<
|
|
30
|
+
Props extends { type: StringSchemaDefinition } = { type: StringSchemaDefinition },
|
|
31
|
+
ExtraOmitFields extends string = never,
|
|
32
|
+
> {
|
|
33
|
+
default: <
|
|
34
|
+
T extends ((this: any, doc: any) => DefaultType<D>) | DefaultType<D> | null,
|
|
35
|
+
D extends string,
|
|
36
|
+
>(value: T) => ExtendStringSchemaBuilder<
|
|
37
|
+
Merge<Props, { default: T }>,
|
|
38
|
+
ExtraOmitFields
|
|
39
|
+
>;
|
|
40
|
+
|
|
27
41
|
enum: <
|
|
28
42
|
T extends
|
|
29
43
|
| Readonlyable<Array<null | S>>
|
|
@@ -35,27 +49,38 @@ export interface StringSchemaBuilder<Props extends { type: StringSchemaDefinitio
|
|
|
35
49
|
value: T
|
|
36
50
|
) => ExtendStringSchemaBuilder<Merge<Props, { enum: T }>, ExtraOmitFields>;
|
|
37
51
|
|
|
38
|
-
index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendStringSchemaBuilder<
|
|
52
|
+
index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendStringSchemaBuilder<
|
|
53
|
+
Merge<Props, { index: T }>,
|
|
54
|
+
ExtraOmitFields
|
|
55
|
+
>;
|
|
39
56
|
|
|
40
57
|
/**
|
|
41
58
|
* Adds IPv4 validation to the string schema.
|
|
42
59
|
* Ensures the string is a valid IPv4 address and trims the input. The validation message can be customized.
|
|
43
60
|
*
|
|
44
|
-
* @param message - The custom error message to return if validation fails. Defaults to a standard
|
|
61
|
+
* @param message - The custom error message to return if validation fails. Defaults to a standard
|
|
62
|
+
* IPv4 validation message.
|
|
45
63
|
*
|
|
46
64
|
* @returns A schema builder with IPv4 validation and the `trim` option enabled.
|
|
47
65
|
*/
|
|
48
|
-
ipv4: <T extends string = typeof defaultIpv4ValidateMessage>(message?: T) => ExtendStringSchemaBuilder<
|
|
66
|
+
ipv4: <T extends string = typeof defaultIpv4ValidateMessage>(message?: T) => ExtendStringSchemaBuilder<
|
|
67
|
+
Merge<Props, IpSchema<T>>,
|
|
68
|
+
'ipv4' | 'ipv6' | ExtraOmitFields
|
|
69
|
+
>;
|
|
49
70
|
|
|
50
71
|
/**
|
|
51
72
|
* Adds IPv6 validation to the string schema.
|
|
52
73
|
* Ensures the string is a valid IPv6 address and trims the input. The validation message can be customized.
|
|
53
74
|
*
|
|
54
|
-
* @param message - The custom error message to return if validation fails. Defaults to a standard
|
|
75
|
+
* @param message - The custom error message to return if validation fails. Defaults to a standard
|
|
76
|
+
* IPv6 validation message.
|
|
55
77
|
*
|
|
56
78
|
* @returns A schema builder with IPv6 validation and the `trim` option enabled.
|
|
57
79
|
*/
|
|
58
|
-
ipv6: <T extends string = typeof defaultIpv6ValidateMessage>(message?: T) => ExtendStringSchemaBuilder<
|
|
80
|
+
ipv6: <T extends string = typeof defaultIpv6ValidateMessage>(message?: T) => ExtendStringSchemaBuilder<
|
|
81
|
+
Merge<Props, IpSchema<T>>,
|
|
82
|
+
'ipv4' | 'ipv6' | ExtraOmitFields
|
|
83
|
+
>;
|
|
59
84
|
|
|
60
85
|
/**
|
|
61
86
|
* Sets both the maximum and minimum length of the string.
|
|
@@ -65,10 +90,34 @@ export interface StringSchemaBuilder<Props extends { type: StringSchemaDefinitio
|
|
|
65
90
|
*
|
|
66
91
|
* @returns A schema builder with both `maxlength` and `minlength` options applied.
|
|
67
92
|
*/
|
|
68
|
-
length: <
|
|
93
|
+
length: <
|
|
94
|
+
T extends L | Readonlyable<[L, S]>,
|
|
95
|
+
L extends number,
|
|
96
|
+
S extends string,
|
|
97
|
+
>(value: T) => ExtendStringSchemaBuilder<
|
|
98
|
+
Merge<Props, { maxlength: T; minlength: T }>,
|
|
99
|
+
ExtraOmitFields
|
|
100
|
+
>;
|
|
101
|
+
|
|
69
102
|
lowercase: ExtendStringSchemaBuilder<Merge<Props, { lowercase: true }>, ExtraOmitFields>;
|
|
70
|
-
maxlength: <
|
|
71
|
-
|
|
103
|
+
maxlength: <
|
|
104
|
+
T extends L | Readonlyable<[L, S]>,
|
|
105
|
+
L extends number,
|
|
106
|
+
S extends string,
|
|
107
|
+
>(value: T) => ExtendStringSchemaBuilder<
|
|
108
|
+
Merge<Props, { maxlength: T }>,
|
|
109
|
+
ExtraOmitFields
|
|
110
|
+
>;
|
|
111
|
+
|
|
112
|
+
minlength: <
|
|
113
|
+
T extends L | Readonlyable<[L, S]>,
|
|
114
|
+
L extends number,
|
|
115
|
+
S extends string,
|
|
116
|
+
>(value: T) => ExtendStringSchemaBuilder<
|
|
117
|
+
Merge<Props, { minlength: T }>,
|
|
118
|
+
ExtraOmitFields
|
|
119
|
+
>;
|
|
120
|
+
|
|
72
121
|
nonRequired: Props;
|
|
73
122
|
private: ExtendStringSchemaBuilder<Merge<Props, { private: true }>, ExtraOmitFields>;
|
|
74
123
|
required: Merge<Props, { required: true }>;
|
|
@@ -75,7 +75,11 @@ declare module 'mongoose' {
|
|
|
75
75
|
totalPages: number;
|
|
76
76
|
}
|
|
77
77
|
|
|
78
|
-
interface AggregatePaginateModel<
|
|
78
|
+
interface AggregatePaginateModel<
|
|
79
|
+
RawDocType,
|
|
80
|
+
QueryHelpers = object,
|
|
81
|
+
InstanceMethodsAndOverrides = object,
|
|
82
|
+
> extends Model<RawDocType, QueryHelpers, InstanceMethodsAndOverrides> {
|
|
79
83
|
aggregatePaginate: <T>(
|
|
80
84
|
query?: Aggregate<T[]> | PrePaginatePipelineStage[],
|
|
81
85
|
options?: AggregatePaginateOptions,
|
package/src/types/data.ts
CHANGED
|
@@ -10,7 +10,10 @@ import type { IfElse } from './utils';
|
|
|
10
10
|
* @template CreatedAtField - A boolean flag indicating whether the `createdAt` field should be included.
|
|
11
11
|
* @template UpdatedAtField - A boolean flag indicating whether the `updatedAt` field should be included.
|
|
12
12
|
*/
|
|
13
|
-
interface LocalBaseMongooseModelData<
|
|
13
|
+
interface LocalBaseMongooseModelData<
|
|
14
|
+
CreatedAtField extends boolean = true,
|
|
15
|
+
UpdatedAtField extends boolean = true,
|
|
16
|
+
> extends LocalWithTimestampFieldsData<CreatedAtField, UpdatedAtField> {
|
|
14
17
|
id: string;
|
|
15
18
|
}
|
|
16
19
|
|
|
@@ -49,7 +52,10 @@ declare global {
|
|
|
49
52
|
* @template CreatedAtField - A boolean flag indicating whether the `createdAt` field should be included.
|
|
50
53
|
* @template UpdatedAtField - A boolean flag indicating whether the `updatedAt` field should be included.
|
|
51
54
|
*/
|
|
52
|
-
type BaseMongooseModelData<
|
|
55
|
+
type BaseMongooseModelData<
|
|
56
|
+
CreatedAtField extends boolean = true,
|
|
57
|
+
UpdatedAtField extends boolean = true,
|
|
58
|
+
> = OmitNever<LocalBaseMongooseModelData<CreatedAtField, UpdatedAtField>>;
|
|
53
59
|
|
|
54
60
|
/**
|
|
55
61
|
* Type definition to omit timestamp fields (`createdAt`, `updatedAt`)
|
|
@@ -67,7 +73,10 @@ declare global {
|
|
|
67
73
|
* @template T - The type from which to omit fields.
|
|
68
74
|
* @template OT - Additional fields to omit, specified as a union of string, number, or symbol keys.
|
|
69
75
|
*/
|
|
70
|
-
type OmitMongooseTimestampAndOtherFields<
|
|
76
|
+
type OmitMongooseTimestampAndOtherFields<
|
|
77
|
+
T,
|
|
78
|
+
OT extends number | string | symbol = never,
|
|
79
|
+
> = Omit<T, 'createdAt' | 'updatedAt' | OT>;
|
|
71
80
|
|
|
72
81
|
/**
|
|
73
82
|
* Type definition for including timestamp fields conditionally based on the provided flags.
|
|
@@ -78,7 +87,10 @@ declare global {
|
|
|
78
87
|
* @template CreatedAt - A boolean flag indicating whether the `createdAt` field should be included.
|
|
79
88
|
* @template UpdatedAt - A boolean flag indicating whether the `updatedAt` field should be included.
|
|
80
89
|
*/
|
|
81
|
-
type WithMongooseTimestampFieldsData<
|
|
90
|
+
type WithMongooseTimestampFieldsData<
|
|
91
|
+
CreatedAt extends boolean = true,
|
|
92
|
+
UpdatedAt extends boolean = true,
|
|
93
|
+
> = OmitNever<LocalWithTimestampFieldsData<CreatedAt, UpdatedAt>>;
|
|
82
94
|
}
|
|
83
95
|
|
|
84
96
|
export {};
|
package/src/types/index.ts
CHANGED
|
@@ -75,7 +75,8 @@ declare global {
|
|
|
75
75
|
* @template RawDocType - The raw document type as stored in MongoDB.
|
|
76
76
|
* @template DocType - The Mongoose document type after applying schema transformations.
|
|
77
77
|
* @template QueryHelpers - Optional type parameter for additional query helper methods.
|
|
78
|
-
* @template InstanceMethodsAndOverrides - Optional type parameter for specifying custom instance methods
|
|
78
|
+
* @template InstanceMethodsAndOverrides - Optional type parameter for specifying custom instance methods
|
|
79
|
+
* and overrides on the document.
|
|
79
80
|
*/
|
|
80
81
|
type MongooseFindOneReturnType<
|
|
81
82
|
RawDocType,
|
|
@@ -103,5 +104,9 @@ declare global {
|
|
|
103
104
|
* @template InstanceMethodsAndOverrides - Optional type parameter for instance methods and overrides.
|
|
104
105
|
* @template QueryHelpers - Optional type parameter for additional query helper methods.
|
|
105
106
|
*/
|
|
106
|
-
type MongooseHydratedDocument<
|
|
107
|
+
type MongooseHydratedDocument<
|
|
108
|
+
DocType,
|
|
109
|
+
InstanceMethodsAndOverrides = object,
|
|
110
|
+
QueryHelpers = object,
|
|
111
|
+
> = HydratedDocument<DocType, InstanceMethodsAndOverrides, QueryHelpers>;
|
|
107
112
|
}
|
package/src/types/options.ts
CHANGED
|
@@ -35,11 +35,14 @@ export interface BuildMongooseModelOptions {
|
|
|
35
35
|
enableNormalizePlugin?: boolean;
|
|
36
36
|
|
|
37
37
|
/**
|
|
38
|
-
*
|
|
38
|
+
* Configures Mongoose timestamps.
|
|
39
39
|
*
|
|
40
|
-
* This option
|
|
41
|
-
* custom timestamp configuration
|
|
42
|
-
*
|
|
40
|
+
* This option enables or disables timestamps, or allows specifying a
|
|
41
|
+
* custom timestamp configuration via {@link SchemaTimestampsConfig}.
|
|
42
|
+
*
|
|
43
|
+
* - If `timestamps` is explicitly set, it will be used.
|
|
44
|
+
* - If `timestamps` is not provided, the schema's existing setting is used.
|
|
45
|
+
* - If the schema has no existing setting, `true` is the default value.
|
|
43
46
|
*
|
|
44
47
|
* @default true
|
|
45
48
|
*/
|
package/src/types/paginate.ts
CHANGED
|
@@ -115,23 +115,44 @@ declare module 'mongoose' {
|
|
|
115
115
|
<UserType = T, O extends PaginateOptions = PaginateOptions>(
|
|
116
116
|
query?: FilterQuery<T>,
|
|
117
117
|
options?: O,
|
|
118
|
-
callback?: (
|
|
118
|
+
callback?: (
|
|
119
|
+
err: any,
|
|
120
|
+
result: PaginateResult<PaginateDocument<UserType, TMethods, TQueryHelpers, O>>
|
|
121
|
+
) => void
|
|
119
122
|
): Promise<PaginateResult<PaginateDocument<UserType, TMethods, TQueryHelpers, O>>>;
|
|
120
123
|
|
|
121
124
|
<UserType = T>(
|
|
122
125
|
query?: FilterQuery<T>,
|
|
123
126
|
options?: PaginateOptions,
|
|
124
|
-
callback?: (
|
|
127
|
+
callback?: (
|
|
128
|
+
err: any,
|
|
129
|
+
result: PaginateResult<PaginateDocument<UserType, TMethods, TQueryHelpers, PaginateOptions>>
|
|
130
|
+
) => void
|
|
125
131
|
): Promise<PaginateResult<PaginateDocument<UserType, TMethods, TQueryHelpers, PaginateOptions>>>;
|
|
126
132
|
};
|
|
127
133
|
}
|
|
128
134
|
|
|
129
135
|
// @ts-expect-error Ignore this error.
|
|
130
|
-
interface Query<
|
|
136
|
+
interface Query<
|
|
137
|
+
ResultType,
|
|
138
|
+
DocType,
|
|
139
|
+
THelpers = NonNullable<unknown>,
|
|
140
|
+
RawDocType = DocType,
|
|
141
|
+
_QueryOp = 'find',
|
|
142
|
+
TInstanceMethods = Record<string, never>,
|
|
143
|
+
> {
|
|
131
144
|
paginate: {
|
|
132
|
-
<O extends PaginateOptions>(options?: O): Promise<
|
|
133
|
-
|
|
134
|
-
|
|
145
|
+
<O extends PaginateOptions>(options?: O): Promise<
|
|
146
|
+
PaginateResult<PaginateDocument<RawDocType, TInstanceMethods, THelpers, O>>
|
|
147
|
+
>;
|
|
148
|
+
|
|
149
|
+
<UserType = ResultType, O extends PaginateOptions = PaginateOptions>(options?: O): Promise<
|
|
150
|
+
PaginateResult<PaginateDocument<UserType, TInstanceMethods, THelpers, O>>
|
|
151
|
+
>;
|
|
152
|
+
|
|
153
|
+
<UserType = ResultType>(options?: PaginateOptions): Promise<
|
|
154
|
+
PaginateResult<PaginateDocument<UserType, TInstanceMethods, THelpers, PaginateOptions>>
|
|
155
|
+
>;
|
|
135
156
|
};
|
|
136
157
|
}
|
|
137
158
|
}
|
package/src/types/utils.ts
CHANGED
|
@@ -1,2 +1,8 @@
|
|
|
1
|
-
export type IfElse<
|
|
1
|
+
export type IfElse<
|
|
2
|
+
Condition,
|
|
3
|
+
Then,
|
|
4
|
+
Else,
|
|
5
|
+
ReferenceCondition = true,
|
|
6
|
+
> = Condition extends ReferenceCondition ? Then : Else;
|
|
7
|
+
|
|
2
8
|
export type Readonlyable<T> = Readonly<T> | T;
|