@kikiutils/mongoose 2.1.2 → 2.1.4
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/{_internals.cjs → dist/_internals.cjs} +1 -0
- package/dist/_internals.cjs.map +1 -0
- package/{_internals.d.ts → dist/_internals.d.ts} +1 -0
- package/dist/_internals.d.ts.map +1 -0
- package/{_internals.mjs → dist/_internals.mjs} +1 -0
- package/dist/_internals.mjs.map +1 -0
- package/{constants.cjs → dist/constants.cjs} +1 -0
- package/dist/constants.cjs.map +1 -0
- package/{constants.d.ts → dist/constants.d.ts} +1 -0
- package/dist/constants.d.ts.map +1 -0
- package/{constants.mjs → dist/constants.mjs} +1 -0
- package/dist/constants.mjs.map +1 -0
- package/{options.cjs → dist/options.cjs} +1 -0
- package/dist/options.cjs.map +1 -0
- package/{options.d.ts → dist/options.d.ts} +1 -0
- package/dist/options.d.ts.map +1 -0
- package/{options.mjs → dist/options.mjs} +1 -0
- package/dist/options.mjs.map +1 -0
- package/{plugins → dist/plugins}/normalize.cjs +1 -0
- package/dist/plugins/normalize.cjs.map +1 -0
- package/{plugins → dist/plugins}/normalize.d.ts +1 -0
- package/dist/plugins/normalize.d.ts.map +1 -0
- package/{plugins → dist/plugins}/normalize.mjs +1 -0
- package/dist/plugins/normalize.mjs.map +1 -0
- package/{schema-builders → dist/schema-builders}/base.cjs +1 -0
- package/dist/schema-builders/base.cjs.map +1 -0
- package/{schema-builders → dist/schema-builders}/base.d.ts +1 -0
- package/dist/schema-builders/base.d.ts.map +1 -0
- package/{schema-builders → dist/schema-builders}/base.mjs +1 -0
- package/dist/schema-builders/base.mjs.map +1 -0
- package/{schema-builders → dist/schema-builders}/boolean.cjs +1 -0
- package/dist/schema-builders/boolean.cjs.map +1 -0
- package/{schema-builders → dist/schema-builders}/boolean.d.ts +1 -0
- package/dist/schema-builders/boolean.d.ts.map +1 -0
- package/{schema-builders → dist/schema-builders}/boolean.mjs +1 -0
- package/dist/schema-builders/boolean.mjs.map +1 -0
- package/{schema-builders → dist/schema-builders}/date.cjs +1 -0
- package/dist/schema-builders/date.cjs.map +1 -0
- package/{schema-builders → dist/schema-builders}/date.d.ts +1 -0
- package/dist/schema-builders/date.d.ts.map +1 -0
- package/{schema-builders → dist/schema-builders}/date.mjs +1 -0
- package/dist/schema-builders/date.mjs.map +1 -0
- package/{schema-builders → dist/schema-builders}/decimal128.cjs +2 -1
- package/dist/schema-builders/decimal128.cjs.map +1 -0
- package/{schema-builders → dist/schema-builders}/decimal128.d.ts +2 -1
- package/dist/schema-builders/decimal128.d.ts.map +1 -0
- package/{schema-builders → dist/schema-builders}/decimal128.mjs +2 -1
- package/dist/schema-builders/decimal128.mjs.map +1 -0
- package/{schema-builders → dist/schema-builders}/index.cjs +1 -0
- package/dist/schema-builders/index.cjs.map +1 -0
- package/{schema-builders → dist/schema-builders}/index.d.ts +1 -0
- package/dist/schema-builders/index.d.ts.map +1 -0
- package/{schema-builders → dist/schema-builders}/index.mjs +1 -0
- package/dist/schema-builders/index.mjs.map +1 -0
- package/{schema-builders → dist/schema-builders}/number.cjs +1 -0
- package/dist/schema-builders/number.cjs.map +1 -0
- package/{schema-builders → dist/schema-builders}/number.d.ts +1 -0
- package/dist/schema-builders/number.d.ts.map +1 -0
- package/{schema-builders → dist/schema-builders}/number.mjs +1 -0
- package/dist/schema-builders/number.mjs.map +1 -0
- package/{schema-builders → dist/schema-builders}/object-id.cjs +1 -0
- package/dist/schema-builders/object-id.cjs.map +1 -0
- package/{schema-builders → dist/schema-builders}/object-id.d.ts +1 -0
- package/dist/schema-builders/object-id.d.ts.map +1 -0
- package/{schema-builders → dist/schema-builders}/object-id.mjs +1 -0
- package/dist/schema-builders/object-id.mjs.map +1 -0
- package/{schema-builders → dist/schema-builders}/ref.cjs +1 -0
- package/dist/schema-builders/ref.cjs.map +1 -0
- package/{schema-builders → dist/schema-builders}/ref.d.ts +1 -0
- package/dist/schema-builders/ref.d.ts.map +1 -0
- package/{schema-builders → dist/schema-builders}/ref.mjs +1 -0
- package/dist/schema-builders/ref.mjs.map +1 -0
- package/{schema-builders → dist/schema-builders}/string.cjs +1 -0
- package/dist/schema-builders/string.cjs.map +1 -0
- package/{schema-builders → dist/schema-builders}/string.d.ts +1 -0
- package/dist/schema-builders/string.d.ts.map +1 -0
- package/{schema-builders → dist/schema-builders}/string.mjs +1 -0
- package/dist/schema-builders/string.mjs.map +1 -0
- package/{types → dist/types}/aggregate-paginate.d.ts +1 -0
- package/dist/types/aggregate-paginate.d.ts.map +1 -0
- package/{types → dist/types}/data.d.ts +1 -0
- package/dist/types/data.d.ts.map +1 -0
- package/{types → dist/types}/index.d.ts +1 -0
- package/dist/types/index.d.ts.map +1 -0
- package/{types → dist/types}/options.d.ts +1 -0
- package/dist/types/options.d.ts.map +1 -0
- package/{types → dist/types}/paginate.d.ts +1 -0
- package/dist/types/paginate.d.ts.map +1 -0
- package/dist/types/utils.d.ts +2 -0
- package/dist/types/utils.d.ts.map +1 -0
- package/{utils.cjs → dist/utils.cjs} +2 -2
- package/dist/utils.cjs.map +1 -0
- package/{utils.d.ts → dist/utils.d.ts} +1 -0
- package/dist/utils.d.ts.map +1 -0
- package/{utils.mjs → dist/utils.mjs} +2 -2
- package/dist/utils.mjs.map +1 -0
- package/package.json +21 -17
- package/src/_internals.ts +3 -0
- package/src/constants.ts +10 -0
- package/src/options.ts +14 -0
- package/src/plugins/normalize.ts +54 -0
- package/src/schema-builders/base.ts +43 -0
- package/src/schema-builders/boolean.ts +27 -0
- package/src/schema-builders/date.ts +42 -0
- package/src/schema-builders/decimal128.ts +98 -0
- package/src/schema-builders/index.ts +26 -0
- package/src/schema-builders/number.ts +42 -0
- package/src/schema-builders/object-id.ts +42 -0
- package/src/schema-builders/ref.ts +44 -0
- package/src/schema-builders/string.ts +128 -0
- package/src/types/aggregate-paginate.ts +89 -0
- package/src/types/data.ts +82 -0
- package/src/types/index.ts +99 -0
- package/src/types/options.ts +61 -0
- package/src/types/paginate.ts +134 -0
- package/src/utils.ts +88 -0
- /package/{types/utils.d.ts → src/types/utils.ts} +0 -0
|
@@ -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,42 @@
|
|
|
1
|
+
import { Schema } from 'mongoose';
|
|
2
|
+
import type {
|
|
3
|
+
DefaultType,
|
|
4
|
+
IndexDirection,
|
|
5
|
+
IndexOptions,
|
|
6
|
+
ObjectIdSchemaDefinition,
|
|
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 ExtendObjectIdSchemaBuilder<Props extends BaseProps, ExtraOmitFields extends string> = Omit<ObjectIdSchemaBuilder<Props, ExtraOmitFields>, ExtraOmitFields | keyof Props>;
|
|
16
|
+
|
|
17
|
+
interface BaseProps {
|
|
18
|
+
type: ObjectIdSchemaDefinition;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface ObjectIdSchemaBuilder<Props extends { type: ObjectIdSchemaDefinition } = { type: ObjectIdSchemaDefinition }, ExtraOmitFields extends string = never> {
|
|
22
|
+
default: <T extends ((this: any, doc: any) => DefaultType<D>) | DefaultType<D> | null, D extends Types.ObjectId>(value: T) => ExtendObjectIdSchemaBuilder<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
|
+
) => ExtendObjectIdSchemaBuilder<Merge<Props, { enum: T }>, ExtraOmitFields>;
|
|
33
|
+
|
|
34
|
+
index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendObjectIdSchemaBuilder<Merge<Props, { index: T }>, ExtraOmitFields>;
|
|
35
|
+
nonRequired: Props;
|
|
36
|
+
private: ExtendObjectIdSchemaBuilder<Merge<Props, { private: true }>, ExtraOmitFields>;
|
|
37
|
+
required: Merge<Props, { required: true }>;
|
|
38
|
+
sparse: ExtendObjectIdSchemaBuilder<Merge<Props, { sparse: true }>, ExtraOmitFields>;
|
|
39
|
+
unique: ExtendObjectIdSchemaBuilder<Merge<Props, { unique: true }>, ExtraOmitFields>;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export const objectIdSchemaBuilder = createBaseSchemaBuilderFactory<ObjectIdSchemaBuilder>(Schema.Types.ObjectId);
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { Schema } from 'mongoose';
|
|
2
|
+
import type {
|
|
3
|
+
DefaultType,
|
|
4
|
+
IndexDirection,
|
|
5
|
+
IndexOptions,
|
|
6
|
+
Model,
|
|
7
|
+
ObjectIdSchemaDefinition,
|
|
8
|
+
Types,
|
|
9
|
+
} from 'mongoose';
|
|
10
|
+
import type { Merge } from 'type-fest';
|
|
11
|
+
|
|
12
|
+
import type { Readonlyable } from '../types/utils';
|
|
13
|
+
|
|
14
|
+
import { createBaseSchemaBuilderFactory } from './base';
|
|
15
|
+
|
|
16
|
+
export type ExtendRefSchemaBuilder<Props extends BaseProps, ExtraOmitFields extends string> = Omit<RefSchemaBuilder<Props, ExtraOmitFields>, ExtraOmitFields | keyof Props>;
|
|
17
|
+
|
|
18
|
+
interface BaseProps {
|
|
19
|
+
type: ObjectIdSchemaDefinition;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface RefSchemaBuilder<Props extends { type: ObjectIdSchemaDefinition } = { type: ObjectIdSchemaDefinition }, ExtraOmitFields extends string = never> {
|
|
23
|
+
default: <T extends ((this: any, doc: any) => DefaultType<D>) | DefaultType<D> | null, D extends Types.ObjectId>(value: T) => ExtendRefSchemaBuilder<Merge<Props, { default: T }>, ExtraOmitFields>;
|
|
24
|
+
enum: <
|
|
25
|
+
T extends
|
|
26
|
+
| Readonlyable<Array<null | O>>
|
|
27
|
+
| { [path: string]: null | O }
|
|
28
|
+
| { message?: M; values: Readonlyable<Array<null | O>> },
|
|
29
|
+
M extends string,
|
|
30
|
+
O extends Types.ObjectId,
|
|
31
|
+
>(
|
|
32
|
+
value: T
|
|
33
|
+
) => ExtendRefSchemaBuilder<Merge<Props, { enum: T }>, ExtraOmitFields>;
|
|
34
|
+
|
|
35
|
+
index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendRefSchemaBuilder<Merge<Props, { index: T }>, ExtraOmitFields>;
|
|
36
|
+
nonRequired: Props;
|
|
37
|
+
private: ExtendRefSchemaBuilder<Merge<Props, { private: true }>, ExtraOmitFields>;
|
|
38
|
+
required: Merge<Props, { required: true }>;
|
|
39
|
+
sparse: ExtendRefSchemaBuilder<Merge<Props, { sparse: true }>, ExtraOmitFields>;
|
|
40
|
+
unique: ExtendRefSchemaBuilder<Merge<Props, { unique: true }>, ExtraOmitFields>;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const baseBuilderFactory = createBaseSchemaBuilderFactory(Schema.Types.ObjectId);
|
|
44
|
+
export const refSchemaBuilder = <T extends ((this: any, doc: any) => Model<any> | string) | Model<any> | string>(ref: T) => baseBuilderFactory({ ref }) as RefSchemaBuilder<{ ref: T; type: ObjectIdSchemaDefinition }>;
|
|
@@ -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;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Interface for the base data of a Mongoose model, optionally including timestamp fields.
|
|
3
|
+
*
|
|
4
|
+
* This interface extends `LocalWithTimestampFieldsData` to optionally include `createdAt`
|
|
5
|
+
* and `updatedAt` fields based on the `CreatedAtField` and `UpdatedAtField` flags.
|
|
6
|
+
* It also includes a mandatory `id` field.
|
|
7
|
+
*
|
|
8
|
+
* @template CreatedAtField - A boolean flag indicating whether the `createdAt` field should be included.
|
|
9
|
+
* @template UpdatedAtField - A boolean flag indicating whether the `updatedAt` field should be included.
|
|
10
|
+
*/
|
|
11
|
+
interface LocalBaseMongooseModelData<CreatedAtField extends boolean = true, UpdatedAtField extends boolean = true> extends LocalWithTimestampFieldsData<CreatedAtField, UpdatedAtField> {
|
|
12
|
+
id: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Interface for including timestamp fields conditionally based on the provided flags.
|
|
17
|
+
*
|
|
18
|
+
* This interface conditionally includes `createdAt` and `updatedAt` fields based on
|
|
19
|
+
* the `CreatedAt` and `UpdatedAt` boolean flags. If the flag is true, the respective
|
|
20
|
+
* field is included, otherwise it is set to `never`.
|
|
21
|
+
*
|
|
22
|
+
* @template CreatedAt - A boolean flag indicating whether the `createdAt` field should be included.
|
|
23
|
+
* @template UpdatedAt - A boolean flag indicating whether the `updatedAt` field should be included.
|
|
24
|
+
*/
|
|
25
|
+
interface LocalWithTimestampFieldsData<CreatedAt extends boolean = true, UpdatedAt extends boolean = true> {
|
|
26
|
+
createdAt: CreatedAt extends true ? string : never;
|
|
27
|
+
updatedAt: UpdatedAt extends true ? string : never;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Utility type to omit properties that are set to `never` in a given type.
|
|
32
|
+
*
|
|
33
|
+
* This type alias iterates over the properties of a given type `T` and removes any
|
|
34
|
+
* properties that are set to `never`.
|
|
35
|
+
*
|
|
36
|
+
* @template T - The type from which to omit `never` properties.
|
|
37
|
+
*/
|
|
38
|
+
type OmitNever<T> = { [P in keyof T as T[P] extends never ? never : P]: T[P] };
|
|
39
|
+
|
|
40
|
+
declare global {
|
|
41
|
+
/**
|
|
42
|
+
* Type definition for the base data of a Mongoose model, optionally including timestamp fields.
|
|
43
|
+
*
|
|
44
|
+
* This type alias uses `OmitNever` to remove any `never` properties from `LocalBaseMongooseModelData`
|
|
45
|
+
* based on the presence of the `CreatedAtField` and `UpdatedAtField` flags.
|
|
46
|
+
*
|
|
47
|
+
* @template CreatedAtField - A boolean flag indicating whether the `createdAt` field should be included.
|
|
48
|
+
* @template UpdatedAtField - A boolean flag indicating whether the `updatedAt` field should be included.
|
|
49
|
+
*/
|
|
50
|
+
type BaseMongooseModelData<CreatedAtField extends boolean = true, UpdatedAtField extends boolean = true> = OmitNever<LocalBaseMongooseModelData<CreatedAtField, UpdatedAtField>>;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Type definition to omit timestamp fields (`createdAt`, `updatedAt`)
|
|
54
|
+
* and other specified fields (`OT`) from a given type `T`.
|
|
55
|
+
*
|
|
56
|
+
* @template T - The type from which to omit fields.
|
|
57
|
+
* @template OT - Additional fields to omit, specified as a union of keys in `T`.
|
|
58
|
+
*/
|
|
59
|
+
type ExceptMongooseTimestampAndOtherFields<T, OT extends keyof T> = Omit<T, 'createdAt' | 'updatedAt' | OT>;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Type definition to omit timestamp fields (`createdAt`, `updatedAt`)
|
|
63
|
+
* and other specified fields (`OT`) from a given type `T`.
|
|
64
|
+
*
|
|
65
|
+
* @template T - The type from which to omit fields.
|
|
66
|
+
* @template OT - Additional fields to omit, specified as a union of string, number, or symbol keys.
|
|
67
|
+
*/
|
|
68
|
+
type OmitMongooseTimestampAndOtherFields<T, OT extends number | string | symbol = never> = Omit<T, 'createdAt' | 'updatedAt' | OT>;
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Type definition for including timestamp fields conditionally based on the provided flags.
|
|
72
|
+
*
|
|
73
|
+
* This type alias uses `OmitNever` to remove any `never` properties from `LocalWithTimestampFieldsData`
|
|
74
|
+
* based on the presence of the `CreatedAt` and `UpdatedAt` flags.
|
|
75
|
+
*
|
|
76
|
+
* @template CreatedAt - A boolean flag indicating whether the `createdAt` field should be included.
|
|
77
|
+
* @template UpdatedAt - A boolean flag indicating whether the `updatedAt` field should be included.
|
|
78
|
+
*/
|
|
79
|
+
type WithMongooseTimestampFieldsData<CreatedAt extends boolean = true, UpdatedAt extends boolean = true> = OmitNever<LocalWithTimestampFieldsData<CreatedAt, UpdatedAt>>;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export {};
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AggregatePaginateModel,
|
|
3
|
+
Connection,
|
|
4
|
+
HydratedDocument,
|
|
5
|
+
PaginateModel,
|
|
6
|
+
QueryWithHelpers,
|
|
7
|
+
Types,
|
|
8
|
+
} from 'mongoose';
|
|
9
|
+
|
|
10
|
+
export type {} from './aggregate-paginate';
|
|
11
|
+
export type {} from './paginate';
|
|
12
|
+
|
|
13
|
+
// @ts-expect-error Ignore this error.
|
|
14
|
+
// eslint-disable-next-line unused-imports/no-unused-vars
|
|
15
|
+
export interface BaseModelStatics<RawDocType, InstanceMethodsAndOverrides = object, QueryHelpers = object> {}
|
|
16
|
+
|
|
17
|
+
export interface MongooseConnections {
|
|
18
|
+
default?: Connection;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
declare global {
|
|
22
|
+
/**
|
|
23
|
+
* Type definition for a base Mongoose document type.
|
|
24
|
+
*
|
|
25
|
+
* This type alias omits the `createdAt`, `id`, and `updatedAt` fields from type `T`,
|
|
26
|
+
* and conditionally includes `createdAt` and `updatedAt` fields as `Date` based on
|
|
27
|
+
* the `CreatedAtField` and `UpdatedAtField` flags.
|
|
28
|
+
*
|
|
29
|
+
* @template T - The original document type.
|
|
30
|
+
* @template CreatedAtField - A boolean flag indicating whether the `createdAt` field should be included.
|
|
31
|
+
* @template UpdatedAtField - A boolean flag indicating whether the `updatedAt` field should be included.
|
|
32
|
+
*/
|
|
33
|
+
type BaseMongooseDocType<T, CreatedAtField extends boolean = true, UpdatedAtField extends boolean = true> = (CreatedAtField extends true ? { createdAt: Date } : object) & Omit<T, 'createdAt' | 'id' | 'updatedAt'> & (UpdatedAtField extends true ? { updatedAt: Date } : object);
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Type definition for a Mongoose model with pagination and aggregation capabilities.
|
|
37
|
+
*
|
|
38
|
+
* This type alias combines the `AggregatePaginateModel` and `PaginateModel` interfaces,
|
|
39
|
+
* along with custom static methods defined in `BaseModelStatics`.
|
|
40
|
+
*
|
|
41
|
+
* @template RawDocType - The raw document type as stored in MongoDB.
|
|
42
|
+
* @template InstanceMethodsAndOverrides - Optional type parameter for instance methods and overrides.
|
|
43
|
+
* @template QueryHelpers - Optional type parameter for additional query helper methods.
|
|
44
|
+
*/
|
|
45
|
+
type BaseMongoosePaginateModel<RawDocType, InstanceMethodsAndOverrides = object, QueryHelpers = object> = AggregatePaginateModel<RawDocType, QueryHelpers, InstanceMethodsAndOverrides> & BaseModelStatics<RawDocType, InstanceMethodsAndOverrides, QueryHelpers> & PaginateModel<RawDocType, QueryHelpers, InstanceMethodsAndOverrides>;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Type definition for a Mongoose document or ObjectId.
|
|
49
|
+
*
|
|
50
|
+
* This type alias represents a value that can be either a Mongoose document, a string,
|
|
51
|
+
* or an ObjectId. It is useful when working with Mongoose models where you might have
|
|
52
|
+
* references to documents that are stored either as objects or as ObjectIds.
|
|
53
|
+
*
|
|
54
|
+
* @template D - The Mongoose document type.
|
|
55
|
+
*/
|
|
56
|
+
type MongooseDocumentOrObjectId<D> = D | string | Types.ObjectId;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Type definition for the return type of a Mongoose `findOne` query.
|
|
60
|
+
*
|
|
61
|
+
* This type alias represents the return type of a `findOne` query in Mongoose.
|
|
62
|
+
* It extends the `QueryWithHelpers` type, indicating that the query can return
|
|
63
|
+
* either a document of type `DocType` or `null` if no document is found. The
|
|
64
|
+
* `QueryHelpers` type parameter allows for the inclusion of additional query
|
|
65
|
+
* helper methods.
|
|
66
|
+
*
|
|
67
|
+
* @template RawDocType - The raw document type as stored in MongoDB.
|
|
68
|
+
* @template DocType - The Mongoose document type after applying schema transformations.
|
|
69
|
+
* @template QueryHelpers - Optional type parameter for additional query helper methods.
|
|
70
|
+
* @template InstanceMethodsAndOverrides - Optional type parameter for specifying custom instance methods and overrides on the document.
|
|
71
|
+
*/
|
|
72
|
+
type MongooseFindOneReturnType<
|
|
73
|
+
RawDocType,
|
|
74
|
+
DocType,
|
|
75
|
+
QueryHelpers = object,
|
|
76
|
+
InstanceMethodsAndOverrides = object,
|
|
77
|
+
> = QueryWithHelpers<
|
|
78
|
+
DocType | null,
|
|
79
|
+
DocType,
|
|
80
|
+
QueryHelpers,
|
|
81
|
+
RawDocType,
|
|
82
|
+
'findOne',
|
|
83
|
+
InstanceMethodsAndOverrides
|
|
84
|
+
>;
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Type definition for a hydrated Mongoose document.
|
|
88
|
+
*
|
|
89
|
+
* This type alias represents a hydrated document in Mongoose, which is a fully
|
|
90
|
+
* populated document that includes instance methods and query helpers. It extends
|
|
91
|
+
* the `HydratedDocument` type from Mongoose, allowing for additional instance
|
|
92
|
+
* methods and query helpers to be specified.
|
|
93
|
+
*
|
|
94
|
+
* @template DocType - The Mongoose document type after applying schema transformations.
|
|
95
|
+
* @template InstanceMethodsAndOverrides - Optional type parameter for instance methods and overrides.
|
|
96
|
+
* @template QueryHelpers - Optional type parameter for additional query helper methods.
|
|
97
|
+
*/
|
|
98
|
+
type MongooseHydratedDocument<DocType, InstanceMethodsAndOverrides = object, QueryHelpers = object> = HydratedDocument<DocType, InstanceMethodsAndOverrides, QueryHelpers>;
|
|
99
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
Connection,
|
|
3
|
+
Schema,
|
|
4
|
+
SchemaTimestampsConfig,
|
|
5
|
+
} from 'mongoose';
|
|
6
|
+
|
|
7
|
+
import type { buildMongooseModel } from '../utils';
|
|
8
|
+
|
|
9
|
+
export type DoNotRemoveOrUseThisType = typeof buildMongooseModel;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Interface for options used when building a Mongoose model.
|
|
13
|
+
*
|
|
14
|
+
* This interface defines the optional configuration settings that can be provided
|
|
15
|
+
* when creating a Mongoose model. It includes options for specifying the database
|
|
16
|
+
* connection, enabling the normalize plugin, and configuring timestamp fields.
|
|
17
|
+
*/
|
|
18
|
+
export interface BuildMongooseModelOptions {
|
|
19
|
+
/**
|
|
20
|
+
* The Mongoose connection to be used for the model.
|
|
21
|
+
*
|
|
22
|
+
* This is an optional setting that allows specifying a custom connection
|
|
23
|
+
* for the model.
|
|
24
|
+
*/
|
|
25
|
+
connection?: Connection;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Flag to enable or disable the normalize plugin.
|
|
29
|
+
*
|
|
30
|
+
* This flag indicates whether the normalize plugin should be enabled.
|
|
31
|
+
* The default value is `true`.
|
|
32
|
+
*
|
|
33
|
+
* @default true
|
|
34
|
+
*/
|
|
35
|
+
enableNormalizePlugin?: boolean;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Configuration for Mongoose timestamps.
|
|
39
|
+
*
|
|
40
|
+
* This option allows enabling or disabling timestamps, or providing a
|
|
41
|
+
* custom timestamp configuration using `SchemaTimestampsConfig`.
|
|
42
|
+
* The default value is `true`.
|
|
43
|
+
*
|
|
44
|
+
* @default true
|
|
45
|
+
*/
|
|
46
|
+
timestamps?: boolean | SchemaTimestampsConfig;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface CustomMongooseOptions {
|
|
50
|
+
/**
|
|
51
|
+
* A function that will be executed before the final build when using {@link buildMongooseModel}.
|
|
52
|
+
*/
|
|
53
|
+
beforeModelBuild?: <
|
|
54
|
+
DocType,
|
|
55
|
+
Model extends BaseMongoosePaginateModel<DocType, InstanceMethodsAndOverrides, QueryHelpers>,
|
|
56
|
+
InstanceMethodsAndOverrides = object,
|
|
57
|
+
QueryHelpers = object,
|
|
58
|
+
>(
|
|
59
|
+
schema: Schema<DocType, Model, InstanceMethodsAndOverrides, QueryHelpers>
|
|
60
|
+
) => void;
|
|
61
|
+
}
|