@kikiutils/mongoose 1.5.0 → 1.7.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/constants.cjs +2 -0
- package/constants.d.ts +4 -0
- package/constants.mjs +2 -0
- package/package.json +7 -3
- package/schema-builders/base.cjs +5 -7
- package/schema-builders/base.d.ts +2 -1
- package/schema-builders/base.mjs +5 -7
- package/schema-builders/boolean.cjs +1 -1
- package/schema-builders/boolean.d.ts +13 -9
- package/schema-builders/boolean.mjs +2 -2
- package/schema-builders/date.cjs +1 -1
- package/schema-builders/date.d.ts +16 -12
- package/schema-builders/date.mjs +2 -2
- package/schema-builders/decimal128.cjs +30 -0
- package/schema-builders/decimal128.d.ts +79 -0
- package/schema-builders/decimal128.mjs +24 -0
- package/schema-builders/index.cjs +8 -0
- package/schema-builders/index.d.ts +18 -10
- package/schema-builders/index.mjs +7 -1
- package/schema-builders/number.cjs +1 -1
- package/schema-builders/number.d.ts +16 -12
- package/schema-builders/number.mjs +2 -2
- package/schema-builders/object-id.cjs +8 -0
- package/schema-builders/object-id.d.ts +67 -0
- package/schema-builders/object-id.mjs +6 -0
- package/schema-builders/ref.cjs +9 -0
- package/schema-builders/ref.d.ts +64 -0
- package/schema-builders/ref.mjs +7 -0
- package/schema-builders/string.cjs +33 -1
- package/schema-builders/string.d.ts +35 -22
- package/schema-builders/string.mjs +30 -2
- package/types/schema.d.ts +4 -0
- package/utils.cjs +12 -0
- package/utils.d.ts +12 -0
- package/utils.mjs +12 -0
package/constants.cjs
CHANGED
|
@@ -9,6 +9,8 @@ const _connections = require('./_connections.cjs');
|
|
|
9
9
|
* This constant holds a set of commonly used Mongoose schema definitions, generated
|
|
10
10
|
* by the `createCommonMongooseSchemas` utility function. These schemas include common
|
|
11
11
|
* field types and configurations used across multiple models.
|
|
12
|
+
*
|
|
13
|
+
* @deprecated - Will be removed in the next major version. Use {@link schemaBuilders} instead.
|
|
12
14
|
*/
|
|
13
15
|
const commonMongooseSchemas = utils.createCommonMongooseSchemas();
|
|
14
16
|
|
package/constants.d.ts
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
export { mongooseConnections } from './_connections';
|
|
2
|
+
import type { schemaBuilders } from './schema-builders';
|
|
3
|
+
export type DoNotUseOrRemoveThisType = typeof schemaBuilders;
|
|
2
4
|
/**
|
|
3
5
|
* Common Mongoose schemas.
|
|
4
6
|
*
|
|
5
7
|
* This constant holds a set of commonly used Mongoose schema definitions, generated
|
|
6
8
|
* by the `createCommonMongooseSchemas` utility function. These schemas include common
|
|
7
9
|
* field types and configurations used across multiple models.
|
|
10
|
+
*
|
|
11
|
+
* @deprecated - Will be removed in the next major version. Use {@link schemaBuilders} instead.
|
|
8
12
|
*/
|
|
9
13
|
export declare const commonMongooseSchemas: {
|
|
10
14
|
readonly boolean: {
|
package/constants.mjs
CHANGED
|
@@ -7,6 +7,8 @@ export { mongooseConnections } from './_connections.mjs';
|
|
|
7
7
|
* This constant holds a set of commonly used Mongoose schema definitions, generated
|
|
8
8
|
* by the `createCommonMongooseSchemas` utility function. These schemas include common
|
|
9
9
|
* field types and configurations used across multiple models.
|
|
10
|
+
*
|
|
11
|
+
* @deprecated - Will be removed in the next major version. Use {@link schemaBuilders} instead.
|
|
10
12
|
*/
|
|
11
13
|
const commonMongooseSchemas = createCommonMongooseSchemas();
|
|
12
14
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kikiutils/mongoose",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.7.0",
|
|
4
4
|
"description": "A Mongoose plugin for enhanced JSON normalization and common schema creation, with built-in support for pagination and automatic Decimal128 conversion.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "kiki-kanri",
|
|
@@ -11,7 +11,8 @@
|
|
|
11
11
|
"scripts": {
|
|
12
12
|
"build": "ts-project-builder ./src/constants.ts ./src/schema-builders/index.ts ./src/utils.ts --clean --preserve-modules",
|
|
13
13
|
"bumplog": "changelogen --bump",
|
|
14
|
-
"release": "pnpm run build && changelogen --push --release && sh ./build-and-publish.sh"
|
|
14
|
+
"release": "pnpm run build && changelogen --push --release && sh ./build-and-publish.sh",
|
|
15
|
+
"test": "jest --coverage"
|
|
15
16
|
},
|
|
16
17
|
"dependencies": {
|
|
17
18
|
"decimal.js": "^10.4.3",
|
|
@@ -24,8 +25,11 @@
|
|
|
24
25
|
"@kikiutils/changelogen": "^0.7.0",
|
|
25
26
|
"@kikiutils/prettierrc": "^1.3.2",
|
|
26
27
|
"@kikiutils/tsconfigs": "^3.0.1",
|
|
28
|
+
"@types/jest": "^29.5.14",
|
|
27
29
|
"@types/lodash": "^4.17.12",
|
|
28
|
-
"@types/node": "^22.7.
|
|
30
|
+
"@types/node": "^22.7.9",
|
|
31
|
+
"jest": "^29.7.0",
|
|
32
|
+
"ts-jest": "^29.2.5",
|
|
29
33
|
"ts-project-builder": "^3.3.0",
|
|
30
34
|
"tslib": "^2.8.0"
|
|
31
35
|
},
|
package/schema-builders/base.cjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const
|
|
4
|
-
return () => {
|
|
5
|
-
|
|
3
|
+
const createBaseSchemaBuilderFactory = (type) => {
|
|
4
|
+
return (schema = {}) => {
|
|
5
|
+
schema.type = type;
|
|
6
6
|
return new Proxy(Object.freeze({}), {
|
|
7
7
|
get(_, key, receiver) {
|
|
8
8
|
if (typeof key === 'symbol')
|
|
@@ -11,10 +11,8 @@ const createSchemaBuilder = (type) => {
|
|
|
11
11
|
throw new Error(`Duplicate schema attribute: ${key}`);
|
|
12
12
|
if (isFunctionKeys.has(key))
|
|
13
13
|
return (value) => ((schema[key] = value), receiver);
|
|
14
|
-
if (key === 'length')
|
|
15
|
-
return (value) => ((schema['maxlength'] = schema['minlength'] = value), receiver);
|
|
16
14
|
if (key === 'nonRequired')
|
|
17
|
-
return schema;
|
|
15
|
+
return { ...schema };
|
|
18
16
|
if (key === 'required')
|
|
19
17
|
return { ...schema, required: true };
|
|
20
18
|
schema[key] = true;
|
|
@@ -32,4 +30,4 @@ const isFunctionKeys = new Set([
|
|
|
32
30
|
'minlength'
|
|
33
31
|
]);
|
|
34
32
|
|
|
35
|
-
exports.
|
|
33
|
+
exports.createBaseSchemaBuilderFactory = createBaseSchemaBuilderFactory;
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
import type { Schema } from 'mongoose';
|
|
2
|
+
export declare const createBaseSchemaBuilderFactory: <Builder = Readonly<{}>>(type: BooleanConstructor | DateConstructor | NumberConstructor | Schema.Types.ObjectId["constructor"] | StringConstructor) => (schema?: Record<string, any>) => Builder;
|
package/schema-builders/base.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
const
|
|
2
|
-
return () => {
|
|
3
|
-
|
|
1
|
+
const createBaseSchemaBuilderFactory = (type) => {
|
|
2
|
+
return (schema = {}) => {
|
|
3
|
+
schema.type = type;
|
|
4
4
|
return new Proxy(Object.freeze({}), {
|
|
5
5
|
get(_, key, receiver) {
|
|
6
6
|
if (typeof key === 'symbol')
|
|
@@ -9,10 +9,8 @@ const createSchemaBuilder = (type) => {
|
|
|
9
9
|
throw new Error(`Duplicate schema attribute: ${key}`);
|
|
10
10
|
if (isFunctionKeys.has(key))
|
|
11
11
|
return (value) => ((schema[key] = value), receiver);
|
|
12
|
-
if (key === 'length')
|
|
13
|
-
return (value) => ((schema['maxlength'] = schema['minlength'] = value), receiver);
|
|
14
12
|
if (key === 'nonRequired')
|
|
15
|
-
return schema;
|
|
13
|
+
return { ...schema };
|
|
16
14
|
if (key === 'required')
|
|
17
15
|
return { ...schema, required: true };
|
|
18
16
|
schema[key] = true;
|
|
@@ -30,4 +28,4 @@ const isFunctionKeys = new Set([
|
|
|
30
28
|
'minlength'
|
|
31
29
|
]);
|
|
32
30
|
|
|
33
|
-
export {
|
|
31
|
+
export { createBaseSchemaBuilderFactory };
|
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
import type { BooleanSchemaDefinition, DefaultType } from 'mongoose';
|
|
2
|
-
type ExtendBooleanSchemaBuilder<Props
|
|
3
|
-
export interface BooleanSchemaBuilder<Props = {
|
|
2
|
+
type ExtendBooleanSchemaBuilder<Props extends {
|
|
4
3
|
type: BooleanSchemaDefinition;
|
|
5
|
-
},
|
|
4
|
+
}, ExtraOmitFields extends string> = Omit<BooleanSchemaBuilder<Props, ExtraOmitFields>, ExtraOmitFields | keyof Props>;
|
|
5
|
+
export interface BooleanSchemaBuilder<Props extends {
|
|
6
|
+
type: BooleanSchemaDefinition;
|
|
7
|
+
} = {
|
|
8
|
+
type: BooleanSchemaDefinition;
|
|
9
|
+
}, ExtraOmitFields extends string = never, PropsWithRequired = Props & {
|
|
6
10
|
required: true;
|
|
7
11
|
}> {
|
|
8
12
|
default: <T extends DefaultType<D> | ((this: any, doc: any) => DefaultType<D>) | null, D extends boolean>(value: T) => ExtendBooleanSchemaBuilder<{
|
|
@@ -11,7 +15,7 @@ export interface BooleanSchemaBuilder<Props = {
|
|
|
11
15
|
})]: (Props & {
|
|
12
16
|
default: T;
|
|
13
17
|
})[key];
|
|
14
|
-
}>;
|
|
18
|
+
}, ExtraOmitFields>;
|
|
15
19
|
nonRequired: {
|
|
16
20
|
[key in keyof Props]: Props[key];
|
|
17
21
|
};
|
|
@@ -21,7 +25,7 @@ export interface BooleanSchemaBuilder<Props = {
|
|
|
21
25
|
})]: (Props & {
|
|
22
26
|
private: true;
|
|
23
27
|
})[key];
|
|
24
|
-
}>;
|
|
28
|
+
}, ExtraOmitFields>;
|
|
25
29
|
required: {
|
|
26
30
|
[key in keyof PropsWithRequired]: PropsWithRequired[key];
|
|
27
31
|
};
|
|
@@ -31,18 +35,18 @@ export interface BooleanSchemaBuilder<Props = {
|
|
|
31
35
|
})]: (Props & {
|
|
32
36
|
sparse: true;
|
|
33
37
|
})[key];
|
|
34
|
-
}>;
|
|
38
|
+
}, ExtraOmitFields>;
|
|
35
39
|
unique: ExtendBooleanSchemaBuilder<{
|
|
36
40
|
[key in keyof (Props & {
|
|
37
41
|
unique: true;
|
|
38
42
|
})]: (Props & {
|
|
39
43
|
unique: true;
|
|
40
44
|
})[key];
|
|
41
|
-
}>;
|
|
45
|
+
}, ExtraOmitFields>;
|
|
42
46
|
}
|
|
43
|
-
export declare const booleanSchemaBuilder: () => BooleanSchemaBuilder<{
|
|
47
|
+
export declare const booleanSchemaBuilder: (schema?: Record<string, any>) => BooleanSchemaBuilder<{
|
|
44
48
|
type: BooleanSchemaDefinition;
|
|
45
|
-
}, {
|
|
49
|
+
}, never, {
|
|
46
50
|
type: BooleanSchemaDefinition;
|
|
47
51
|
} & {
|
|
48
52
|
required: true;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createBaseSchemaBuilderFactory } from './base.mjs';
|
|
2
2
|
|
|
3
|
-
const booleanSchemaBuilder =
|
|
3
|
+
const booleanSchemaBuilder = createBaseSchemaBuilderFactory(Boolean);
|
|
4
4
|
|
|
5
5
|
export { booleanSchemaBuilder };
|
package/schema-builders/date.cjs
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
import type { DefaultType, DateSchemaDefinition } from 'mongoose';
|
|
2
2
|
import type { MaybeReadonly } from '../types/utils';
|
|
3
|
-
type ExtendDateSchemaBuilder<Props
|
|
4
|
-
export interface DateSchemaBuilder<Props = {
|
|
3
|
+
type ExtendDateSchemaBuilder<Props extends {
|
|
5
4
|
type: DateSchemaDefinition;
|
|
6
|
-
},
|
|
5
|
+
}, ExtraOmitFields extends string> = Omit<DateSchemaBuilder<Props, ExtraOmitFields>, ExtraOmitFields | keyof Props>;
|
|
6
|
+
export interface DateSchemaBuilder<Props extends {
|
|
7
|
+
type: DateSchemaDefinition;
|
|
8
|
+
} = {
|
|
9
|
+
type: DateSchemaDefinition;
|
|
10
|
+
}, ExtraOmitFields extends string = never, PropsWithRequired = Props & {
|
|
7
11
|
required: true;
|
|
8
12
|
}> {
|
|
9
13
|
default: <T extends DefaultType<D> | ((this: any, doc: any) => DefaultType<D>) | null, D extends NativeDate>(value: T) => ExtendDateSchemaBuilder<{
|
|
@@ -12,7 +16,7 @@ export interface DateSchemaBuilder<Props = {
|
|
|
12
16
|
})]: (Props & {
|
|
13
17
|
default: T;
|
|
14
18
|
})[key];
|
|
15
|
-
}>;
|
|
19
|
+
}, ExtraOmitFields>;
|
|
16
20
|
enum: <T extends MaybeReadonly<Array<D | null>> | {
|
|
17
21
|
message?: M;
|
|
18
22
|
values: MaybeReadonly<Array<D | null>>;
|
|
@@ -24,21 +28,21 @@ export interface DateSchemaBuilder<Props = {
|
|
|
24
28
|
})]: (Props & {
|
|
25
29
|
enum: T;
|
|
26
30
|
})[key];
|
|
27
|
-
}>;
|
|
31
|
+
}, ExtraOmitFields>;
|
|
28
32
|
max: <T extends D | MaybeReadonly<[D, S]>, D extends NativeDate, S extends string>(value: T) => ExtendDateSchemaBuilder<{
|
|
29
33
|
[key in keyof (Props & {
|
|
30
34
|
max: T;
|
|
31
35
|
})]: (Props & {
|
|
32
36
|
max: T;
|
|
33
37
|
})[key];
|
|
34
|
-
}>;
|
|
38
|
+
}, ExtraOmitFields>;
|
|
35
39
|
min: <T extends D | MaybeReadonly<[D, S]>, D extends NativeDate, S extends string>(value: T) => ExtendDateSchemaBuilder<{
|
|
36
40
|
[key in keyof (Props & {
|
|
37
41
|
min: T;
|
|
38
42
|
})]: (Props & {
|
|
39
43
|
min: T;
|
|
40
44
|
})[key];
|
|
41
|
-
}>;
|
|
45
|
+
}, ExtraOmitFields>;
|
|
42
46
|
nonRequired: {
|
|
43
47
|
[key in keyof Props]: Props[key];
|
|
44
48
|
};
|
|
@@ -48,7 +52,7 @@ export interface DateSchemaBuilder<Props = {
|
|
|
48
52
|
})]: (Props & {
|
|
49
53
|
private: true;
|
|
50
54
|
})[key];
|
|
51
|
-
}>;
|
|
55
|
+
}, ExtraOmitFields>;
|
|
52
56
|
required: {
|
|
53
57
|
[key in keyof PropsWithRequired]: PropsWithRequired[key];
|
|
54
58
|
};
|
|
@@ -58,18 +62,18 @@ export interface DateSchemaBuilder<Props = {
|
|
|
58
62
|
})]: (Props & {
|
|
59
63
|
sparse: true;
|
|
60
64
|
})[key];
|
|
61
|
-
}>;
|
|
65
|
+
}, ExtraOmitFields>;
|
|
62
66
|
unique: ExtendDateSchemaBuilder<{
|
|
63
67
|
[key in keyof (Props & {
|
|
64
68
|
unique: true;
|
|
65
69
|
})]: (Props & {
|
|
66
70
|
unique: true;
|
|
67
71
|
})[key];
|
|
68
|
-
}>;
|
|
72
|
+
}, ExtraOmitFields>;
|
|
69
73
|
}
|
|
70
|
-
export declare const dateSchemaBuilder: () => DateSchemaBuilder<{
|
|
74
|
+
export declare const dateSchemaBuilder: (schema?: Record<string, any>) => DateSchemaBuilder<{
|
|
71
75
|
type: DateSchemaDefinition;
|
|
72
|
-
}, {
|
|
76
|
+
}, never, {
|
|
73
77
|
type: DateSchemaDefinition;
|
|
74
78
|
} & {
|
|
75
79
|
required: true;
|
package/schema-builders/date.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createBaseSchemaBuilderFactory } from './base.mjs';
|
|
2
2
|
|
|
3
|
-
const dateSchemaBuilder =
|
|
3
|
+
const dateSchemaBuilder = createBaseSchemaBuilderFactory(Date);
|
|
4
4
|
|
|
5
5
|
export { dateSchemaBuilder };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const Decimal = require('decimal.js');
|
|
4
|
+
const mongoose = require('mongoose');
|
|
5
|
+
const base = require('./base.cjs');
|
|
6
|
+
|
|
7
|
+
const _interopDefaultCompat = e => e && typeof e === 'object' && 'default' in e ? e.default : e;
|
|
8
|
+
|
|
9
|
+
const Decimal__default = /*#__PURE__*/_interopDefaultCompat(Decimal);
|
|
10
|
+
|
|
11
|
+
const baseBuilderFactory = base.createBaseSchemaBuilderFactory(mongoose.Schema.Types.Decimal128);
|
|
12
|
+
const decimal128SchemaBuilder = () => {
|
|
13
|
+
const schema = {};
|
|
14
|
+
const baseBuilder = baseBuilderFactory(schema);
|
|
15
|
+
return new Proxy(baseBuilder, {
|
|
16
|
+
get(target, key, receiver) {
|
|
17
|
+
if (key === 'setRoundAndToFixedSetter') {
|
|
18
|
+
return (places = 2, rounding = Decimal__default.ROUND_DOWN) => {
|
|
19
|
+
schema['set'] = (value) => new Decimal__default(value.toString()).toFixed(places, rounding);
|
|
20
|
+
return receiver;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
if (key === 'setToStringGetter')
|
|
24
|
+
return (schema['get'] = (value) => value.toString()), receiver;
|
|
25
|
+
return Reflect.get(target, key, receiver);
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
exports.decimal128SchemaBuilder = decimal128SchemaBuilder;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import Decimal from 'decimal.js';
|
|
2
|
+
import { Schema } from 'mongoose';
|
|
3
|
+
import type { DefaultType, Types } from 'mongoose';
|
|
4
|
+
import type { MaybeReadonly } from '../types/utils';
|
|
5
|
+
type ExtendDecimal128SchemaBuilder<Props extends {
|
|
6
|
+
type: Schema.Types.Decimal128;
|
|
7
|
+
}, ExtraOmitFields extends string> = Omit<Decimal128SchemaBuilder<Props, ExtraOmitFields>, ExtraOmitFields | keyof Props>;
|
|
8
|
+
type ToStringGetterSchema = {
|
|
9
|
+
get: (value: Types.Decimal128) => string;
|
|
10
|
+
};
|
|
11
|
+
type ToStringSetterSchema = {
|
|
12
|
+
set: (value: {
|
|
13
|
+
toString(): string;
|
|
14
|
+
}) => string;
|
|
15
|
+
};
|
|
16
|
+
export interface Decimal128SchemaBuilder<Props extends {
|
|
17
|
+
type: Schema.Types.Decimal128;
|
|
18
|
+
} = {
|
|
19
|
+
type: Schema.Types.Decimal128;
|
|
20
|
+
}, ExtraOmitFields extends string = never> {
|
|
21
|
+
default: <T extends DefaultType<D> | ((this: any, doc: any) => DefaultType<D>) | null, D extends Types.Decimal128>(value: T) => ExtendDecimal128SchemaBuilder<{
|
|
22
|
+
[key in keyof (Props & {
|
|
23
|
+
default: T;
|
|
24
|
+
})]: (Props & {
|
|
25
|
+
default: T;
|
|
26
|
+
})[key];
|
|
27
|
+
}, ExtraOmitFields>;
|
|
28
|
+
enum: <T extends MaybeReadonly<Array<D | null>> | {
|
|
29
|
+
message?: M;
|
|
30
|
+
values: MaybeReadonly<Array<D | null>>;
|
|
31
|
+
} | {
|
|
32
|
+
[path: string]: D | null;
|
|
33
|
+
}, D extends Types.Decimal128, M extends string>(value: T) => ExtendDecimal128SchemaBuilder<{
|
|
34
|
+
[key in keyof (Props & {
|
|
35
|
+
enum: T;
|
|
36
|
+
})]: (Props & {
|
|
37
|
+
enum: T;
|
|
38
|
+
})[key];
|
|
39
|
+
}, ExtraOmitFields>;
|
|
40
|
+
nonRequired: {
|
|
41
|
+
[key in keyof Props]: Props[key];
|
|
42
|
+
};
|
|
43
|
+
private: ExtendDecimal128SchemaBuilder<{
|
|
44
|
+
[key in keyof (Props & {
|
|
45
|
+
private: true;
|
|
46
|
+
})]: (Props & {
|
|
47
|
+
private: true;
|
|
48
|
+
})[key];
|
|
49
|
+
}, ExtraOmitFields>;
|
|
50
|
+
required: {
|
|
51
|
+
[key in keyof (Props & {
|
|
52
|
+
required: true;
|
|
53
|
+
})]: (Props & {
|
|
54
|
+
required: true;
|
|
55
|
+
})[key];
|
|
56
|
+
};
|
|
57
|
+
setRoundAndToFixedSetter: (places?: number, rounding?: Decimal.Rounding) => ExtendDecimal128SchemaBuilder<{
|
|
58
|
+
[key in keyof (Props & ToStringSetterSchema)]: (Props & ToStringSetterSchema)[key];
|
|
59
|
+
}, ExtraOmitFields | 'setRoundAndToFixedSetter'>;
|
|
60
|
+
setToStringGetter: ExtendDecimal128SchemaBuilder<{
|
|
61
|
+
[key in keyof (Props & ToStringGetterSchema)]: (Props & ToStringGetterSchema)[key];
|
|
62
|
+
}, ExtraOmitFields | 'setToStringGetter'>;
|
|
63
|
+
sparse: ExtendDecimal128SchemaBuilder<{
|
|
64
|
+
[key in keyof (Props & {
|
|
65
|
+
sparse: true;
|
|
66
|
+
})]: (Props & {
|
|
67
|
+
sparse: true;
|
|
68
|
+
})[key];
|
|
69
|
+
}, ExtraOmitFields>;
|
|
70
|
+
unique: ExtendDecimal128SchemaBuilder<{
|
|
71
|
+
[key in keyof (Props & {
|
|
72
|
+
unique: true;
|
|
73
|
+
})]: (Props & {
|
|
74
|
+
unique: true;
|
|
75
|
+
})[key];
|
|
76
|
+
}, ExtraOmitFields>;
|
|
77
|
+
}
|
|
78
|
+
export declare const decimal128SchemaBuilder: () => Decimal128SchemaBuilder;
|
|
79
|
+
export {};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import Decimal from 'decimal.js';
|
|
2
|
+
import { Schema } from 'mongoose';
|
|
3
|
+
import { createBaseSchemaBuilderFactory } from './base.mjs';
|
|
4
|
+
|
|
5
|
+
const baseBuilderFactory = createBaseSchemaBuilderFactory(Schema.Types.Decimal128);
|
|
6
|
+
const decimal128SchemaBuilder = () => {
|
|
7
|
+
const schema = {};
|
|
8
|
+
const baseBuilder = baseBuilderFactory(schema);
|
|
9
|
+
return new Proxy(baseBuilder, {
|
|
10
|
+
get(target, key, receiver) {
|
|
11
|
+
if (key === 'setRoundAndToFixedSetter') {
|
|
12
|
+
return (places = 2, rounding = Decimal.ROUND_DOWN) => {
|
|
13
|
+
schema['set'] = (value) => new Decimal(value.toString()).toFixed(places, rounding);
|
|
14
|
+
return receiver;
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
if (key === 'setToStringGetter')
|
|
18
|
+
return (schema['get'] = (value) => value.toString()), receiver;
|
|
19
|
+
return Reflect.get(target, key, receiver);
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export { decimal128SchemaBuilder };
|
|
@@ -3,20 +3,28 @@
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
5
|
const date = require('./date.cjs');
|
|
6
|
+
const decimal128 = require('./decimal128.cjs');
|
|
6
7
|
const boolean = require('./boolean.cjs');
|
|
7
8
|
const number = require('./number.cjs');
|
|
9
|
+
const objectId = require('./object-id.cjs');
|
|
10
|
+
const ref = require('./ref.cjs');
|
|
8
11
|
const string = require('./string.cjs');
|
|
9
12
|
|
|
10
13
|
const schemaBuilders = {
|
|
11
14
|
date: date.dateSchemaBuilder,
|
|
15
|
+
decimal128: decimal128.decimal128SchemaBuilder,
|
|
12
16
|
boolean: boolean.booleanSchemaBuilder,
|
|
13
17
|
number: number.numberSchemaBuilder,
|
|
18
|
+
objectId: objectId.objectIdSchemaBuilder,
|
|
19
|
+
ref: ref.refSchemaBuilder,
|
|
14
20
|
string: string.stringSchemaBuilder
|
|
15
21
|
};
|
|
16
22
|
|
|
17
23
|
exports.dateSchemaBuilder = date.dateSchemaBuilder;
|
|
24
|
+
exports.decimal128SchemaBuilder = decimal128.decimal128SchemaBuilder;
|
|
18
25
|
exports.booleanSchemaBuilder = boolean.booleanSchemaBuilder;
|
|
19
26
|
exports.numberSchemaBuilder = number.numberSchemaBuilder;
|
|
27
|
+
exports.refSchemaBuilder = ref.refSchemaBuilder;
|
|
20
28
|
exports.stringSchemaBuilder = string.stringSchemaBuilder;
|
|
21
29
|
exports.default = schemaBuilders;
|
|
22
30
|
exports.schemaBuilders = schemaBuilders;
|
|
@@ -1,35 +1,43 @@
|
|
|
1
1
|
export * from './date';
|
|
2
|
+
export * from './decimal128';
|
|
2
3
|
export * from './boolean';
|
|
3
4
|
export * from './number';
|
|
5
|
+
export * from './ref';
|
|
4
6
|
export * from './string';
|
|
5
7
|
export declare const schemaBuilders: {
|
|
6
|
-
date: () => import("./date").DateSchemaBuilder<{
|
|
8
|
+
date: (schema?: Record<string, any>) => import("./date").DateSchemaBuilder<{
|
|
7
9
|
type: import("mongoose").DateSchemaDefinition;
|
|
8
|
-
}, {
|
|
10
|
+
}, never, {
|
|
9
11
|
type: import("mongoose").DateSchemaDefinition;
|
|
10
12
|
} & {
|
|
11
13
|
required: true;
|
|
12
14
|
}>;
|
|
13
|
-
|
|
15
|
+
decimal128: () => import("./decimal128").Decimal128SchemaBuilder;
|
|
16
|
+
boolean: (schema?: Record<string, any>) => import("./boolean").BooleanSchemaBuilder<{
|
|
14
17
|
type: import("mongoose").BooleanSchemaDefinition;
|
|
15
|
-
}, {
|
|
18
|
+
}, never, {
|
|
16
19
|
type: import("mongoose").BooleanSchemaDefinition;
|
|
17
20
|
} & {
|
|
18
21
|
required: true;
|
|
19
22
|
}>;
|
|
20
|
-
number: () => import("./number").NumberSchemaBuilder<{
|
|
23
|
+
number: (schema?: Record<string, any>) => import("./number").NumberSchemaBuilder<{
|
|
21
24
|
type: import("mongoose").NumberSchemaDefinition;
|
|
22
|
-
}, {
|
|
25
|
+
}, never, {
|
|
23
26
|
type: import("mongoose").NumberSchemaDefinition;
|
|
24
27
|
} & {
|
|
25
28
|
required: true;
|
|
26
29
|
}>;
|
|
27
|
-
|
|
28
|
-
type: import("mongoose").
|
|
29
|
-
}, {
|
|
30
|
-
type: import("mongoose").
|
|
30
|
+
objectId: (schema?: Record<string, any>) => import("./object-id").ObjectIdSchemaBuilder<{
|
|
31
|
+
type: import("mongoose").ObjectIdSchemaDefinition;
|
|
32
|
+
}, never, {
|
|
33
|
+
type: import("mongoose").ObjectIdSchemaDefinition;
|
|
31
34
|
} & {
|
|
32
35
|
required: true;
|
|
33
36
|
}>;
|
|
37
|
+
ref: <T extends string | import("mongoose").Model<any> | ((this: any, doc: any) => string | import("mongoose").Model<any>)>(ref: T) => import("./ref").RefSchemaBuilder<{
|
|
38
|
+
ref: T;
|
|
39
|
+
type: import("mongoose").ObjectIdSchemaDefinition;
|
|
40
|
+
}>;
|
|
41
|
+
string: () => import("./string").StringSchemaBuilder;
|
|
34
42
|
};
|
|
35
43
|
export default schemaBuilders;
|
|
@@ -1,13 +1,19 @@
|
|
|
1
1
|
import { dateSchemaBuilder } from './date.mjs';
|
|
2
|
+
import { decimal128SchemaBuilder } from './decimal128.mjs';
|
|
2
3
|
import { booleanSchemaBuilder } from './boolean.mjs';
|
|
3
4
|
import { numberSchemaBuilder } from './number.mjs';
|
|
5
|
+
import { objectIdSchemaBuilder } from './object-id.mjs';
|
|
6
|
+
import { refSchemaBuilder } from './ref.mjs';
|
|
4
7
|
import { stringSchemaBuilder } from './string.mjs';
|
|
5
8
|
|
|
6
9
|
const schemaBuilders = {
|
|
7
10
|
date: dateSchemaBuilder,
|
|
11
|
+
decimal128: decimal128SchemaBuilder,
|
|
8
12
|
boolean: booleanSchemaBuilder,
|
|
9
13
|
number: numberSchemaBuilder,
|
|
14
|
+
objectId: objectIdSchemaBuilder,
|
|
15
|
+
ref: refSchemaBuilder,
|
|
10
16
|
string: stringSchemaBuilder
|
|
11
17
|
};
|
|
12
18
|
|
|
13
|
-
export { booleanSchemaBuilder, dateSchemaBuilder, schemaBuilders as default, numberSchemaBuilder, schemaBuilders, stringSchemaBuilder };
|
|
19
|
+
export { booleanSchemaBuilder, dateSchemaBuilder, decimal128SchemaBuilder, schemaBuilders as default, numberSchemaBuilder, refSchemaBuilder, schemaBuilders, stringSchemaBuilder };
|
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
import type { DefaultType, NumberSchemaDefinition } from 'mongoose';
|
|
2
2
|
import type { MaybeReadonly } from '../types/utils';
|
|
3
|
-
type ExtendNumberSchemaBuilder<Props
|
|
4
|
-
export interface NumberSchemaBuilder<Props = {
|
|
3
|
+
type ExtendNumberSchemaBuilder<Props extends {
|
|
5
4
|
type: NumberSchemaDefinition;
|
|
6
|
-
},
|
|
5
|
+
}, ExtraOmitFields extends string> = Omit<NumberSchemaBuilder<Props, ExtraOmitFields>, ExtraOmitFields | keyof Props>;
|
|
6
|
+
export interface NumberSchemaBuilder<Props extends {
|
|
7
|
+
type: NumberSchemaDefinition;
|
|
8
|
+
} = {
|
|
9
|
+
type: NumberSchemaDefinition;
|
|
10
|
+
}, ExtraOmitFields extends string = never, PropsWithRequired = Props & {
|
|
7
11
|
required: true;
|
|
8
12
|
}> {
|
|
9
13
|
default: <T extends DefaultType<D> | ((this: any, doc: any) => DefaultType<D>) | null, D extends number>(value: T) => ExtendNumberSchemaBuilder<{
|
|
@@ -12,7 +16,7 @@ export interface NumberSchemaBuilder<Props = {
|
|
|
12
16
|
})]: (Props & {
|
|
13
17
|
default: T;
|
|
14
18
|
})[key];
|
|
15
|
-
}>;
|
|
19
|
+
}, ExtraOmitFields>;
|
|
16
20
|
enum: <T extends MaybeReadonly<Array<N | null>> | {
|
|
17
21
|
message?: M;
|
|
18
22
|
values: MaybeReadonly<Array<N | null>>;
|
|
@@ -24,21 +28,21 @@ export interface NumberSchemaBuilder<Props = {
|
|
|
24
28
|
})]: (Props & {
|
|
25
29
|
enum: T;
|
|
26
30
|
})[key];
|
|
27
|
-
}>;
|
|
31
|
+
}, ExtraOmitFields>;
|
|
28
32
|
max: <T extends N | MaybeReadonly<[N, S]>, N extends number, S extends string>(value: T) => ExtendNumberSchemaBuilder<{
|
|
29
33
|
[key in keyof (Props & {
|
|
30
34
|
max: T;
|
|
31
35
|
})]: (Props & {
|
|
32
36
|
max: T;
|
|
33
37
|
})[key];
|
|
34
|
-
}>;
|
|
38
|
+
}, ExtraOmitFields>;
|
|
35
39
|
min: <T extends N | MaybeReadonly<[N, S]>, N extends number, S extends string>(value: T) => ExtendNumberSchemaBuilder<{
|
|
36
40
|
[key in keyof (Props & {
|
|
37
41
|
min: T;
|
|
38
42
|
})]: (Props & {
|
|
39
43
|
min: T;
|
|
40
44
|
})[key];
|
|
41
|
-
}>;
|
|
45
|
+
}, ExtraOmitFields>;
|
|
42
46
|
nonRequired: {
|
|
43
47
|
[key in keyof Props]: Props[key];
|
|
44
48
|
};
|
|
@@ -48,7 +52,7 @@ export interface NumberSchemaBuilder<Props = {
|
|
|
48
52
|
})]: (Props & {
|
|
49
53
|
private: true;
|
|
50
54
|
})[key];
|
|
51
|
-
}>;
|
|
55
|
+
}, ExtraOmitFields>;
|
|
52
56
|
required: {
|
|
53
57
|
[key in keyof PropsWithRequired]: PropsWithRequired[key];
|
|
54
58
|
};
|
|
@@ -58,18 +62,18 @@ export interface NumberSchemaBuilder<Props = {
|
|
|
58
62
|
})]: (Props & {
|
|
59
63
|
sparse: true;
|
|
60
64
|
})[key];
|
|
61
|
-
}>;
|
|
65
|
+
}, ExtraOmitFields>;
|
|
62
66
|
unique: ExtendNumberSchemaBuilder<{
|
|
63
67
|
[key in keyof (Props & {
|
|
64
68
|
unique: true;
|
|
65
69
|
})]: (Props & {
|
|
66
70
|
unique: true;
|
|
67
71
|
})[key];
|
|
68
|
-
}>;
|
|
72
|
+
}, ExtraOmitFields>;
|
|
69
73
|
}
|
|
70
|
-
export declare const numberSchemaBuilder: () => NumberSchemaBuilder<{
|
|
74
|
+
export declare const numberSchemaBuilder: (schema?: Record<string, any>) => NumberSchemaBuilder<{
|
|
71
75
|
type: NumberSchemaDefinition;
|
|
72
|
-
}, {
|
|
76
|
+
}, never, {
|
|
73
77
|
type: NumberSchemaDefinition;
|
|
74
78
|
} & {
|
|
75
79
|
required: true;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createBaseSchemaBuilderFactory } from './base.mjs';
|
|
2
2
|
|
|
3
|
-
const numberSchemaBuilder =
|
|
3
|
+
const numberSchemaBuilder = createBaseSchemaBuilderFactory(Number);
|
|
4
4
|
|
|
5
5
|
export { numberSchemaBuilder };
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import type { DefaultType, ObjectIdSchemaDefinition, Types } from 'mongoose';
|
|
2
|
+
import type { MaybeReadonly } from '../types/utils';
|
|
3
|
+
type ExtendObjectIdSchemaBuilder<Props extends {
|
|
4
|
+
type: ObjectIdSchemaDefinition;
|
|
5
|
+
}, ExtraOmitFields extends string> = Omit<ObjectIdSchemaBuilder<Props, ExtraOmitFields>, ExtraOmitFields | keyof Props>;
|
|
6
|
+
export interface ObjectIdSchemaBuilder<Props extends {
|
|
7
|
+
type: ObjectIdSchemaDefinition;
|
|
8
|
+
} = {
|
|
9
|
+
type: ObjectIdSchemaDefinition;
|
|
10
|
+
}, ExtraOmitFields extends string = never, PropsWithRequired = Props & {
|
|
11
|
+
required: true;
|
|
12
|
+
}> {
|
|
13
|
+
default: <T extends DefaultType<D> | ((this: any, doc: any) => DefaultType<D>) | null, D extends Types.ObjectId>(value: T) => ExtendObjectIdSchemaBuilder<{
|
|
14
|
+
[key in keyof (Props & {
|
|
15
|
+
default: T;
|
|
16
|
+
})]: (Props & {
|
|
17
|
+
default: T;
|
|
18
|
+
})[key];
|
|
19
|
+
}, ExtraOmitFields>;
|
|
20
|
+
enum: <T extends MaybeReadonly<Array<O | null>> | {
|
|
21
|
+
message?: M;
|
|
22
|
+
values: MaybeReadonly<Array<O | null>>;
|
|
23
|
+
} | {
|
|
24
|
+
[path: string]: O | null;
|
|
25
|
+
}, M extends string, O extends Types.ObjectId>(value: T) => ExtendObjectIdSchemaBuilder<{
|
|
26
|
+
[key in keyof (Props & {
|
|
27
|
+
enum: T;
|
|
28
|
+
})]: (Props & {
|
|
29
|
+
enum: T;
|
|
30
|
+
})[key];
|
|
31
|
+
}, ExtraOmitFields>;
|
|
32
|
+
nonRequired: {
|
|
33
|
+
[key in keyof Props]: Props[key];
|
|
34
|
+
};
|
|
35
|
+
private: ExtendObjectIdSchemaBuilder<{
|
|
36
|
+
[key in keyof (Props & {
|
|
37
|
+
private: true;
|
|
38
|
+
})]: (Props & {
|
|
39
|
+
private: true;
|
|
40
|
+
})[key];
|
|
41
|
+
}, ExtraOmitFields>;
|
|
42
|
+
required: {
|
|
43
|
+
[key in keyof PropsWithRequired]: PropsWithRequired[key];
|
|
44
|
+
};
|
|
45
|
+
sparse: ExtendObjectIdSchemaBuilder<{
|
|
46
|
+
[key in keyof (Props & {
|
|
47
|
+
sparse: true;
|
|
48
|
+
})]: (Props & {
|
|
49
|
+
sparse: true;
|
|
50
|
+
})[key];
|
|
51
|
+
}, ExtraOmitFields>;
|
|
52
|
+
unique: ExtendObjectIdSchemaBuilder<{
|
|
53
|
+
[key in keyof (Props & {
|
|
54
|
+
unique: true;
|
|
55
|
+
})]: (Props & {
|
|
56
|
+
unique: true;
|
|
57
|
+
})[key];
|
|
58
|
+
}, ExtraOmitFields>;
|
|
59
|
+
}
|
|
60
|
+
export declare const objectIdSchemaBuilder: (schema?: Record<string, any>) => ObjectIdSchemaBuilder<{
|
|
61
|
+
type: ObjectIdSchemaDefinition;
|
|
62
|
+
}, never, {
|
|
63
|
+
type: ObjectIdSchemaDefinition;
|
|
64
|
+
} & {
|
|
65
|
+
required: true;
|
|
66
|
+
}>;
|
|
67
|
+
export {};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const mongoose = require('mongoose');
|
|
4
|
+
const base = require('./base.cjs');
|
|
5
|
+
|
|
6
|
+
const baseBuilderFactory = base.createBaseSchemaBuilderFactory(mongoose.Schema.Types.ObjectId);
|
|
7
|
+
const refSchemaBuilder = (ref) => baseBuilderFactory({ ref });
|
|
8
|
+
|
|
9
|
+
exports.refSchemaBuilder = refSchemaBuilder;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { DefaultType, Model, ObjectIdSchemaDefinition, Types } from 'mongoose';
|
|
2
|
+
import type { MaybeReadonly } from '../types/utils';
|
|
3
|
+
type ExtendRefSchemaBuilder<Props extends {
|
|
4
|
+
type: ObjectIdSchemaDefinition;
|
|
5
|
+
}, ExtraOmitFields extends string> = Omit<RefSchemaBuilder<Props, ExtraOmitFields>, ExtraOmitFields | keyof Props>;
|
|
6
|
+
export interface RefSchemaBuilder<Props extends {
|
|
7
|
+
type: ObjectIdSchemaDefinition;
|
|
8
|
+
} = {
|
|
9
|
+
type: ObjectIdSchemaDefinition;
|
|
10
|
+
}, ExtraOmitFields extends string = never, PropsWithRequired = Props & {
|
|
11
|
+
required: true;
|
|
12
|
+
}> {
|
|
13
|
+
default: <T extends DefaultType<D> | ((this: any, doc: any) => DefaultType<D>) | null, D extends Types.ObjectId>(value: T) => ExtendRefSchemaBuilder<{
|
|
14
|
+
[key in keyof (Props & {
|
|
15
|
+
default: T;
|
|
16
|
+
})]: (Props & {
|
|
17
|
+
default: T;
|
|
18
|
+
})[key];
|
|
19
|
+
}, ExtraOmitFields>;
|
|
20
|
+
enum: <T extends MaybeReadonly<Array<O | null>> | {
|
|
21
|
+
message?: M;
|
|
22
|
+
values: MaybeReadonly<Array<O | null>>;
|
|
23
|
+
} | {
|
|
24
|
+
[path: string]: O | null;
|
|
25
|
+
}, M extends string, O extends Types.ObjectId>(value: T) => ExtendRefSchemaBuilder<{
|
|
26
|
+
[key in keyof (Props & {
|
|
27
|
+
enum: T;
|
|
28
|
+
})]: (Props & {
|
|
29
|
+
enum: T;
|
|
30
|
+
})[key];
|
|
31
|
+
}, ExtraOmitFields>;
|
|
32
|
+
nonRequired: {
|
|
33
|
+
[key in keyof Props]: Props[key];
|
|
34
|
+
};
|
|
35
|
+
private: ExtendRefSchemaBuilder<{
|
|
36
|
+
[key in keyof (Props & {
|
|
37
|
+
private: true;
|
|
38
|
+
})]: (Props & {
|
|
39
|
+
private: true;
|
|
40
|
+
})[key];
|
|
41
|
+
}, ExtraOmitFields>;
|
|
42
|
+
required: {
|
|
43
|
+
[key in keyof PropsWithRequired]: PropsWithRequired[key];
|
|
44
|
+
};
|
|
45
|
+
sparse: ExtendRefSchemaBuilder<{
|
|
46
|
+
[key in keyof (Props & {
|
|
47
|
+
sparse: true;
|
|
48
|
+
})]: (Props & {
|
|
49
|
+
sparse: true;
|
|
50
|
+
})[key];
|
|
51
|
+
}, ExtraOmitFields>;
|
|
52
|
+
unique: ExtendRefSchemaBuilder<{
|
|
53
|
+
[key in keyof (Props & {
|
|
54
|
+
unique: true;
|
|
55
|
+
})]: (Props & {
|
|
56
|
+
unique: true;
|
|
57
|
+
})[key];
|
|
58
|
+
}, ExtraOmitFields>;
|
|
59
|
+
}
|
|
60
|
+
export declare const refSchemaBuilder: <T extends string | Model<any> | ((this: any, doc: any) => string | Model<any>)>(ref: T) => RefSchemaBuilder<{
|
|
61
|
+
ref: T;
|
|
62
|
+
type: ObjectIdSchemaDefinition;
|
|
63
|
+
}>;
|
|
64
|
+
export {};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Schema } from 'mongoose';
|
|
2
|
+
import { createBaseSchemaBuilderFactory } from './base.mjs';
|
|
3
|
+
|
|
4
|
+
const baseBuilderFactory = createBaseSchemaBuilderFactory(Schema.Types.ObjectId);
|
|
5
|
+
const refSchemaBuilder = (ref) => baseBuilderFactory({ ref });
|
|
6
|
+
|
|
7
|
+
export { refSchemaBuilder };
|
|
@@ -1,7 +1,39 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
const net = require('node:net');
|
|
3
4
|
const base = require('./base.cjs');
|
|
4
5
|
|
|
5
|
-
const
|
|
6
|
+
const _interopDefaultCompat = e => e && typeof e === 'object' && 'default' in e ? e.default : e;
|
|
7
|
+
|
|
8
|
+
const net__default = /*#__PURE__*/_interopDefaultCompat(net);
|
|
9
|
+
|
|
10
|
+
const baseBuilderFactory = base.createBaseSchemaBuilderFactory(String);
|
|
11
|
+
const defaultIPv4ValidateMessage = '`{VALUE}` is not a valid IPv4 address for path `{PATH}`.';
|
|
12
|
+
const defaultIPv6ValidateMessage = '`{VALUE}` is not a valid IPv6 address for path `{PATH}`.';
|
|
13
|
+
const stringSchemaBuilder = () => {
|
|
14
|
+
const schema = {};
|
|
15
|
+
const baseBuilder = baseBuilderFactory(schema);
|
|
16
|
+
return new Proxy(baseBuilder, {
|
|
17
|
+
get(target, key, receiver) {
|
|
18
|
+
if (key === 'ipv4') {
|
|
19
|
+
return (message = defaultIPv4ValidateMessage) => {
|
|
20
|
+
schema.trim = true;
|
|
21
|
+
schema.validate = { message, validator: (value) => net__default.isIPv4(value) };
|
|
22
|
+
return receiver;
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
if (key === 'ipv6') {
|
|
26
|
+
return (message = defaultIPv6ValidateMessage) => {
|
|
27
|
+
schema.trim = true;
|
|
28
|
+
schema.validate = { message, validator: (value) => net__default.isIPv6(value) };
|
|
29
|
+
return receiver;
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
if (key === 'length')
|
|
33
|
+
return (value) => ((schema['maxlength'] = schema['minlength'] = value), receiver);
|
|
34
|
+
return Reflect.get(target, key, receiver);
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
};
|
|
6
38
|
|
|
7
39
|
exports.stringSchemaBuilder = stringSchemaBuilder;
|
|
@@ -1,9 +1,20 @@
|
|
|
1
1
|
import type { DefaultType, StringSchemaDefinition } from 'mongoose';
|
|
2
2
|
import type { MaybeReadonly } from '../types/utils';
|
|
3
|
-
type ExtendStringSchemaBuilder<Props
|
|
4
|
-
export interface StringSchemaBuilder<Props = {
|
|
3
|
+
type ExtendStringSchemaBuilder<Props extends {
|
|
5
4
|
type: StringSchemaDefinition;
|
|
6
|
-
},
|
|
5
|
+
}, ExtraOmitFields extends string> = Omit<StringSchemaBuilder<Props, ExtraOmitFields>, ExtraOmitFields | keyof Props>;
|
|
6
|
+
type IPSchema<T extends string> = {
|
|
7
|
+
trim: true;
|
|
8
|
+
validate: {
|
|
9
|
+
message: T;
|
|
10
|
+
validator: (value: string) => boolean;
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
export interface StringSchemaBuilder<Props extends {
|
|
14
|
+
type: StringSchemaDefinition;
|
|
15
|
+
} = {
|
|
16
|
+
type: StringSchemaDefinition;
|
|
17
|
+
}, ExtraOmitFields extends string = never, PropsWithRequired = Props & {
|
|
7
18
|
required: true;
|
|
8
19
|
}> {
|
|
9
20
|
default: <T extends DefaultType<D> | ((this: any, doc: any) => DefaultType<D>) | null, D extends string>(value: T) => ExtendStringSchemaBuilder<{
|
|
@@ -12,7 +23,7 @@ export interface StringSchemaBuilder<Props = {
|
|
|
12
23
|
})]: (Props & {
|
|
13
24
|
default: T;
|
|
14
25
|
})[key];
|
|
15
|
-
}>;
|
|
26
|
+
}, ExtraOmitFields>;
|
|
16
27
|
enum: <T extends MaybeReadonly<Array<S | null>> | {
|
|
17
28
|
message?: M;
|
|
18
29
|
values: MaybeReadonly<Array<S | null>>;
|
|
@@ -24,7 +35,13 @@ export interface StringSchemaBuilder<Props = {
|
|
|
24
35
|
})]: (Props & {
|
|
25
36
|
enum: T;
|
|
26
37
|
})[key];
|
|
27
|
-
}>;
|
|
38
|
+
}, ExtraOmitFields>;
|
|
39
|
+
ipv4: <T extends string = typeof defaultIPv4ValidateMessage>(message?: T) => ExtendStringSchemaBuilder<{
|
|
40
|
+
[key in keyof (Props & IPSchema<T>)]: (Props & IPSchema<T>)[key];
|
|
41
|
+
}, ExtraOmitFields | 'ipv4' | 'ipv6'>;
|
|
42
|
+
ipv6: <T extends string = typeof defaultIPv6ValidateMessage>(message?: T) => ExtendStringSchemaBuilder<{
|
|
43
|
+
[key in keyof (Props & IPSchema<T>)]: (Props & IPSchema<T>)[key];
|
|
44
|
+
}, ExtraOmitFields | 'ipv4' | 'ipv6'>;
|
|
28
45
|
length: <T extends L | MaybeReadonly<[L, S]>, L extends number, S extends string>(value: T) => ExtendStringSchemaBuilder<{
|
|
29
46
|
[key in keyof (Props & {
|
|
30
47
|
maxlength: T;
|
|
@@ -33,28 +50,28 @@ export interface StringSchemaBuilder<Props = {
|
|
|
33
50
|
maxlength: T;
|
|
34
51
|
minlength: T;
|
|
35
52
|
})[key];
|
|
36
|
-
}>;
|
|
53
|
+
}, ExtraOmitFields>;
|
|
37
54
|
lowercase: ExtendStringSchemaBuilder<{
|
|
38
55
|
[key in keyof (Props & {
|
|
39
56
|
lowercase: true;
|
|
40
57
|
})]: (Props & {
|
|
41
58
|
lowercase: true;
|
|
42
59
|
})[key];
|
|
43
|
-
}>;
|
|
60
|
+
}, ExtraOmitFields>;
|
|
44
61
|
maxlength: <T extends L | MaybeReadonly<[L, S]>, L extends number, S extends string>(value: T) => ExtendStringSchemaBuilder<{
|
|
45
62
|
[key in keyof (Props & {
|
|
46
63
|
maxlength: T;
|
|
47
64
|
})]: (Props & {
|
|
48
65
|
maxlength: T;
|
|
49
66
|
})[key];
|
|
50
|
-
}>;
|
|
67
|
+
}, ExtraOmitFields>;
|
|
51
68
|
minlength: <T extends L | MaybeReadonly<[L, S]>, L extends number, S extends string>(value: T) => ExtendStringSchemaBuilder<{
|
|
52
69
|
[key in keyof (Props & {
|
|
53
70
|
minlength: T;
|
|
54
71
|
})]: (Props & {
|
|
55
72
|
minlength: T;
|
|
56
73
|
})[key];
|
|
57
|
-
}>;
|
|
74
|
+
}, ExtraOmitFields>;
|
|
58
75
|
nonRequired: {
|
|
59
76
|
[key in keyof Props]: Props[key];
|
|
60
77
|
};
|
|
@@ -64,7 +81,7 @@ export interface StringSchemaBuilder<Props = {
|
|
|
64
81
|
})]: (Props & {
|
|
65
82
|
private: true;
|
|
66
83
|
})[key];
|
|
67
|
-
}>;
|
|
84
|
+
}, ExtraOmitFields>;
|
|
68
85
|
required: {
|
|
69
86
|
[key in keyof PropsWithRequired]: PropsWithRequired[key];
|
|
70
87
|
};
|
|
@@ -74,41 +91,37 @@ export interface StringSchemaBuilder<Props = {
|
|
|
74
91
|
})]: (Props & {
|
|
75
92
|
sparse: true;
|
|
76
93
|
})[key];
|
|
77
|
-
}>;
|
|
94
|
+
}, ExtraOmitFields>;
|
|
78
95
|
text: ExtendStringSchemaBuilder<{
|
|
79
96
|
[key in keyof (Props & {
|
|
80
97
|
text: true;
|
|
81
98
|
})]: (Props & {
|
|
82
99
|
text: true;
|
|
83
100
|
})[key];
|
|
84
|
-
}>;
|
|
101
|
+
}, ExtraOmitFields>;
|
|
85
102
|
trim: ExtendStringSchemaBuilder<{
|
|
86
103
|
[key in keyof (Props & {
|
|
87
104
|
trim: true;
|
|
88
105
|
})]: (Props & {
|
|
89
106
|
trim: true;
|
|
90
107
|
})[key];
|
|
91
|
-
}>;
|
|
108
|
+
}, ExtraOmitFields>;
|
|
92
109
|
unique: ExtendStringSchemaBuilder<{
|
|
93
110
|
[key in keyof (Props & {
|
|
94
111
|
unique: true;
|
|
95
112
|
})]: (Props & {
|
|
96
113
|
unique: true;
|
|
97
114
|
})[key];
|
|
98
|
-
}>;
|
|
115
|
+
}, ExtraOmitFields>;
|
|
99
116
|
uppercase: ExtendStringSchemaBuilder<{
|
|
100
117
|
[key in keyof (Props & {
|
|
101
118
|
uppercase: true;
|
|
102
119
|
})]: (Props & {
|
|
103
120
|
uppercase: true;
|
|
104
121
|
})[key];
|
|
105
|
-
}>;
|
|
122
|
+
}, ExtraOmitFields>;
|
|
106
123
|
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
type: StringSchemaDefinition;
|
|
111
|
-
} & {
|
|
112
|
-
required: true;
|
|
113
|
-
}>;
|
|
124
|
+
declare const defaultIPv4ValidateMessage = "`{VALUE}` is not a valid IPv4 address for path `{PATH}`.";
|
|
125
|
+
declare const defaultIPv6ValidateMessage = "`{VALUE}` is not a valid IPv6 address for path `{PATH}`.";
|
|
126
|
+
export declare const stringSchemaBuilder: () => StringSchemaBuilder;
|
|
114
127
|
export {};
|
|
@@ -1,5 +1,33 @@
|
|
|
1
|
-
import
|
|
1
|
+
import net from 'node:net';
|
|
2
|
+
import { createBaseSchemaBuilderFactory } from './base.mjs';
|
|
2
3
|
|
|
3
|
-
const
|
|
4
|
+
const baseBuilderFactory = createBaseSchemaBuilderFactory(String);
|
|
5
|
+
const defaultIPv4ValidateMessage = '`{VALUE}` is not a valid IPv4 address for path `{PATH}`.';
|
|
6
|
+
const defaultIPv6ValidateMessage = '`{VALUE}` is not a valid IPv6 address for path `{PATH}`.';
|
|
7
|
+
const stringSchemaBuilder = () => {
|
|
8
|
+
const schema = {};
|
|
9
|
+
const baseBuilder = baseBuilderFactory(schema);
|
|
10
|
+
return new Proxy(baseBuilder, {
|
|
11
|
+
get(target, key, receiver) {
|
|
12
|
+
if (key === 'ipv4') {
|
|
13
|
+
return (message = defaultIPv4ValidateMessage) => {
|
|
14
|
+
schema.trim = true;
|
|
15
|
+
schema.validate = { message, validator: (value) => net.isIPv4(value) };
|
|
16
|
+
return receiver;
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
if (key === 'ipv6') {
|
|
20
|
+
return (message = defaultIPv6ValidateMessage) => {
|
|
21
|
+
schema.trim = true;
|
|
22
|
+
schema.validate = { message, validator: (value) => net.isIPv6(value) };
|
|
23
|
+
return receiver;
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
if (key === 'length')
|
|
27
|
+
return (value) => ((schema['maxlength'] = schema['minlength'] = value), receiver);
|
|
28
|
+
return Reflect.get(target, key, receiver);
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
};
|
|
4
32
|
|
|
5
33
|
export { stringSchemaBuilder };
|
package/types/schema.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import type Decimal from 'decimal.js';
|
|
2
2
|
import type { ObjectIdSchemaDefinition, SchemaTypeOptions, StringSchemaDefinition, Types } from 'mongoose';
|
|
3
|
+
import type { schemaBuilders } from '../schema-builders';
|
|
4
|
+
export type DoNotUseOrRemoveThisType = typeof schemaBuilders;
|
|
3
5
|
/**
|
|
4
6
|
* Base attributes for Mongoose schemas.
|
|
5
7
|
*
|
|
@@ -18,6 +20,8 @@ export type MongooseStringSchemaAttribute = BaseSchemaAttribute | 'lowercase' |
|
|
|
18
20
|
*
|
|
19
21
|
* This interface defines options for creating common Mongoose schemas, including
|
|
20
22
|
* settings for automatic rounding and fixed decimal places for Decimal128 fields.
|
|
23
|
+
*
|
|
24
|
+
* @deprecated - Will be removed in the next major version. Use {@link schemaBuilders} instead.
|
|
21
25
|
*/
|
|
22
26
|
export interface CreateCommonMongooseSchemasOptions {
|
|
23
27
|
/**
|
package/utils.cjs
CHANGED
|
@@ -60,6 +60,8 @@ function buildMongooseModel(collection, name, schema, options$1) {
|
|
|
60
60
|
* number, objectId, and string schemas, with optional custom configurations and rounding
|
|
61
61
|
* options for Decimal128 fields.
|
|
62
62
|
*
|
|
63
|
+
* @deprecated - Will be removed in the next major version. Use {@link schemaBuilders} instead.
|
|
64
|
+
*
|
|
63
65
|
* @template T - The type for custom schemas.
|
|
64
66
|
*
|
|
65
67
|
* @param customSchemas - Optional custom schemas to merge with the common schemas.
|
|
@@ -108,6 +110,9 @@ function createCommonMongooseSchemas(customSchemas, options) {
|
|
|
108
110
|
nonRequired: { type: Boolean },
|
|
109
111
|
required: baseRequiredBoolean
|
|
110
112
|
},
|
|
113
|
+
/**
|
|
114
|
+
* @deprecated - Will be removed in the next major version. Use {@link schemaBuilders.decimal128} instead.
|
|
115
|
+
*/
|
|
111
116
|
decimal128: {
|
|
112
117
|
autoRoundAndToFixed: {
|
|
113
118
|
nonRequired: baseAutoRoundAndToFixedDecimal128,
|
|
@@ -127,6 +132,9 @@ function createCommonMongooseSchemas(customSchemas, options) {
|
|
|
127
132
|
required: { ...baseRequiredNumber, unique: true }
|
|
128
133
|
}
|
|
129
134
|
},
|
|
135
|
+
/**
|
|
136
|
+
* @deprecated - Will be removed in the next major version. Use {@link schemaBuilders.objectId} instead.
|
|
137
|
+
*/
|
|
130
138
|
objectId: {
|
|
131
139
|
nonRequired: { type: mongoose__default.Schema.Types.ObjectId },
|
|
132
140
|
required: baseRequiredObjectId,
|
|
@@ -276,6 +284,8 @@ function createCommonMongooseSchemas(customSchemas, options) {
|
|
|
276
284
|
* This function generates a schema definition for an ObjectId reference field, including
|
|
277
285
|
* attributes such as 'private', 'required', and 'unique'.
|
|
278
286
|
*
|
|
287
|
+
* @deprecated - Will be removed in the next major version. Use {@link schemaBuilders.ref} instead.
|
|
288
|
+
*
|
|
279
289
|
* @template R - The type of the reference model name.
|
|
280
290
|
* @template T - An array of attributes to apply to the schema field.
|
|
281
291
|
*
|
|
@@ -342,6 +352,8 @@ async function mongooseDocumentOrObjectIdToDocument(documentOrObjectId, model, s
|
|
|
342
352
|
* This function adds a getter to the specified Decimal128 fields in the schema,
|
|
343
353
|
* converting the values to strings for easier use.
|
|
344
354
|
*
|
|
355
|
+
* @deprecated - Will be removed in the next major version. Use {@link schemaBuilders.decimal128} instead.
|
|
356
|
+
*
|
|
345
357
|
* @template DocType - The type of the document.
|
|
346
358
|
* @template Model - The type of the model, which extends `BaseMongoosePaginateModel`.
|
|
347
359
|
* @template InstanceMethodsAndOverrides - The type for instance methods and overrides.
|
package/utils.d.ts
CHANGED
|
@@ -35,6 +35,8 @@ export declare function buildMongooseModel<DocType, Model extends BaseMongoosePa
|
|
|
35
35
|
* number, objectId, and string schemas, with optional custom configurations and rounding
|
|
36
36
|
* options for Decimal128 fields.
|
|
37
37
|
*
|
|
38
|
+
* @deprecated - Will be removed in the next major version. Use {@link schemaBuilders} instead.
|
|
39
|
+
*
|
|
38
40
|
* @template T - The type for custom schemas.
|
|
39
41
|
*
|
|
40
42
|
* @param customSchemas - Optional custom schemas to merge with the common schemas.
|
|
@@ -77,6 +79,9 @@ export declare function createCommonMongooseSchemas<T extends {}>(customSchemas?
|
|
|
77
79
|
readonly type: BooleanConstructor;
|
|
78
80
|
};
|
|
79
81
|
};
|
|
82
|
+
/**
|
|
83
|
+
* @deprecated - Will be removed in the next major version. Use {@link schemaBuilders.decimal128} instead.
|
|
84
|
+
*/
|
|
80
85
|
readonly decimal128: {
|
|
81
86
|
readonly autoRoundAndToFixed: {
|
|
82
87
|
readonly nonRequired: {
|
|
@@ -124,6 +129,9 @@ export declare function createCommonMongooseSchemas<T extends {}>(customSchemas?
|
|
|
124
129
|
};
|
|
125
130
|
};
|
|
126
131
|
};
|
|
132
|
+
/**
|
|
133
|
+
* @deprecated - Will be removed in the next major version. Use {@link schemaBuilders.objectId} instead.
|
|
134
|
+
*/
|
|
127
135
|
readonly objectId: {
|
|
128
136
|
readonly nonRequired: {
|
|
129
137
|
readonly type: typeof mongoose.Schema.Types.ObjectId;
|
|
@@ -386,6 +394,8 @@ export declare function createCommonMongooseSchemas<T extends {}>(customSchemas?
|
|
|
386
394
|
* This function generates a schema definition for an ObjectId reference field, including
|
|
387
395
|
* attributes such as 'private', 'required', and 'unique'.
|
|
388
396
|
*
|
|
397
|
+
* @deprecated - Will be removed in the next major version. Use {@link schemaBuilders.ref} instead.
|
|
398
|
+
*
|
|
389
399
|
* @template R - The type of the reference model name.
|
|
390
400
|
* @template T - An array of attributes to apply to the schema field.
|
|
391
401
|
*
|
|
@@ -435,6 +445,8 @@ export declare function mongooseDocumentOrObjectIdToDocument<D extends MongooseH
|
|
|
435
445
|
* This function adds a getter to the specified Decimal128 fields in the schema,
|
|
436
446
|
* converting the values to strings for easier use.
|
|
437
447
|
*
|
|
448
|
+
* @deprecated - Will be removed in the next major version. Use {@link schemaBuilders.decimal128} instead.
|
|
449
|
+
*
|
|
438
450
|
* @template DocType - The type of the document.
|
|
439
451
|
* @template Model - The type of the model, which extends `BaseMongoosePaginateModel`.
|
|
440
452
|
* @template InstanceMethodsAndOverrides - The type for instance methods and overrides.
|
package/utils.mjs
CHANGED
|
@@ -49,6 +49,8 @@ function buildMongooseModel(collection, name, schema, options) {
|
|
|
49
49
|
* number, objectId, and string schemas, with optional custom configurations and rounding
|
|
50
50
|
* options for Decimal128 fields.
|
|
51
51
|
*
|
|
52
|
+
* @deprecated - Will be removed in the next major version. Use {@link schemaBuilders} instead.
|
|
53
|
+
*
|
|
52
54
|
* @template T - The type for custom schemas.
|
|
53
55
|
*
|
|
54
56
|
* @param customSchemas - Optional custom schemas to merge with the common schemas.
|
|
@@ -97,6 +99,9 @@ function createCommonMongooseSchemas(customSchemas, options) {
|
|
|
97
99
|
nonRequired: { type: Boolean },
|
|
98
100
|
required: baseRequiredBoolean
|
|
99
101
|
},
|
|
102
|
+
/**
|
|
103
|
+
* @deprecated - Will be removed in the next major version. Use {@link schemaBuilders.decimal128} instead.
|
|
104
|
+
*/
|
|
100
105
|
decimal128: {
|
|
101
106
|
autoRoundAndToFixed: {
|
|
102
107
|
nonRequired: baseAutoRoundAndToFixedDecimal128,
|
|
@@ -116,6 +121,9 @@ function createCommonMongooseSchemas(customSchemas, options) {
|
|
|
116
121
|
required: { ...baseRequiredNumber, unique: true }
|
|
117
122
|
}
|
|
118
123
|
},
|
|
124
|
+
/**
|
|
125
|
+
* @deprecated - Will be removed in the next major version. Use {@link schemaBuilders.objectId} instead.
|
|
126
|
+
*/
|
|
119
127
|
objectId: {
|
|
120
128
|
nonRequired: { type: mongoose.Schema.Types.ObjectId },
|
|
121
129
|
required: baseRequiredObjectId,
|
|
@@ -265,6 +273,8 @@ function createCommonMongooseSchemas(customSchemas, options) {
|
|
|
265
273
|
* This function generates a schema definition for an ObjectId reference field, including
|
|
266
274
|
* attributes such as 'private', 'required', and 'unique'.
|
|
267
275
|
*
|
|
276
|
+
* @deprecated - Will be removed in the next major version. Use {@link schemaBuilders.ref} instead.
|
|
277
|
+
*
|
|
268
278
|
* @template R - The type of the reference model name.
|
|
269
279
|
* @template T - An array of attributes to apply to the schema field.
|
|
270
280
|
*
|
|
@@ -331,6 +341,8 @@ async function mongooseDocumentOrObjectIdToDocument(documentOrObjectId, model, s
|
|
|
331
341
|
* This function adds a getter to the specified Decimal128 fields in the schema,
|
|
332
342
|
* converting the values to strings for easier use.
|
|
333
343
|
*
|
|
344
|
+
* @deprecated - Will be removed in the next major version. Use {@link schemaBuilders.decimal128} instead.
|
|
345
|
+
*
|
|
334
346
|
* @template DocType - The type of the document.
|
|
335
347
|
* @template Model - The type of the model, which extends `BaseMongoosePaginateModel`.
|
|
336
348
|
* @template InstanceMethodsAndOverrides - The type for instance methods and overrides.
|