@kikiutils/mongoose 7.0.3 → 8.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -8
- package/dist/_internals.d.ts +5 -2
- package/dist/_internals.d.ts.map +1 -1
- package/dist/_internals.js +2 -1
- package/dist/_internals.js.map +1 -1
- package/dist/assertions.d.ts +1 -2
- package/dist/assertions.d.ts.map +1 -1
- package/dist/builders.d.ts +3 -4
- package/dist/builders.d.ts.map +1 -1
- package/dist/builders.js +2 -3
- package/dist/builders.js.map +1 -1
- package/dist/connection.d.ts +5 -0
- package/dist/connection.d.ts.map +1 -0
- package/dist/connection.js +10 -0
- package/dist/connection.js.map +1 -0
- package/dist/options.d.ts +1 -2
- package/dist/options.d.ts.map +1 -1
- package/dist/options.js +2 -1
- package/dist/options.js.map +1 -1
- package/dist/plugins/assertions.d.ts +1 -2
- package/dist/plugins/assertions.d.ts.map +1 -1
- package/dist/plugins/normalize.d.ts +23 -17
- package/dist/plugins/normalize.d.ts.map +1 -1
- package/dist/plugins/normalize.js +19 -12
- package/dist/plugins/normalize.js.map +1 -1
- package/dist/schema-builders/base.d.ts +2 -3
- package/dist/schema-builders/base.d.ts.map +1 -1
- package/dist/schema-builders/base.js.map +1 -1
- package/dist/schema-builders/bigint.d.ts +48 -0
- package/dist/schema-builders/bigint.d.ts.map +1 -0
- package/dist/schema-builders/bigint.js +36 -0
- package/dist/schema-builders/bigint.js.map +1 -0
- package/dist/schema-builders/boolean.d.ts +2 -3
- package/dist/schema-builders/boolean.d.ts.map +1 -1
- package/dist/schema-builders/boolean.js.map +1 -1
- package/dist/schema-builders/date.d.ts +2 -3
- package/dist/schema-builders/date.d.ts.map +1 -1
- package/dist/schema-builders/decimal128.d.ts +33 -15
- package/dist/schema-builders/decimal128.d.ts.map +1 -1
- package/dist/schema-builders/decimal128.js +23 -0
- package/dist/schema-builders/decimal128.js.map +1 -1
- package/dist/schema-builders/double.d.ts +48 -0
- package/dist/schema-builders/double.d.ts.map +1 -0
- package/dist/schema-builders/double.js +37 -0
- package/dist/schema-builders/double.js.map +1 -0
- package/dist/schema-builders/index.d.ts +6 -1
- package/dist/schema-builders/index.js +6 -1
- package/dist/schema-builders/int32.d.ts +48 -0
- package/dist/schema-builders/int32.d.ts.map +1 -0
- package/dist/schema-builders/int32.js +36 -0
- package/dist/schema-builders/int32.js.map +1 -0
- package/dist/schema-builders/mixed.d.ts +34 -0
- package/dist/schema-builders/mixed.d.ts.map +1 -0
- package/dist/schema-builders/mixed.js +8 -0
- package/dist/schema-builders/mixed.js.map +1 -0
- package/dist/schema-builders/number.d.ts +2 -3
- package/dist/schema-builders/number.d.ts.map +1 -1
- package/dist/schema-builders/object-id.d.ts +2 -3
- package/dist/schema-builders/object-id.d.ts.map +1 -1
- package/dist/schema-builders/ref.d.ts +2 -3
- package/dist/schema-builders/ref.d.ts.map +1 -1
- package/dist/schema-builders/string.d.ts +56 -19
- package/dist/schema-builders/string.d.ts.map +1 -1
- package/dist/schema-builders/string.js +13 -1
- package/dist/schema-builders/string.js.map +1 -1
- package/dist/schema-builders/uuid.d.ts +34 -0
- package/dist/schema-builders/uuid.d.ts.map +1 -0
- package/dist/schema-builders/uuid.js +8 -0
- package/dist/schema-builders/uuid.js.map +1 -0
- package/dist/types/_internals.d.ts +1 -2
- package/dist/types/_internals.d.ts.map +1 -1
- package/dist/types/aggregate-paginate.d.ts +30 -14
- package/dist/types/aggregate-paginate.d.ts.map +1 -1
- package/dist/types/data.d.ts +2 -3
- package/dist/types/data.d.ts.map +1 -1
- package/dist/types/index.d.ts +10 -14
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/options.d.ts +4 -4
- package/dist/types/options.d.ts.map +1 -1
- package/dist/types/paginate.d.ts +15 -11
- package/dist/types/paginate.d.ts.map +1 -1
- package/package.json +23 -18
- package/src/_internals.ts +3 -0
- package/src/builders.ts +3 -10
- package/src/connection.ts +14 -0
- package/src/options.ts +8 -1
- package/src/plugins/normalize.ts +23 -15
- package/src/schema-builders/base.ts +8 -1
- package/src/schema-builders/bigint.ts +134 -0
- package/src/schema-builders/boolean.ts +1 -4
- package/src/schema-builders/decimal128.ts +92 -12
- package/src/schema-builders/double.ts +135 -0
- package/src/schema-builders/index.ts +15 -0
- package/src/schema-builders/int32.ts +135 -0
- package/src/schema-builders/mixed.ts +37 -0
- package/src/schema-builders/string.ts +90 -17
- package/src/schema-builders/uuid.ts +48 -0
- package/src/types/aggregate-paginate.ts +29 -8
- package/src/types/index.ts +0 -5
- package/src/types/options.ts +3 -0
- package/src/types/paginate.ts +6 -2
- package/dist/constants.d.ts +0 -6
- package/dist/constants.d.ts.map +0 -1
- package/dist/constants.js +0 -6
- package/dist/constants.js.map +0 -1
- package/dist/helpers.d.ts +0 -6
- package/dist/helpers.d.ts.map +0 -1
- package/dist/helpers.js +0 -9
- package/dist/helpers.js.map +0 -1
- package/src/constants.ts +0 -3
- package/src/helpers.ts +0 -5
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kikiutils/mongoose",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "
|
|
5
|
-
"description": "
|
|
4
|
+
"version": "8.0.0",
|
|
5
|
+
"description": "Provides Mongoose schema builders and utilities for connections, pagination, JSON normalization, and update checks.",
|
|
6
6
|
"author": "kiki-kanri",
|
|
7
7
|
"license": "MIT",
|
|
8
8
|
"repository": {
|
|
@@ -10,14 +10,18 @@
|
|
|
10
10
|
"url": "git+https://github.com/kikiutils/node-mongoose.git"
|
|
11
11
|
},
|
|
12
12
|
"keywords": [
|
|
13
|
-
"
|
|
13
|
+
"bson",
|
|
14
|
+
"decimal128",
|
|
14
15
|
"json",
|
|
15
|
-
"
|
|
16
|
+
"mongodb",
|
|
16
17
|
"mongoose",
|
|
17
18
|
"normalization",
|
|
18
19
|
"pagination",
|
|
19
20
|
"plugin",
|
|
20
|
-
"schema"
|
|
21
|
+
"schema",
|
|
22
|
+
"schema-builder",
|
|
23
|
+
"typescript",
|
|
24
|
+
"uuid"
|
|
21
25
|
],
|
|
22
26
|
"sideEffects": false,
|
|
23
27
|
"exports": {
|
|
@@ -74,27 +78,28 @@
|
|
|
74
78
|
"test:watch": "vitest watch --coverage",
|
|
75
79
|
"typecheck": "tsc -b --noEmit"
|
|
76
80
|
},
|
|
81
|
+
"peerDependenciesMeta": {},
|
|
77
82
|
"dependencies": {
|
|
78
83
|
"decimal.js": "^10.6.0",
|
|
79
|
-
"es-toolkit": "^1.
|
|
80
|
-
"mongoose": "^9.
|
|
81
|
-
"mongoose-aggregate-paginate-v2": "^1.1.
|
|
84
|
+
"es-toolkit": "^1.52.0",
|
|
85
|
+
"mongoose": "^9.10.3",
|
|
86
|
+
"mongoose-aggregate-paginate-v2": "^1.1.5",
|
|
82
87
|
"mongoose-paginate-v2": "^1.9.5",
|
|
83
|
-
"type-fest": "^5.
|
|
88
|
+
"type-fest": "^5.10.0"
|
|
84
89
|
},
|
|
85
90
|
"devDependencies": {
|
|
86
|
-
"@antfu/eslint-config": "^9.
|
|
87
|
-
"@kikiutils/eslint-config": "^9.2.
|
|
91
|
+
"@antfu/eslint-config": "^9.5.1",
|
|
92
|
+
"@kikiutils/eslint-config": "^9.2.1",
|
|
88
93
|
"@kikiutils/tsconfigs": "^7.0.0",
|
|
89
|
-
"@types/node": "^26.
|
|
90
|
-
"@vitest/coverage-v8": "^
|
|
94
|
+
"@types/node": "^26.6.4",
|
|
95
|
+
"@vitest/coverage-v8": "^5.0.3",
|
|
91
96
|
"changelogen": "^0.6.2",
|
|
92
|
-
"eslint": "^10.
|
|
93
|
-
"knip": "^6.
|
|
94
|
-
"publint": "^0.3.
|
|
95
|
-
"tsdown": "^0.
|
|
97
|
+
"eslint": "^10.12.0",
|
|
98
|
+
"knip": "^6.39.0",
|
|
99
|
+
"publint": "^0.3.25",
|
|
100
|
+
"tsdown": "^0.23.0",
|
|
96
101
|
"typescript": "^6.0.3",
|
|
97
102
|
"unplugin-unused": "^0.6.0",
|
|
98
|
-
"vitest": "^
|
|
103
|
+
"vitest": "^5.0.3"
|
|
99
104
|
}
|
|
100
105
|
}
|
package/src/_internals.ts
CHANGED
package/src/builders.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import mongoose from 'mongoose';
|
|
2
1
|
import type {
|
|
3
2
|
Model as MongooseModel,
|
|
4
3
|
Schema,
|
|
@@ -7,7 +6,7 @@ import mongooseAggregatePaginate from 'mongoose-aggregate-paginate-v2';
|
|
|
7
6
|
import mongoosePaginate from 'mongoose-paginate-v2';
|
|
8
7
|
|
|
9
8
|
import { customMongooseOptions } from './_internals';
|
|
10
|
-
import {
|
|
9
|
+
import { getDefaultMongooseConnection } from './connection';
|
|
11
10
|
import { mongooseAssertionsPlugin } from './plugins/assertions';
|
|
12
11
|
import { mongooseNormalizePlugin } from './plugins/normalize';
|
|
13
12
|
import type { MongooseNormalizePluginOptions } from './plugins/normalize';
|
|
@@ -36,16 +35,10 @@ export function buildMongooseModel<
|
|
|
36
35
|
schema.plugin(mongoosePaginate);
|
|
37
36
|
schema.set('timestamps', options?.timestamps ?? schema.get('timestamps') ?? true);
|
|
38
37
|
customMongooseOptions.beforeModelBuild?.(schema);
|
|
39
|
-
const connection =
|
|
40
|
-
options?.connection
|
|
41
|
-
|| (
|
|
42
|
-
mongooseConnections.default ||= mongoose.createConnection(
|
|
43
|
-
process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017',
|
|
44
|
-
)
|
|
45
|
-
);
|
|
46
|
-
|
|
38
|
+
const connection = options?.connection ?? getDefaultMongooseConnection();
|
|
47
39
|
return connection.model<DocType, Model, QueryHelpers>(name, schema, collection);
|
|
48
40
|
}
|
|
41
|
+
|
|
49
42
|
export function registerMongooseNormalizePlugin<
|
|
50
43
|
DocType,
|
|
51
44
|
Model extends MongooseModel<DocType, QueryHelpers, InstanceMethodsAndOverrides>,
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import mongoose from 'mongoose';
|
|
2
|
+
import type { Connection } from 'mongoose';
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
customMongooseOptions,
|
|
6
|
+
mongooseConnectionState,
|
|
7
|
+
} from './_internals';
|
|
8
|
+
|
|
9
|
+
export function getDefaultMongooseConnection(): Connection {
|
|
10
|
+
return mongooseConnectionState.defaultConnection ||= mongoose.createConnection(
|
|
11
|
+
process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017',
|
|
12
|
+
customMongooseOptions.defaultConnectionOptions,
|
|
13
|
+
);
|
|
14
|
+
}
|
package/src/options.ts
CHANGED
|
@@ -1,9 +1,16 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
customMongooseOptions,
|
|
3
|
+
mongooseConnectionState,
|
|
4
|
+
} from './_internals';
|
|
2
5
|
import type { CustomMongooseOptions } from './types/options';
|
|
3
6
|
|
|
4
7
|
export function setCustomMongooseOptions<K extends keyof CustomMongooseOptions>(
|
|
5
8
|
key: K,
|
|
6
9
|
value: CustomMongooseOptions[K],
|
|
7
10
|
) {
|
|
11
|
+
if (key === 'defaultConnectionOptions' && mongooseConnectionState.defaultConnection) {
|
|
12
|
+
throw new Error('Default connection options must be set before the default connection is created.');
|
|
13
|
+
}
|
|
14
|
+
|
|
8
15
|
customMongooseOptions[key] = value;
|
|
9
16
|
}
|
package/src/plugins/normalize.ts
CHANGED
|
@@ -8,34 +8,41 @@ import type { Schema } from 'mongoose';
|
|
|
8
8
|
|
|
9
9
|
export interface MongooseNormalizePluginOptions {
|
|
10
10
|
/**
|
|
11
|
-
* Whether to
|
|
12
|
-
* If true, the `_id` field will be replaced with `id`.
|
|
11
|
+
* Whether to move the serialized `_id` value to `id`.
|
|
13
12
|
*
|
|
14
|
-
* @
|
|
13
|
+
* @remarks
|
|
14
|
+
* If `false`, preserves the value under `_id` instead. Only values other than `undefined` are copied.
|
|
15
|
+
*
|
|
16
|
+
* @defaultValue `true`.
|
|
15
17
|
*/
|
|
16
18
|
convertIdField?: boolean;
|
|
17
19
|
|
|
18
20
|
/**
|
|
19
|
-
* Whether to convert
|
|
21
|
+
* Whether to convert a serialized `ObjectId` identifier to its hexadecimal string.
|
|
22
|
+
*
|
|
23
|
+
* @remarks
|
|
24
|
+
* Applies before choosing the `id` or `_id` output key. Identifiers of other types are unchanged.
|
|
20
25
|
*
|
|
21
|
-
* @
|
|
26
|
+
* @defaultValue `true`.
|
|
22
27
|
*/
|
|
23
28
|
toHexIdIfObjectId?: boolean;
|
|
24
29
|
}
|
|
25
30
|
|
|
26
31
|
/**
|
|
27
|
-
*
|
|
32
|
+
* Registers JSON normalization on a Mongoose schema.
|
|
28
33
|
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
* - Convert `Decimal128` fields to strings
|
|
34
|
-
* - Remove the `__v` version key
|
|
34
|
+
* @remarks
|
|
35
|
+
* Mutates the schema's `toJSON` configuration while preserving its other options. The transform removes `__v`,
|
|
36
|
+
* normalizes `_id` according to the plugin options, removes schema paths marked `private`, and converts
|
|
37
|
+
* truthy serialized values at registered `Decimal128` paths to strings.
|
|
35
38
|
*
|
|
36
|
-
*
|
|
39
|
+
* If an existing transform is a function, invokes it after normalization. If it returns `undefined`,
|
|
40
|
+
* preserves the normalized output and any mutations made by that transform; otherwise, uses its return value.
|
|
41
|
+
* That transform can change the normalized output, and its errors propagate during serialization.
|
|
42
|
+
* This plugin does not configure `toObject` or recursively register itself on child schemas.
|
|
37
43
|
*
|
|
38
|
-
* @param schema - The
|
|
44
|
+
* @param schema - The schema whose JSON serialization configuration is modified.
|
|
45
|
+
* @param pluginOptions - The identifier normalization options; omitted options use their documented defaults.
|
|
39
46
|
*/
|
|
40
47
|
export function mongooseNormalizePlugin<S extends Schema>(
|
|
41
48
|
schema: S,
|
|
@@ -87,7 +94,8 @@ export function mongooseNormalizePlugin<S extends Schema>(
|
|
|
87
94
|
|
|
88
95
|
// Run original toJSON transform
|
|
89
96
|
if (toJsonTransform && typeof toJsonTransform !== 'boolean') {
|
|
90
|
-
|
|
97
|
+
const transformed = toJsonTransform(doc as any, copiedRet as any, options as any);
|
|
98
|
+
return transformed === undefined ? copiedRet : transformed;
|
|
91
99
|
}
|
|
92
100
|
|
|
93
101
|
// Return normalized object
|
|
@@ -5,8 +5,14 @@ type BaseSchemaType =
|
|
|
5
5
|
| DateConstructor
|
|
6
6
|
| NumberConstructor
|
|
7
7
|
| Schema.Types.ObjectId['constructor']
|
|
8
|
-
| StringConstructor
|
|
8
|
+
| StringConstructor
|
|
9
|
+
| typeof Schema.Types.BigInt
|
|
10
|
+
| typeof Schema.Types.Double
|
|
11
|
+
| typeof Schema.Types.Int32
|
|
12
|
+
| typeof Schema.Types.Mixed
|
|
13
|
+
| typeof Schema.Types.UUID;
|
|
9
14
|
|
|
15
|
+
// Constants/Variables
|
|
10
16
|
const isFunctionKeys = new Set([
|
|
11
17
|
'default',
|
|
12
18
|
'enum',
|
|
@@ -17,6 +23,7 @@ const isFunctionKeys = new Set([
|
|
|
17
23
|
'minlength',
|
|
18
24
|
]);
|
|
19
25
|
|
|
26
|
+
// Functions
|
|
20
27
|
export function createBaseSchemaBuilderFactory<Builder = Readonly<Record<string, any>>>(type: BaseSchemaType) {
|
|
21
28
|
return (schema: Record<string, any> = {}) => {
|
|
22
29
|
schema.type = type;
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { Schema } from 'mongoose';
|
|
2
|
+
import type {
|
|
3
|
+
DefaultType,
|
|
4
|
+
IndexDirection,
|
|
5
|
+
IndexOptions,
|
|
6
|
+
} from 'mongoose';
|
|
7
|
+
import type { Merge } from 'type-fest';
|
|
8
|
+
|
|
9
|
+
import type { Readonlyable } from '../types/_internals';
|
|
10
|
+
|
|
11
|
+
import { createBaseSchemaBuilderFactory } from './base';
|
|
12
|
+
|
|
13
|
+
type BigIntLimit = bigint;
|
|
14
|
+
type ExtendSchemaBuilder<
|
|
15
|
+
Props extends BaseProps,
|
|
16
|
+
ExtraOmitFields extends string,
|
|
17
|
+
> = Omit<
|
|
18
|
+
BigIntSchemaBuilder<Props, ExtraOmitFields>,
|
|
19
|
+
ExtraOmitFields | keyof Props
|
|
20
|
+
>;
|
|
21
|
+
|
|
22
|
+
interface BaseProps {
|
|
23
|
+
type: typeof Schema.Types.BigInt;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
interface BigIntLimitValidator {
|
|
27
|
+
max?: bigint;
|
|
28
|
+
message: string;
|
|
29
|
+
min?: bigint;
|
|
30
|
+
type: 'max' | 'min';
|
|
31
|
+
validator: (value: bigint | null | undefined) => boolean;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface BigIntSchemaBuilder<Props extends BaseProps = BaseProps, ExtraOmitFields extends string = never> {
|
|
35
|
+
default: <
|
|
36
|
+
T extends ((this: any, doc: any) => DefaultType<D> | null | undefined) | DefaultType<D> | null | undefined,
|
|
37
|
+
D extends bigint,
|
|
38
|
+
>(value: T) => ExtendSchemaBuilder<
|
|
39
|
+
Merge<Props, { default: T }>,
|
|
40
|
+
ExtraOmitFields
|
|
41
|
+
>;
|
|
42
|
+
|
|
43
|
+
immutable: ExtendSchemaBuilder<Merge<Props, { immutable: true }>, ExtraOmitFields>;
|
|
44
|
+
index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendSchemaBuilder<
|
|
45
|
+
Merge<Props, { index: T }>,
|
|
46
|
+
ExtraOmitFields
|
|
47
|
+
>;
|
|
48
|
+
|
|
49
|
+
max: <
|
|
50
|
+
T extends L | Readonlyable<[L, S]>,
|
|
51
|
+
L extends BigIntLimit,
|
|
52
|
+
S extends string,
|
|
53
|
+
>(value: T) => ExtendSchemaBuilder<
|
|
54
|
+
Merge<Props, BigIntValidationSchema>,
|
|
55
|
+
'max' | ExtraOmitFields
|
|
56
|
+
>;
|
|
57
|
+
|
|
58
|
+
min: <
|
|
59
|
+
T extends L | Readonlyable<[L, S]>,
|
|
60
|
+
L extends BigIntLimit,
|
|
61
|
+
S extends string,
|
|
62
|
+
>(value: T) => ExtendSchemaBuilder<
|
|
63
|
+
Merge<Props, BigIntValidationSchema>,
|
|
64
|
+
'min' | ExtraOmitFields
|
|
65
|
+
>;
|
|
66
|
+
|
|
67
|
+
nonRequired: Props;
|
|
68
|
+
private: ExtendSchemaBuilder<Merge<Props, { private: true }>, ExtraOmitFields>;
|
|
69
|
+
required: Merge<Props, { required: true }>;
|
|
70
|
+
sparse: ExtendSchemaBuilder<Merge<Props, { sparse: true }>, ExtraOmitFields>;
|
|
71
|
+
unique: ExtendSchemaBuilder<Merge<Props, { unique: true }>, ExtraOmitFields>;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
interface BigIntValidationSchema {
|
|
75
|
+
validate: BigIntLimitValidator[];
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Constants/Variables
|
|
79
|
+
const defaultMinValidateMessage = 'Path `{PATH}` ({VALUE}) is less than minimum allowed value ({MIN}).';
|
|
80
|
+
const defaultMaxValidateMessage = 'Path `{PATH}` ({VALUE}) is more than maximum allowed value ({MAX}).';
|
|
81
|
+
|
|
82
|
+
// Functions
|
|
83
|
+
const baseBuilderFactory = createBaseSchemaBuilderFactory(Schema.Types.BigInt);
|
|
84
|
+
|
|
85
|
+
export function bigintSchemaBuilder() {
|
|
86
|
+
const schema: Record<string, any> = {};
|
|
87
|
+
const baseBuilder = baseBuilderFactory(schema);
|
|
88
|
+
return new Proxy(
|
|
89
|
+
baseBuilder,
|
|
90
|
+
{
|
|
91
|
+
get(target, key, receiver) {
|
|
92
|
+
if (key === 'max' || key === 'min') {
|
|
93
|
+
return (value: BigIntLimit | Readonlyable<[BigIntLimit, string]>) => {
|
|
94
|
+
const validator = createLimitValidator(key, value);
|
|
95
|
+
const validators = Array.isArray(schema.validate) ? schema.validate : [];
|
|
96
|
+
schema.validate = [
|
|
97
|
+
...validators.filter(({ type }) => type !== key),
|
|
98
|
+
validator,
|
|
99
|
+
];
|
|
100
|
+
|
|
101
|
+
return receiver;
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return Reflect.get(target, key, receiver);
|
|
106
|
+
},
|
|
107
|
+
},
|
|
108
|
+
) as BigIntSchemaBuilder;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function createLimitValidator(
|
|
112
|
+
type: 'max' | 'min',
|
|
113
|
+
value: BigIntLimit | Readonlyable<[BigIntLimit, string]>,
|
|
114
|
+
): BigIntLimitValidator {
|
|
115
|
+
const [limit, message] = Array.isArray(value)
|
|
116
|
+
? value as Readonly<[BigIntLimit, string]>
|
|
117
|
+
: [
|
|
118
|
+
value as BigIntLimit,
|
|
119
|
+
undefined,
|
|
120
|
+
];
|
|
121
|
+
|
|
122
|
+
if (typeof limit !== 'bigint') throw new TypeError(`BigInt ${type} limit must be a bigint`);
|
|
123
|
+
|
|
124
|
+
return {
|
|
125
|
+
message: message ?? (type === 'min' ? defaultMinValidateMessage : defaultMaxValidateMessage),
|
|
126
|
+
[type]: limit,
|
|
127
|
+
type,
|
|
128
|
+
validator: (value) => {
|
|
129
|
+
if (value === null || value === undefined) return true;
|
|
130
|
+
|
|
131
|
+
return type === 'min' ? value >= limit : value <= limit;
|
|
132
|
+
},
|
|
133
|
+
};
|
|
134
|
+
}
|
|
@@ -20,10 +20,7 @@ interface BaseProps {
|
|
|
20
20
|
type: BooleanSchemaDefinition;
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
-
export interface BooleanSchemaBuilder<
|
|
24
|
-
Props extends BaseProps = BaseProps,
|
|
25
|
-
ExtraOmitFields extends string = never,
|
|
26
|
-
> {
|
|
23
|
+
export interface BooleanSchemaBuilder<Props extends BaseProps = BaseProps, ExtraOmitFields extends string = never> {
|
|
27
24
|
default: <
|
|
28
25
|
T extends ((this: any, doc: any) => DefaultType<D>) | DefaultType<D> | null,
|
|
29
26
|
D extends boolean,
|
|
@@ -12,6 +12,7 @@ import type { Readonlyable } from '../types/_internals';
|
|
|
12
12
|
|
|
13
13
|
import { createBaseSchemaBuilderFactory } from './base';
|
|
14
14
|
|
|
15
|
+
type Decimal128Limit = Decimal.Value | Types.Decimal128 | { toString: () => string };
|
|
15
16
|
type ExtendSchemaBuilder<
|
|
16
17
|
Props extends BaseProps,
|
|
17
18
|
ExtraOmitFields extends string,
|
|
@@ -24,6 +25,14 @@ interface BaseProps {
|
|
|
24
25
|
type: typeof Schema.Types.Decimal128;
|
|
25
26
|
}
|
|
26
27
|
|
|
28
|
+
interface Decimal128LimitValidator {
|
|
29
|
+
max?: string;
|
|
30
|
+
message: string;
|
|
31
|
+
min?: string;
|
|
32
|
+
type: 'max' | 'min';
|
|
33
|
+
validator: (value: null | Types.Decimal128 | undefined) => boolean;
|
|
34
|
+
}
|
|
35
|
+
|
|
27
36
|
export interface Decimal128SchemaBuilder<
|
|
28
37
|
Props extends { type: typeof Schema.Types.Decimal128 } = { type: typeof Schema.Types.Decimal128 },
|
|
29
38
|
ExtraOmitFields extends string = never,
|
|
@@ -54,19 +63,41 @@ export interface Decimal128SchemaBuilder<
|
|
|
54
63
|
ExtraOmitFields
|
|
55
64
|
>;
|
|
56
65
|
|
|
66
|
+
max: <
|
|
67
|
+
T extends D | Readonlyable<[D, S]>,
|
|
68
|
+
D extends Decimal128Limit,
|
|
69
|
+
S extends string,
|
|
70
|
+
>(value: T) => ExtendSchemaBuilder<
|
|
71
|
+
Merge<Props, Decimal128ValidationSchema>,
|
|
72
|
+
'max' | ExtraOmitFields
|
|
73
|
+
>;
|
|
74
|
+
|
|
75
|
+
min: <
|
|
76
|
+
T extends D | Readonlyable<[D, S]>,
|
|
77
|
+
D extends Decimal128Limit,
|
|
78
|
+
S extends string,
|
|
79
|
+
>(value: T) => ExtendSchemaBuilder<
|
|
80
|
+
Merge<Props, Decimal128ValidationSchema>,
|
|
81
|
+
'min' | ExtraOmitFields
|
|
82
|
+
>;
|
|
83
|
+
|
|
57
84
|
nonRequired: Props;
|
|
58
85
|
private: ExtendSchemaBuilder<Merge<Props, { private: true }>, ExtraOmitFields>;
|
|
59
86
|
required: Merge<Props, { required: true }>;
|
|
60
87
|
|
|
61
88
|
/**
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
*
|
|
89
|
+
* Configures a setter that rounds decimal values and formats them with a fixed number of decimal places.
|
|
90
|
+
*
|
|
91
|
+
* @remarks
|
|
92
|
+
* Replaces the configured setter on this builder. The setter converts non-nullish values through Decimal.js
|
|
93
|
+
* and returns a decimal string for Mongoose to cast to `Decimal128`. It returns `undefined` for `null` or
|
|
94
|
+
* `undefined`. Decimal.js validates conversion and formatting when the setter processes a value,
|
|
95
|
+
* not when the setter is configured.
|
|
65
96
|
*
|
|
66
|
-
* @param places - The number of decimal places
|
|
67
|
-
* @param rounding - The rounding
|
|
97
|
+
* @param places - The non-negative integer number of decimal places. Defaults to `2`.
|
|
98
|
+
* @param rounding - The Decimal.js rounding mode. Defaults to `Decimal.ROUND_DOWN`, which rounds toward zero.
|
|
68
99
|
*
|
|
69
|
-
* @returns
|
|
100
|
+
* @returns This instance for chaining.
|
|
70
101
|
*/
|
|
71
102
|
setRoundAndToFixedSetter: (places?: number, rounding?: Decimal.Rounding) => ExtendSchemaBuilder<
|
|
72
103
|
Merge<Props, ToStringSetterSchema>,
|
|
@@ -74,19 +105,23 @@ export interface Decimal128SchemaBuilder<
|
|
|
74
105
|
>;
|
|
75
106
|
|
|
76
107
|
/**
|
|
77
|
-
*
|
|
78
|
-
* This method defines how the Decimal128 field should be converted to a string when processing or
|
|
79
|
-
* retrieving the value in the application (e.g., via getters), while still storing the value as Decimal128
|
|
80
|
-
* in the database.
|
|
108
|
+
* Configures a getter that converts decimal values to strings.
|
|
81
109
|
*
|
|
82
|
-
* @
|
|
83
|
-
*
|
|
110
|
+
* @remarks
|
|
111
|
+
* Replaces the configured getter on this builder. The getter returns the value's string representation,
|
|
112
|
+
* or `undefined` for `null` or `undefined`. It changes the read representation, not the stored BSON type.
|
|
113
|
+
*
|
|
114
|
+
* @returns This instance for chaining.
|
|
84
115
|
*/
|
|
85
116
|
setToStringGetter: ExtendSchemaBuilder<Merge<Props, ToStringGetterSchema>, 'setToStringGetter' | ExtraOmitFields>;
|
|
86
117
|
sparse: ExtendSchemaBuilder<Merge<Props, { sparse: true }>, ExtraOmitFields>;
|
|
87
118
|
unique: ExtendSchemaBuilder<Merge<Props, { unique: true }>, ExtraOmitFields>;
|
|
88
119
|
}
|
|
89
120
|
|
|
121
|
+
interface Decimal128ValidationSchema {
|
|
122
|
+
validate: Decimal128LimitValidator[];
|
|
123
|
+
}
|
|
124
|
+
|
|
90
125
|
interface ToStringGetterSchema {
|
|
91
126
|
get: (value?: Types.Decimal128) => string | undefined;
|
|
92
127
|
}
|
|
@@ -95,8 +130,40 @@ interface ToStringSetterSchema {
|
|
|
95
130
|
set: (value?: { toString: () => string }) => string | undefined;
|
|
96
131
|
}
|
|
97
132
|
|
|
133
|
+
// Constants/Variables
|
|
134
|
+
const defaultMinValidateMessage = 'Path `{PATH}` ({VALUE}) is less than minimum allowed value ({MIN}).';
|
|
135
|
+
const defaultMaxValidateMessage = 'Path `{PATH}` ({VALUE}) is more than maximum allowed value ({MAX}).';
|
|
136
|
+
|
|
137
|
+
// Functions
|
|
98
138
|
const baseBuilderFactory = createBaseSchemaBuilderFactory(Schema.Types.Decimal128);
|
|
99
139
|
|
|
140
|
+
function createLimitValidator(
|
|
141
|
+
type: 'max' | 'min',
|
|
142
|
+
value: Decimal128Limit | Readonlyable<[Decimal128Limit, string]>,
|
|
143
|
+
): Decimal128LimitValidator {
|
|
144
|
+
const [limit, message] = Array.isArray(value)
|
|
145
|
+
? value as Readonly<[Decimal128Limit, string]>
|
|
146
|
+
: [
|
|
147
|
+
value,
|
|
148
|
+
undefined,
|
|
149
|
+
];
|
|
150
|
+
|
|
151
|
+
const decimalLimit = new Decimal(limit.toString());
|
|
152
|
+
if (!decimalLimit.isFinite()) throw new RangeError(`Decimal128 ${type} limit must be finite`);
|
|
153
|
+
|
|
154
|
+
return {
|
|
155
|
+
message: message ?? (type === 'min' ? defaultMinValidateMessage : defaultMaxValidateMessage),
|
|
156
|
+
[type]: decimalLimit.toString(),
|
|
157
|
+
type,
|
|
158
|
+
validator: (value) => {
|
|
159
|
+
if (value === null || value === undefined) return true;
|
|
160
|
+
|
|
161
|
+
const decimalValue = new Decimal(value.toString());
|
|
162
|
+
return type === 'min' ? decimalValue.gte(decimalLimit) : decimalValue.lte(decimalLimit);
|
|
163
|
+
},
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
|
|
100
167
|
export function decimal128SchemaBuilder() {
|
|
101
168
|
const schema: Record<string, any> = {};
|
|
102
169
|
const baseBuilder = baseBuilderFactory(schema);
|
|
@@ -104,6 +171,19 @@ export function decimal128SchemaBuilder() {
|
|
|
104
171
|
baseBuilder,
|
|
105
172
|
{
|
|
106
173
|
get(target, key, receiver) {
|
|
174
|
+
if (key === 'max' || key === 'min') {
|
|
175
|
+
return (value: Decimal128Limit | Readonlyable<[Decimal128Limit, string]>) => {
|
|
176
|
+
const validator = createLimitValidator(key, value);
|
|
177
|
+
const validators = Array.isArray(schema.validate) ? schema.validate : [];
|
|
178
|
+
schema.validate = [
|
|
179
|
+
...validators.filter(({ type }) => type !== key),
|
|
180
|
+
validator,
|
|
181
|
+
];
|
|
182
|
+
|
|
183
|
+
return receiver;
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
|
|
107
187
|
if (key === 'setRoundAndToFixedSetter') {
|
|
108
188
|
return (places: number = 2, rounding: Decimal.Rounding = Decimal.ROUND_DOWN) => {
|
|
109
189
|
schema.set = (value?: { toString: () => string }) => {
|