@kikiutils/mongoose 7.1.0 → 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 +4 -0
- package/dist/_internals.d.ts.map +1 -1
- package/dist/_internals.js +2 -1
- package/dist/_internals.js.map +1 -1
- 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.map +1 -1
- package/dist/options.js +2 -1
- package/dist/options.js.map +1 -1
- package/dist/plugins/normalize.d.ts +21 -14
- package/dist/plugins/normalize.d.ts.map +1 -1
- package/dist/plugins/normalize.js +15 -10
- package/dist/plugins/normalize.js.map +1 -1
- package/dist/schema-builders/base.d.ts +1 -1
- 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.map +1 -1
- package/dist/schema-builders/boolean.js.map +1 -1
- package/dist/schema-builders/decimal128.d.ts +16 -12
- package/dist/schema-builders/decimal128.d.ts.map +1 -1
- 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/string.d.ts +33 -23
- package/dist/schema-builders/string.d.ts.map +1 -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/aggregate-paginate.d.ts +24 -8
- package/dist/types/aggregate-paginate.d.ts.map +1 -1
- package/dist/types/index.d.ts +1 -4
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/options.d.ts +2 -1
- package/dist/types/options.d.ts.map +1 -1
- package/dist/types/paginate.d.ts +6 -2
- package/dist/types/paginate.d.ts.map +1 -1
- package/package.json +16 -12
- 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 +6 -1
- package/src/schema-builders/bigint.ts +134 -0
- package/src/schema-builders/boolean.ts +1 -4
- package/src/schema-builders/decimal128.ts +16 -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 +33 -23
- 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 -5
- 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 -5
- 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/README.md
CHANGED
|
@@ -7,19 +7,19 @@
|
|
|
7
7
|
[![codecov][codecov-src]][codecov-href]
|
|
8
8
|
[![License][license-src]][license-href]
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
Provides Mongoose schema builders and utilities for connections, pagination, JSON normalization, and update checks.
|
|
11
11
|
|
|
12
12
|
- [✨ Release Notes](./CHANGELOG.md)
|
|
13
13
|
|
|
14
14
|
## Features
|
|
15
15
|
|
|
16
|
-
- ✨
|
|
17
|
-
- 🔌
|
|
18
|
-
- 🛠
|
|
19
|
-
- 🧮
|
|
20
|
-
- 🔄
|
|
21
|
-
-
|
|
22
|
-
- 🔧
|
|
16
|
+
- ✨ Supports custom connections and reuses an automatically created default connection with configurable creation options.
|
|
17
|
+
- 🔌 Registers `mongoose-aggregate-paginate-v2`, `mongoose-paginate-v2`, and update-result assertion plugins, with optional recursive JSON normalization.
|
|
18
|
+
- 🛠 Provides chainable schema builders for `bigint`, `boolean`, `date`, `decimal128`, `double`, `int32`, `mixed`, `number`, `objectId`, `ref`, `string`, and `uuid`, with field options tailored to each schema type.
|
|
19
|
+
- 🧮 Provides `Decimal128` rounding, fixed-decimal formatting, string getters, and inclusive range validation.
|
|
20
|
+
- 🔄 Normalizes JSON output by default: adds `id`, removes `_id`, `__v`, and private fields, and serializes `Decimal128` values as strings.
|
|
21
|
+
- 🔗 Defines ObjectId reference fields using model names, model constructors, or callbacks.
|
|
22
|
+
- 🔧 Checks update acknowledgment and a minimum modified-document count through function, model, and document APIs.
|
|
23
23
|
|
|
24
24
|
## Requirements
|
|
25
25
|
|
package/dist/_internals.d.ts
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import { CustomMongooseOptions } from "./types/options.js";
|
|
2
|
+
import { Connection } from "mongoose";
|
|
2
3
|
//#region src/_internals.d.ts
|
|
3
4
|
export declare const customMongooseOptions: CustomMongooseOptions;
|
|
5
|
+
export declare const mongooseConnectionState: {
|
|
6
|
+
defaultConnection?: Connection;
|
|
7
|
+
};
|
|
4
8
|
//#endregion
|
|
5
9
|
//# sourceMappingURL=_internals.d.ts.map
|
package/dist/_internals.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"_internals.d.ts","names":[],"sources":["../src/_internals.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"_internals.d.ts","names":[],"sources":["../src/_internals.ts"],"mappings":";;;qBAIa,uBAAuB;qBACvB;EAA2B,oBAAoB"}
|
package/dist/_internals.js
CHANGED
package/dist/_internals.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"_internals.js","names":[],"sources":["../src/_internals.ts"],"sourcesContent":["import type { CustomMongooseOptions } from './types/options';\n\nexport const customMongooseOptions: CustomMongooseOptions = {};\n"],"mappings":";
|
|
1
|
+
{"version":3,"file":"_internals.js","names":[],"sources":["../src/_internals.ts"],"sourcesContent":["import type { Connection } from 'mongoose';\n\nimport type { CustomMongooseOptions } from './types/options';\n\nexport const customMongooseOptions: CustomMongooseOptions = {};\nexport const mongooseConnectionState: { defaultConnection?: Connection } = {};\n"],"mappings":";AAIA,MAAa,wBAA+C,CAAC;AAC7D,MAAa,0BAA8D,CAAC"}
|
package/dist/builders.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"builders.d.ts","names":["MongooseModel","Model"],"sources":["../src/builders.ts"],"mappings":";;;;;;
|
|
1
|
+
{"version":3,"file":"builders.d.ts","names":["MongooseModel","Model"],"sources":["../src/builders.ts"],"mappings":";;;;;;YAeY,kCAAkC;wBAE9B,mBACZ,SACA,cAAc,0BAA0B,SAAS,6BAA6B,eAC9E,sCACA,uBAEA,oBACA,cACA,QAAQ,OAAO,SAAS,OAAO,6BAA6B,eAC5D,UAAU,4BACX;wBAca,gCACZ,SACA,gBAAcA,MAAc,SAAS,cAAc,8BACnD,sCACA,uBAEA,QAAQ,OAAO,SAASC,SAAO,6BAA6B,eAC5D,qBACA,UAAU"}
|
package/dist/builders.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { customMongooseOptions } from "./_internals.js";
|
|
2
|
-
import {
|
|
2
|
+
import { getDefaultMongooseConnection } from "./connection.js";
|
|
3
3
|
import { mongooseAssertionsPlugin } from "./plugins/assertions.js";
|
|
4
4
|
import { mongooseNormalizePlugin } from "./plugins/normalize.js";
|
|
5
|
-
import mongoose from "mongoose";
|
|
6
5
|
import mongooseAggregatePaginate from "mongoose-aggregate-paginate-v2";
|
|
7
6
|
import mongoosePaginate from "mongoose-paginate-v2";
|
|
8
7
|
//#region src/builders.ts
|
|
@@ -13,7 +12,7 @@ function buildMongooseModel(collection, name, schema, options) {
|
|
|
13
12
|
schema.plugin(mongoosePaginate);
|
|
14
13
|
schema.set("timestamps", options?.timestamps ?? schema.get("timestamps") ?? true);
|
|
15
14
|
customMongooseOptions.beforeModelBuild?.(schema);
|
|
16
|
-
return (options?.connection
|
|
15
|
+
return (options?.connection ?? getDefaultMongooseConnection()).model(name, schema, collection);
|
|
17
16
|
}
|
|
18
17
|
function registerMongooseNormalizePlugin(schema, recursive = true, options) {
|
|
19
18
|
schema.plugin(mongooseNormalizePlugin, options);
|
package/dist/builders.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"builders.js","names":[],"sources":["../src/builders.ts"],"sourcesContent":["import
|
|
1
|
+
{"version":3,"file":"builders.js","names":[],"sources":["../src/builders.ts"],"sourcesContent":["import type {\n Model as MongooseModel,\n Schema,\n} from 'mongoose';\nimport mongooseAggregatePaginate from 'mongoose-aggregate-paginate-v2';\nimport mongoosePaginate from 'mongoose-paginate-v2';\n\nimport { customMongooseOptions } from './_internals';\nimport { getDefaultMongooseConnection } from './connection';\nimport { mongooseAssertionsPlugin } from './plugins/assertions';\nimport { mongooseNormalizePlugin } from './plugins/normalize';\nimport type { MongooseNormalizePluginOptions } from './plugins/normalize';\nimport type { BaseMongoosePaginateModel } from './types';\nimport type { BuildMongooseModelOptions } from './types/options';\n\nexport type DoNotRemoveOrUseThisType = typeof mongooseAssertionsPlugin;\n\nexport function buildMongooseModel<\n DocType,\n Model extends BaseMongoosePaginateModel<DocType, InstanceMethodsAndOverrides, QueryHelpers>,\n InstanceMethodsAndOverrides = object,\n QueryHelpers = object,\n>(\n collection: string,\n name: string,\n schema: Schema<DocType, Model, InstanceMethodsAndOverrides, QueryHelpers>,\n options?: BuildMongooseModelOptions,\n): Model {\n if (options?.enableNormalizePlugin !== false) {\n registerMongooseNormalizePlugin(schema, options?.normalizePluginRecursive, options?.normalizePluginOptions);\n }\n\n schema.plugin(mongooseAggregatePaginate);\n schema.plugin(mongooseAssertionsPlugin);\n schema.plugin(mongoosePaginate);\n schema.set('timestamps', options?.timestamps ?? schema.get('timestamps') ?? true);\n customMongooseOptions.beforeModelBuild?.(schema);\n const connection = options?.connection ?? getDefaultMongooseConnection();\n return connection.model<DocType, Model, QueryHelpers>(name, schema, collection);\n}\n\nexport function registerMongooseNormalizePlugin<\n DocType,\n Model extends MongooseModel<DocType, QueryHelpers, InstanceMethodsAndOverrides>,\n InstanceMethodsAndOverrides = object,\n QueryHelpers = object,\n>(\n schema: Schema<DocType, Model, InstanceMethodsAndOverrides, QueryHelpers>,\n recursive: boolean = true,\n options?: MongooseNormalizePluginOptions,\n) {\n schema.plugin(mongooseNormalizePlugin, options);\n if (recursive) {\n schema.childSchemas.forEach(({ schema }) => registerMongooseNormalizePlugin(schema, recursive, options));\n }\n}\n"],"mappings":";;;;;;;AAiBA,SAAgB,mBAMZ,YACA,MACA,QACA,SACK;CACL,IAAI,SAAS,0BAA0B,OACnC,gCAAgC,QAAQ,SAAS,0BAA0B,SAAS,sBAAsB;CAG9G,OAAO,OAAO,yBAAyB;CACvC,OAAO,OAAO,wBAAwB;CACtC,OAAO,OAAO,gBAAgB;CAC9B,OAAO,IAAI,cAAc,SAAS,cAAc,OAAO,IAAI,YAAY,KAAK,IAAI;CAChF,sBAAsB,mBAAmB,MAAM;CAE/C,QADmB,SAAS,cAAc,6BAA6B,EAAA,CACrD,MAAoC,MAAM,QAAQ,UAAU;AAClF;AAEA,SAAgB,gCAMZ,QACA,YAAqB,MACrB,SACF;CACE,OAAO,OAAO,yBAAyB,OAAO;CAC9C,IAAI,WACA,OAAO,aAAa,SAAS,EAAE,aAAa,gCAAgC,QAAQ,WAAW,OAAO,CAAC;AAE/G"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"connection.d.ts","names":[],"sources":["../src/connection.ts"],"mappings":";;wBAQgB,gCAAgC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { customMongooseOptions, mongooseConnectionState } from "./_internals.js";
|
|
2
|
+
import mongoose from "mongoose";
|
|
3
|
+
//#region src/connection.ts
|
|
4
|
+
function getDefaultMongooseConnection() {
|
|
5
|
+
return mongooseConnectionState.defaultConnection ||= mongoose.createConnection(process.env.MONGODB_URI || "mongodb://127.0.0.1:27017", customMongooseOptions.defaultConnectionOptions);
|
|
6
|
+
}
|
|
7
|
+
//#endregion
|
|
8
|
+
export { getDefaultMongooseConnection };
|
|
9
|
+
|
|
10
|
+
//# sourceMappingURL=connection.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"connection.js","names":[],"sources":["../src/connection.ts"],"sourcesContent":["import mongoose from 'mongoose';\nimport type { Connection } from 'mongoose';\n\nimport {\n customMongooseOptions,\n mongooseConnectionState,\n} from './_internals';\n\nexport function getDefaultMongooseConnection(): Connection {\n return mongooseConnectionState.defaultConnection ||= mongoose.createConnection(\n process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017',\n customMongooseOptions.defaultConnectionOptions,\n );\n}\n"],"mappings":";;;AAQA,SAAgB,+BAA2C;CACvD,OAAO,wBAAwB,sBAAsB,SAAS,iBAC1D,QAAQ,IAAI,eAAe,6BAC3B,sBAAsB,wBAC1B;AACJ"}
|
package/dist/options.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"options.d.ts","names":[],"sources":["../src/options.ts"],"mappings":";;
|
|
1
|
+
{"version":3,"file":"options.d.ts","names":[],"sources":["../src/options.ts"],"mappings":";;wBAMgB,yBAAyB,gBAAgB,uBACrD,KAAK,GACL,OAAO,sBAAsB"}
|
package/dist/options.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { customMongooseOptions } from "./_internals.js";
|
|
1
|
+
import { customMongooseOptions, mongooseConnectionState } from "./_internals.js";
|
|
2
2
|
//#region src/options.ts
|
|
3
3
|
function setCustomMongooseOptions(key, value) {
|
|
4
|
+
if (key === "defaultConnectionOptions" && mongooseConnectionState.defaultConnection) throw new Error("Default connection options must be set before the default connection is created.");
|
|
4
5
|
customMongooseOptions[key] = value;
|
|
5
6
|
}
|
|
6
7
|
//#endregion
|
package/dist/options.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"options.js","names":[],"sources":["../src/options.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"file":"options.js","names":[],"sources":["../src/options.ts"],"sourcesContent":["import {\n customMongooseOptions,\n mongooseConnectionState,\n} from './_internals';\nimport type { CustomMongooseOptions } from './types/options';\n\nexport function setCustomMongooseOptions<K extends keyof CustomMongooseOptions>(\n key: K,\n value: CustomMongooseOptions[K],\n) {\n if (key === 'defaultConnectionOptions' && mongooseConnectionState.defaultConnection) {\n throw new Error('Default connection options must be set before the default connection is created.');\n }\n\n customMongooseOptions[key] = value;\n}\n"],"mappings":";;AAMA,SAAgB,yBACZ,KACA,OACF;CACE,IAAI,QAAQ,8BAA8B,wBAAwB,mBAC9D,MAAM,IAAI,MAAM,kFAAkF;CAGtG,sBAAsB,OAAO;AACjC"}
|
|
@@ -2,32 +2,39 @@ import { Schema } from "mongoose";
|
|
|
2
2
|
//#region src/plugins/normalize.d.ts
|
|
3
3
|
export interface MongooseNormalizePluginOptions {
|
|
4
4
|
/**
|
|
5
|
-
* Whether to
|
|
6
|
-
* If true, the `_id` field will be replaced with `id`.
|
|
5
|
+
* Whether to move the serialized `_id` value to `id`.
|
|
7
6
|
*
|
|
8
|
-
* @
|
|
7
|
+
* @remarks
|
|
8
|
+
* If `false`, preserves the value under `_id` instead. Only values other than `undefined` are copied.
|
|
9
|
+
*
|
|
10
|
+
* @defaultValue `true`.
|
|
9
11
|
*/
|
|
10
12
|
convertIdField?: boolean;
|
|
11
13
|
/**
|
|
12
|
-
* Whether to convert
|
|
14
|
+
* Whether to convert a serialized `ObjectId` identifier to its hexadecimal string.
|
|
15
|
+
*
|
|
16
|
+
* @remarks
|
|
17
|
+
* Applies before choosing the `id` or `_id` output key. Identifiers of other types are unchanged.
|
|
13
18
|
*
|
|
14
|
-
* @
|
|
19
|
+
* @defaultValue `true`.
|
|
15
20
|
*/
|
|
16
21
|
toHexIdIfObjectId?: boolean;
|
|
17
22
|
}
|
|
18
23
|
/**
|
|
19
|
-
*
|
|
24
|
+
* Registers JSON normalization on a Mongoose schema.
|
|
20
25
|
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
* - Convert `Decimal128` fields to strings
|
|
26
|
-
* - Remove the `__v` version key
|
|
26
|
+
* @remarks
|
|
27
|
+
* Mutates the schema's `toJSON` configuration while preserving its other options. The transform removes `__v`,
|
|
28
|
+
* normalizes `_id` according to the plugin options, removes schema paths marked `private`, and converts
|
|
29
|
+
* truthy serialized values at registered `Decimal128` paths to strings.
|
|
27
30
|
*
|
|
28
|
-
*
|
|
31
|
+
* If an existing transform is a function, invokes it after normalization. If it returns `undefined`,
|
|
32
|
+
* preserves the normalized output and any mutations made by that transform; otherwise, uses its return value.
|
|
33
|
+
* That transform can change the normalized output, and its errors propagate during serialization.
|
|
34
|
+
* This plugin does not configure `toObject` or recursively register itself on child schemas.
|
|
29
35
|
*
|
|
30
|
-
* @param schema - The
|
|
36
|
+
* @param schema - The schema whose JSON serialization configuration is modified.
|
|
37
|
+
* @param pluginOptions - The identifier normalization options; omitted options use their documented defaults.
|
|
31
38
|
*/
|
|
32
39
|
export declare function mongooseNormalizePlugin<S extends Schema>(schema: S, pluginOptions?: MongooseNormalizePluginOptions): void;
|
|
33
40
|
//#endregion
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"normalize.d.ts","names":[],"sources":["../../src/plugins/normalize.ts"],"mappings":";;iBAQiB
|
|
1
|
+
{"version":3,"file":"normalize.d.ts","names":[],"sources":["../../src/plugins/normalize.ts"],"mappings":";;iBAQiB;;;;;;;;;EASb;;;;;;;;;EAUA;;;;;;;;;;;;;;;;;;wBAmBY,wBAAwB,UAAU,QAC9C,QAAQ,GACR,gBAAgB"}
|
|
@@ -2,18 +2,20 @@ import { Types } from "mongoose";
|
|
|
2
2
|
import { get, set, unset } from "es-toolkit/compat";
|
|
3
3
|
//#region src/plugins/normalize.ts
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
5
|
+
* Registers JSON normalization on a Mongoose schema.
|
|
6
6
|
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
* - Convert `Decimal128` fields to strings
|
|
12
|
-
* - Remove the `__v` version key
|
|
7
|
+
* @remarks
|
|
8
|
+
* Mutates the schema's `toJSON` configuration while preserving its other options. The transform removes `__v`,
|
|
9
|
+
* normalizes `_id` according to the plugin options, removes schema paths marked `private`, and converts
|
|
10
|
+
* truthy serialized values at registered `Decimal128` paths to strings.
|
|
13
11
|
*
|
|
14
|
-
*
|
|
12
|
+
* If an existing transform is a function, invokes it after normalization. If it returns `undefined`,
|
|
13
|
+
* preserves the normalized output and any mutations made by that transform; otherwise, uses its return value.
|
|
14
|
+
* That transform can change the normalized output, and its errors propagate during serialization.
|
|
15
|
+
* This plugin does not configure `toObject` or recursively register itself on child schemas.
|
|
15
16
|
*
|
|
16
|
-
* @param schema - The
|
|
17
|
+
* @param schema - The schema whose JSON serialization configuration is modified.
|
|
18
|
+
* @param pluginOptions - The identifier normalization options; omitted options use their documented defaults.
|
|
17
19
|
*/
|
|
18
20
|
function mongooseNormalizePlugin(schema, pluginOptions) {
|
|
19
21
|
const decimalPaths = [];
|
|
@@ -39,7 +41,10 @@ function mongooseNormalizePlugin(schema, pluginOptions) {
|
|
|
39
41
|
const value = get(copiedRet, path);
|
|
40
42
|
if (value) set(copiedRet, path, value.toString());
|
|
41
43
|
}
|
|
42
|
-
if (toJsonTransform && typeof toJsonTransform !== "boolean")
|
|
44
|
+
if (toJsonTransform && typeof toJsonTransform !== "boolean") {
|
|
45
|
+
const transformed = toJsonTransform(doc, copiedRet, options);
|
|
46
|
+
return transformed === void 0 ? copiedRet : transformed;
|
|
47
|
+
}
|
|
43
48
|
return copiedRet;
|
|
44
49
|
}
|
|
45
50
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"normalize.js","names":["getProp"],"sources":["../../src/plugins/normalize.ts"],"sourcesContent":["import {\n get as getProp,\n set as setProp,\n unset as unsetProp,\n} from 'es-toolkit/compat';\nimport { Types } from 'mongoose';\nimport type { Schema } from 'mongoose';\n\nexport interface MongooseNormalizePluginOptions {\n /**\n * Whether to
|
|
1
|
+
{"version":3,"file":"normalize.js","names":["getProp"],"sources":["../../src/plugins/normalize.ts"],"sourcesContent":["import {\n get as getProp,\n set as setProp,\n unset as unsetProp,\n} from 'es-toolkit/compat';\nimport { Types } from 'mongoose';\nimport type { Schema } from 'mongoose';\n\nexport interface MongooseNormalizePluginOptions {\n /**\n * Whether to move the serialized `_id` value to `id`.\n *\n * @remarks\n * If `false`, preserves the value under `_id` instead. Only values other than `undefined` are copied.\n *\n * @defaultValue `true`.\n */\n convertIdField?: boolean;\n\n /**\n * Whether to convert a serialized `ObjectId` identifier to its hexadecimal string.\n *\n * @remarks\n * Applies before choosing the `id` or `_id` output key. Identifiers of other types are unchanged.\n *\n * @defaultValue `true`.\n */\n toHexIdIfObjectId?: boolean;\n}\n\n/**\n * Registers JSON normalization on a Mongoose schema.\n *\n * @remarks\n * Mutates the schema's `toJSON` configuration while preserving its other options. The transform removes `__v`,\n * normalizes `_id` according to the plugin options, removes schema paths marked `private`, and converts\n * truthy serialized values at registered `Decimal128` paths to strings.\n *\n * If an existing transform is a function, invokes it after normalization. If it returns `undefined`,\n * preserves the normalized output and any mutations made by that transform; otherwise, uses its return value.\n * That transform can change the normalized output, and its errors propagate during serialization.\n * This plugin does not configure `toObject` or recursively register itself on child schemas.\n *\n * @param schema - The schema whose JSON serialization configuration is modified.\n * @param pluginOptions - The identifier normalization options; omitted options use their documented defaults.\n */\nexport function mongooseNormalizePlugin<S extends Schema>(\n schema: S,\n pluginOptions?: MongooseNormalizePluginOptions,\n) {\n // Collect special paths (Decimal128 & private)\n const decimalPaths: string[] = [];\n const privatePaths: string[] = [];\n for (const [path, schemaType] of Object.entries(schema.paths)) {\n if (schemaType?.options?.private) privatePaths.push(path);\n else if (schemaType?.instance === 'Decimal128') decimalPaths.push(path);\n }\n\n // Get original toJSON configuration\n const toJson = schema.get('toJSON');\n const toJsonTransform = toJson?.transform;\n\n // Override toJSON with custom transform\n schema.set(\n 'toJSON',\n {\n ...toJson,\n transform(doc, ret, options) {\n // Copy object and remove __v\n const copiedRet = { ...ret };\n // @ts-expect-error Ignore this error\n delete copiedRet.__v;\n\n // Normalize _id (convert to hex and/or move to id)\n let _id = copiedRet._id;\n delete copiedRet._id;\n if (pluginOptions?.toHexIdIfObjectId !== false && _id instanceof Types.ObjectId) {\n _id = _id.toHexString();\n }\n\n if (_id !== undefined) {\n if (pluginOptions?.convertIdField !== false) copiedRet.id = _id;\n else copiedRet._id = _id;\n }\n\n // Remove private fields\n for (const path of privatePaths) unsetProp(copiedRet, path);\n\n // Convert Decimal128 fields to string\n for (const path of decimalPaths) {\n const value = getProp(copiedRet, path) as Types.Decimal128 | undefined;\n if (value) setProp(copiedRet, path, value.toString());\n }\n\n // Run original toJSON transform\n if (toJsonTransform && typeof toJsonTransform !== 'boolean') {\n const transformed = toJsonTransform(doc as any, copiedRet as any, options as any);\n return transformed === undefined ? copiedRet : transformed;\n }\n\n // Return normalized object\n return copiedRet;\n },\n },\n );\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AA8CA,SAAgB,wBACZ,QACA,eACF;CAEE,MAAM,eAAyB,CAAC;CAChC,MAAM,eAAyB,CAAC;CAChC,KAAK,MAAM,CAAC,MAAM,eAAe,OAAO,QAAQ,OAAO,KAAK,GACxD,IAAI,YAAY,SAAS,SAAS,aAAa,KAAK,IAAI;MACnD,IAAI,YAAY,aAAa,cAAc,aAAa,KAAK,IAAI;CAI1E,MAAM,SAAS,OAAO,IAAI,QAAQ;CAClC,MAAM,kBAAkB,QAAQ;CAGhC,OAAO,IACH,UACA;EACI,GAAG;EACH,UAAU,KAAK,KAAK,SAAS;GAEzB,MAAM,YAAY,EAAE,GAAG,IAAI;GAE3B,OAAO,UAAU;GAGjB,IAAI,MAAM,UAAU;GACpB,OAAO,UAAU;GACjB,IAAI,eAAe,sBAAsB,SAAS,eAAe,MAAM,UACnE,MAAM,IAAI,YAAY;GAG1B,IAAI,QAAQ,KAAA,GAAW;IACnB,IAAI,eAAe,mBAAmB,OAAO,UAAU,KAAK;SACvD,UAAU,MAAM;GACzB;GAGA,KAAK,MAAM,QAAQ,cAAc,MAAU,WAAW,IAAI;GAG1D,KAAK,MAAM,QAAQ,cAAc;IAC7B,MAAM,QAAQA,IAAQ,WAAW,IAAI;IACrC,IAAI,OAAO,IAAQ,WAAW,MAAM,MAAM,SAAS,CAAC;GACxD;GAGA,IAAI,mBAAmB,OAAO,oBAAoB,WAAW;IACzD,MAAM,cAAc,gBAAgB,KAAY,WAAkB,OAAc;IAChF,OAAO,gBAAgB,KAAA,IAAY,YAAY;GACnD;GAGA,OAAO;EACX;CACJ,CACJ;AACJ"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Schema } from "mongoose";
|
|
2
2
|
//#region src/schema-builders/base.d.ts
|
|
3
|
-
type BaseSchemaType = BooleanConstructor | DateConstructor | NumberConstructor | Schema.Types.ObjectId['constructor'] | StringConstructor;
|
|
3
|
+
type BaseSchemaType = BooleanConstructor | DateConstructor | NumberConstructor | Schema.Types.ObjectId['constructor'] | StringConstructor | typeof Schema.Types.BigInt | typeof Schema.Types.Double | typeof Schema.Types.Int32 | typeof Schema.Types.Mixed | typeof Schema.Types.UUID;
|
|
4
4
|
export declare function createBaseSchemaBuilderFactory<Builder = Readonly<Record<string, any>>>(type: BaseSchemaType): (schema?: Record<string, any>) => Builder;
|
|
5
5
|
//#endregion
|
|
6
6
|
//# sourceMappingURL=base.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base.d.ts","names":[],"sources":["../../src/schema-builders/base.ts"],"mappings":";;KAEK,iBACD,qBACA,kBACA,oBACA,OAAO,MAAM,0BACb;
|
|
1
|
+
{"version":3,"file":"base.d.ts","names":[],"sources":["../../src/schema-builders/base.ts"],"mappings":";;KAEK,iBACD,qBACA,kBACA,oBACA,OAAO,MAAM,0BACb,2BACO,OAAO,MAAM,gBACb,OAAO,MAAM,gBACb,OAAO,MAAM,eACb,OAAO,MAAM,eACb,OAAO,MAAM;wBAcR,+BAA+B,UAAU,SAAS,sBAAsB,MAAM,kBAClF,SAAQ,wBA2BP"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base.js","names":[],"sources":["../../src/schema-builders/base.ts"],"sourcesContent":["import type { Schema } from 'mongoose';\n\ntype BaseSchemaType =\n | BooleanConstructor\n | DateConstructor\n | NumberConstructor\n | Schema.Types.ObjectId['constructor']\n | StringConstructor;\n\n// Constants/Variables\nconst isFunctionKeys = new Set([\n 'default',\n 'enum',\n 'index',\n 'max',\n 'maxlength',\n 'min',\n 'minlength',\n]);\n\n// Functions\nexport function createBaseSchemaBuilderFactory<Builder = Readonly<Record<string, any>>>(type: BaseSchemaType) {\n return (schema: Record<string, any> = {}) => {\n schema.type = type;\n return new Proxy(\n Object.freeze({}),\n {\n get(_, key, receiver) {\n if (typeof key === 'symbol') throw new Error('Cannot use symbol as a schema attribute');\n if (key in schema) throw new Error(`Duplicate schema attribute: ${key}`);\n if (isFunctionKeys.has(key)) {\n return (value: any) => {\n schema[key] = value;\n return receiver;\n };\n }\n\n if (key === 'nonRequired') return { ...schema };\n if (key === 'required') {\n return {\n ...schema,\n required: true,\n };\n }\n\n schema[key] = true;\n return receiver;\n },\n },\n ) as Builder;\n };\n}\n"],"mappings":";
|
|
1
|
+
{"version":3,"file":"base.js","names":[],"sources":["../../src/schema-builders/base.ts"],"sourcesContent":["import type { Schema } from 'mongoose';\n\ntype BaseSchemaType =\n | BooleanConstructor\n | DateConstructor\n | NumberConstructor\n | Schema.Types.ObjectId['constructor']\n | StringConstructor\n | typeof Schema.Types.BigInt\n | typeof Schema.Types.Double\n | typeof Schema.Types.Int32\n | typeof Schema.Types.Mixed\n | typeof Schema.Types.UUID;\n\n// Constants/Variables\nconst isFunctionKeys = new Set([\n 'default',\n 'enum',\n 'index',\n 'max',\n 'maxlength',\n 'min',\n 'minlength',\n]);\n\n// Functions\nexport function createBaseSchemaBuilderFactory<Builder = Readonly<Record<string, any>>>(type: BaseSchemaType) {\n return (schema: Record<string, any> = {}) => {\n schema.type = type;\n return new Proxy(\n Object.freeze({}),\n {\n get(_, key, receiver) {\n if (typeof key === 'symbol') throw new Error('Cannot use symbol as a schema attribute');\n if (key in schema) throw new Error(`Duplicate schema attribute: ${key}`);\n if (isFunctionKeys.has(key)) {\n return (value: any) => {\n schema[key] = value;\n return receiver;\n };\n }\n\n if (key === 'nonRequired') return { ...schema };\n if (key === 'required') {\n return {\n ...schema,\n required: true,\n };\n }\n\n schema[key] = true;\n return receiver;\n },\n },\n ) as Builder;\n };\n}\n"],"mappings":";AAeA,MAAM,iCAAiB,IAAI,IAAI;CAC3B;CACA;CACA;CACA;CACA;CACA;CACA;AACJ,CAAC;AAGD,SAAgB,+BAAwE,MAAsB;CAC1G,QAAQ,SAA8B,CAAC,MAAM;EACzC,OAAO,OAAO;EACd,OAAO,IAAI,MACP,OAAO,OAAO,CAAC,CAAC,GAChB,EACI,IAAI,GAAG,KAAK,UAAU;GAClB,IAAI,OAAO,QAAQ,UAAU,MAAM,IAAI,MAAM,yCAAyC;GACtF,IAAI,OAAO,QAAQ,MAAM,IAAI,MAAM,+BAA+B,KAAK;GACvE,IAAI,eAAe,IAAI,GAAG,GACtB,QAAQ,UAAe;IACnB,OAAO,OAAO;IACd,OAAO;GACX;GAGJ,IAAI,QAAQ,eAAe,OAAO,EAAE,GAAG,OAAO;GAC9C,IAAI,QAAQ,YACR,OAAO;IACH,GAAG;IACH,UAAU;GACd;GAGJ,OAAO,OAAO;GACd,OAAO;EACX,EACJ,CACJ;CACJ;AACJ"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { Readonlyable } from "../types/_internals.js";
|
|
2
|
+
import { DefaultType, IndexDirection, IndexOptions, Schema } from "mongoose";
|
|
3
|
+
import { Merge } from "type-fest";
|
|
4
|
+
//#region src/schema-builders/bigint.d.ts
|
|
5
|
+
type BigIntLimit = bigint;
|
|
6
|
+
type ExtendSchemaBuilder<Props extends BaseProps, ExtraOmitFields extends string> = Omit<BigIntSchemaBuilder<Props, ExtraOmitFields>, ExtraOmitFields | keyof Props>;
|
|
7
|
+
interface BaseProps {
|
|
8
|
+
type: typeof Schema.Types.BigInt;
|
|
9
|
+
}
|
|
10
|
+
interface BigIntLimitValidator {
|
|
11
|
+
max?: bigint;
|
|
12
|
+
message: string;
|
|
13
|
+
min?: bigint;
|
|
14
|
+
type: 'max' | 'min';
|
|
15
|
+
validator: (value: bigint | null | undefined) => boolean;
|
|
16
|
+
}
|
|
17
|
+
export interface BigIntSchemaBuilder<Props extends BaseProps = BaseProps, ExtraOmitFields extends string = never> {
|
|
18
|
+
default: <T extends ((this: any, doc: any) => DefaultType<D> | null | undefined) | DefaultType<D> | null | undefined, D extends bigint>(value: T) => ExtendSchemaBuilder<Merge<Props, {
|
|
19
|
+
default: T;
|
|
20
|
+
}>, ExtraOmitFields>;
|
|
21
|
+
immutable: ExtendSchemaBuilder<Merge<Props, {
|
|
22
|
+
immutable: true;
|
|
23
|
+
}>, ExtraOmitFields>;
|
|
24
|
+
index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendSchemaBuilder<Merge<Props, {
|
|
25
|
+
index: T;
|
|
26
|
+
}>, ExtraOmitFields>;
|
|
27
|
+
max: <T extends L | Readonlyable<[L, S]>, L extends BigIntLimit, S extends string>(value: T) => ExtendSchemaBuilder<Merge<Props, BigIntValidationSchema>, 'max' | ExtraOmitFields>;
|
|
28
|
+
min: <T extends L | Readonlyable<[L, S]>, L extends BigIntLimit, S extends string>(value: T) => ExtendSchemaBuilder<Merge<Props, BigIntValidationSchema>, 'min' | ExtraOmitFields>;
|
|
29
|
+
nonRequired: Props;
|
|
30
|
+
private: ExtendSchemaBuilder<Merge<Props, {
|
|
31
|
+
private: true;
|
|
32
|
+
}>, ExtraOmitFields>;
|
|
33
|
+
required: Merge<Props, {
|
|
34
|
+
required: true;
|
|
35
|
+
}>;
|
|
36
|
+
sparse: ExtendSchemaBuilder<Merge<Props, {
|
|
37
|
+
sparse: true;
|
|
38
|
+
}>, ExtraOmitFields>;
|
|
39
|
+
unique: ExtendSchemaBuilder<Merge<Props, {
|
|
40
|
+
unique: true;
|
|
41
|
+
}>, ExtraOmitFields>;
|
|
42
|
+
}
|
|
43
|
+
interface BigIntValidationSchema {
|
|
44
|
+
validate: BigIntLimitValidator[];
|
|
45
|
+
}
|
|
46
|
+
export declare function bigintSchemaBuilder(): BigIntSchemaBuilder;
|
|
47
|
+
//#endregion
|
|
48
|
+
//# sourceMappingURL=bigint.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bigint.d.ts","names":[],"sources":["../../src/schema-builders/bigint.ts"],"mappings":";;;;KAYK;KACA,oBACD,cAAc,WACd,kCACA,KACA,oBAAoB,OAAO,kBAC3B,wBAAwB;UAGlB;EACN,aAAa,OAAO,MAAM;;UAGpB;EACN;EACA;EACA;EACA;EACA,YAAY;;iBAGC,oBAAoB,cAAc,YAAY,WAAW;EACtE,UACI,YAAY,WAAW,aAAa,YAAY,yBAAyB,YAAY,uBACrF,kBACF,OAAO,MAAM,oBACX,MAAM;IAAS,SAAS;MACxB;EAGJ,WAAW,oBAAoB,MAAM;IAAS;MAAoB;EAClE,QAAQ,oBAAoB,iBAAiB,cAAc,OAAO,MAAM,oBACpE,MAAM;IAAS,OAAO;MACtB;EAGJ,MACI,UAAU,IAAI,cAAc,GAAG,KAC/B,UAAU,aACV,kBACF,OAAO,MAAM,oBACX,MAAM,OAAO,iCACL;EAGZ,MACI,UAAU,IAAI,cAAc,GAAG,KAC/B,UAAU,aACV,kBACF,OAAO,MAAM,oBACX,MAAM,OAAO,iCACL;EAGZ,aAAa;EACb,SAAS,oBAAoB,MAAM;IAAS;MAAkB;EAC9D,UAAU,MAAM;IAAS;;EACzB,QAAQ,oBAAoB,MAAM;IAAS;MAAiB;EAC5D,QAAQ,oBAAoB,MAAM;IAAS;MAAiB;;UAGtD;EACN,UAAU;;wBAUE,uBAuBP"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { createBaseSchemaBuilderFactory } from "./base.js";
|
|
2
|
+
import { Schema } from "mongoose";
|
|
3
|
+
//#region src/schema-builders/bigint.ts
|
|
4
|
+
const defaultMinValidateMessage = "Path `{PATH}` ({VALUE}) is less than minimum allowed value ({MIN}).";
|
|
5
|
+
const defaultMaxValidateMessage = "Path `{PATH}` ({VALUE}) is more than maximum allowed value ({MAX}).";
|
|
6
|
+
const baseBuilderFactory = createBaseSchemaBuilderFactory(Schema.Types.BigInt);
|
|
7
|
+
function bigintSchemaBuilder() {
|
|
8
|
+
const schema = {};
|
|
9
|
+
const baseBuilder = baseBuilderFactory(schema);
|
|
10
|
+
return new Proxy(baseBuilder, { get(target, key, receiver) {
|
|
11
|
+
if (key === "max" || key === "min") return (value) => {
|
|
12
|
+
const validator = createLimitValidator(key, value);
|
|
13
|
+
const validators = Array.isArray(schema.validate) ? schema.validate : [];
|
|
14
|
+
schema.validate = [...validators.filter(({ type }) => type !== key), validator];
|
|
15
|
+
return receiver;
|
|
16
|
+
};
|
|
17
|
+
return Reflect.get(target, key, receiver);
|
|
18
|
+
} });
|
|
19
|
+
}
|
|
20
|
+
function createLimitValidator(type, value) {
|
|
21
|
+
const [limit, message] = Array.isArray(value) ? value : [value, void 0];
|
|
22
|
+
if (typeof limit !== "bigint") throw new TypeError(`BigInt ${type} limit must be a bigint`);
|
|
23
|
+
return {
|
|
24
|
+
message: message ?? (type === "min" ? defaultMinValidateMessage : defaultMaxValidateMessage),
|
|
25
|
+
[type]: limit,
|
|
26
|
+
type,
|
|
27
|
+
validator: (value) => {
|
|
28
|
+
if (value === null || value === void 0) return true;
|
|
29
|
+
return type === "min" ? value >= limit : value <= limit;
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
//#endregion
|
|
34
|
+
export { bigintSchemaBuilder };
|
|
35
|
+
|
|
36
|
+
//# sourceMappingURL=bigint.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bigint.js","names":[],"sources":["../../src/schema-builders/bigint.ts"],"sourcesContent":["import { Schema } from 'mongoose';\nimport type {\n DefaultType,\n IndexDirection,\n IndexOptions,\n} from 'mongoose';\nimport type { Merge } from 'type-fest';\n\nimport type { Readonlyable } from '../types/_internals';\n\nimport { createBaseSchemaBuilderFactory } from './base';\n\ntype BigIntLimit = bigint;\ntype ExtendSchemaBuilder<\n Props extends BaseProps,\n ExtraOmitFields extends string,\n> = Omit<\n BigIntSchemaBuilder<Props, ExtraOmitFields>,\n ExtraOmitFields | keyof Props\n>;\n\ninterface BaseProps {\n type: typeof Schema.Types.BigInt;\n}\n\ninterface BigIntLimitValidator {\n max?: bigint;\n message: string;\n min?: bigint;\n type: 'max' | 'min';\n validator: (value: bigint | null | undefined) => boolean;\n}\n\nexport interface BigIntSchemaBuilder<Props extends BaseProps = BaseProps, ExtraOmitFields extends string = never> {\n default: <\n T extends ((this: any, doc: any) => DefaultType<D> | null | undefined) | DefaultType<D> | null | undefined,\n D extends bigint,\n >(value: T) => ExtendSchemaBuilder<\n Merge<Props, { default: T }>,\n ExtraOmitFields\n >;\n\n immutable: ExtendSchemaBuilder<Merge<Props, { immutable: true }>, ExtraOmitFields>;\n index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendSchemaBuilder<\n Merge<Props, { index: T }>,\n ExtraOmitFields\n >;\n\n max: <\n T extends L | Readonlyable<[L, S]>,\n L extends BigIntLimit,\n S extends string,\n >(value: T) => ExtendSchemaBuilder<\n Merge<Props, BigIntValidationSchema>,\n 'max' | ExtraOmitFields\n >;\n\n min: <\n T extends L | Readonlyable<[L, S]>,\n L extends BigIntLimit,\n S extends string,\n >(value: T) => ExtendSchemaBuilder<\n Merge<Props, BigIntValidationSchema>,\n 'min' | ExtraOmitFields\n >;\n\n nonRequired: Props;\n private: ExtendSchemaBuilder<Merge<Props, { private: true }>, ExtraOmitFields>;\n required: Merge<Props, { required: true }>;\n sparse: ExtendSchemaBuilder<Merge<Props, { sparse: true }>, ExtraOmitFields>;\n unique: ExtendSchemaBuilder<Merge<Props, { unique: true }>, ExtraOmitFields>;\n}\n\ninterface BigIntValidationSchema {\n validate: BigIntLimitValidator[];\n}\n\n// Constants/Variables\nconst defaultMinValidateMessage = 'Path `{PATH}` ({VALUE}) is less than minimum allowed value ({MIN}).';\nconst defaultMaxValidateMessage = 'Path `{PATH}` ({VALUE}) is more than maximum allowed value ({MAX}).';\n\n// Functions\nconst baseBuilderFactory = createBaseSchemaBuilderFactory(Schema.Types.BigInt);\n\nexport function bigintSchemaBuilder() {\n const schema: Record<string, any> = {};\n const baseBuilder = baseBuilderFactory(schema);\n return new Proxy(\n baseBuilder,\n {\n get(target, key, receiver) {\n if (key === 'max' || key === 'min') {\n return (value: BigIntLimit | Readonlyable<[BigIntLimit, string]>) => {\n const validator = createLimitValidator(key, value);\n const validators = Array.isArray(schema.validate) ? schema.validate : [];\n schema.validate = [\n ...validators.filter(({ type }) => type !== key),\n validator,\n ];\n\n return receiver;\n };\n }\n\n return Reflect.get(target, key, receiver);\n },\n },\n ) as BigIntSchemaBuilder;\n}\n\nfunction createLimitValidator(\n type: 'max' | 'min',\n value: BigIntLimit | Readonlyable<[BigIntLimit, string]>,\n): BigIntLimitValidator {\n const [limit, message] = Array.isArray(value)\n ? value as Readonly<[BigIntLimit, string]>\n : [\n value as BigIntLimit,\n undefined,\n ];\n\n if (typeof limit !== 'bigint') throw new TypeError(`BigInt ${type} limit must be a bigint`);\n\n return {\n message: message ?? (type === 'min' ? defaultMinValidateMessage : defaultMaxValidateMessage),\n [type]: limit,\n type,\n validator: (value) => {\n if (value === null || value === undefined) return true;\n\n return type === 'min' ? value >= limit : value <= limit;\n },\n };\n}\n"],"mappings":";;;AA8EA,MAAM,4BAA4B;AAClC,MAAM,4BAA4B;AAGlC,MAAM,qBAAqB,+BAA+B,OAAO,MAAM,MAAM;AAE7E,SAAgB,sBAAsB;CAClC,MAAM,SAA8B,CAAC;CACrC,MAAM,cAAc,mBAAmB,MAAM;CAC7C,OAAO,IAAI,MACP,aACA,EACI,IAAI,QAAQ,KAAK,UAAU;EACvB,IAAI,QAAQ,SAAS,QAAQ,OACzB,QAAQ,UAA6D;GACjE,MAAM,YAAY,qBAAqB,KAAK,KAAK;GACjD,MAAM,aAAa,MAAM,QAAQ,OAAO,QAAQ,IAAI,OAAO,WAAW,CAAC;GACvE,OAAO,WAAW,CACd,GAAG,WAAW,QAAQ,EAAE,WAAW,SAAS,GAAG,GAC/C,SACJ;GAEA,OAAO;EACX;EAGJ,OAAO,QAAQ,IAAI,QAAQ,KAAK,QAAQ;CAC5C,EACJ,CACJ;AACJ;AAEA,SAAS,qBACL,MACA,OACoB;CACpB,MAAM,CAAC,OAAO,WAAW,MAAM,QAAQ,KAAK,IACtC,QACA,CACE,OACA,KAAA,CACJ;CAEJ,IAAI,OAAO,UAAU,UAAU,MAAM,IAAI,UAAU,UAAU,KAAK,wBAAwB;CAE1F,OAAO;EACH,SAAS,YAAY,SAAS,QAAQ,4BAA4B;GACjE,OAAO;EACR;EACA,YAAY,UAAU;GAClB,IAAI,UAAU,QAAQ,UAAU,KAAA,GAAW,OAAO;GAElD,OAAO,SAAS,QAAQ,SAAS,QAAQ,SAAS;EACtD;CACJ;AACJ"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"boolean.d.ts","names":[],"sources":["../../src/schema-builders/boolean.ts"],"mappings":";;;KAUK,oBACD,cAAc,WACd,kCACA,KACA,qBAAqB,OAAO,kBAC5B,wBAAwB;UAGlB;EACN,MAAM;;iBAGO,
|
|
1
|
+
{"version":3,"file":"boolean.d.ts","names":[],"sources":["../../src/schema-builders/boolean.ts"],"mappings":";;;KAUK,oBACD,cAAc,WACd,kCACA,KACA,qBAAqB,OAAO,kBAC5B,wBAAwB;UAGlB;EACN,MAAM;;iBAGO,qBAAqB,cAAc,YAAY,WAAW;EACvE,UACI,YAAY,WAAW,aAAa,YAAY,MAAM,YAAY,WAClE,mBACF,OAAO,MAAM,oBACX,MAAM;IAAS,SAAS;MACxB;EAGJ,WAAW,oBAAoB,MAAM;IAAS;MAAoB;EAClE,QAAQ,oBAAoB,iBAAiB,cAAc,OAAO,MAAM,oBACpE,MAAM;IAAS,OAAO;MACtB;EAGJ,aAAa;EACb,SAAS,oBAAoB,MAAM;IAAS;MAAkB;EAC9D,UAAU,MAAM;IAAS;;EACzB,QAAQ,oBAAoB,MAAM;IAAS;MAAiB;EAC5D,QAAQ,oBAAoB,MAAM;IAAS;MAAiB;;qBAGnD,uBAAoB,SAAA,wBAAA,qBAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"boolean.js","names":[],"sources":["../../src/schema-builders/boolean.ts"],"sourcesContent":["import type {\n BooleanSchemaDefinition,\n DefaultType,\n IndexDirection,\n IndexOptions,\n} from 'mongoose';\nimport type { Merge } from 'type-fest';\n\nimport { createBaseSchemaBuilderFactory } from './base';\n\ntype ExtendSchemaBuilder<\n Props extends BaseProps,\n ExtraOmitFields extends string,\n> = Omit<\n BooleanSchemaBuilder<Props, ExtraOmitFields>,\n ExtraOmitFields | keyof Props\n>;\n\ninterface BaseProps {\n type: BooleanSchemaDefinition;\n}\n\nexport interface BooleanSchemaBuilder
|
|
1
|
+
{"version":3,"file":"boolean.js","names":[],"sources":["../../src/schema-builders/boolean.ts"],"sourcesContent":["import type {\n BooleanSchemaDefinition,\n DefaultType,\n IndexDirection,\n IndexOptions,\n} from 'mongoose';\nimport type { Merge } from 'type-fest';\n\nimport { createBaseSchemaBuilderFactory } from './base';\n\ntype ExtendSchemaBuilder<\n Props extends BaseProps,\n ExtraOmitFields extends string,\n> = Omit<\n BooleanSchemaBuilder<Props, ExtraOmitFields>,\n ExtraOmitFields | keyof Props\n>;\n\ninterface BaseProps {\n type: BooleanSchemaDefinition;\n}\n\nexport interface BooleanSchemaBuilder<Props extends BaseProps = BaseProps, ExtraOmitFields extends string = never> {\n default: <\n T extends ((this: any, doc: any) => DefaultType<D>) | DefaultType<D> | null,\n D extends boolean,\n >(value: T) => ExtendSchemaBuilder<\n Merge<Props, { default: T }>,\n ExtraOmitFields\n >;\n\n immutable: ExtendSchemaBuilder<Merge<Props, { immutable: true }>, ExtraOmitFields>;\n index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendSchemaBuilder<\n Merge<Props, { index: T }>,\n ExtraOmitFields\n >;\n\n nonRequired: Props;\n private: ExtendSchemaBuilder<Merge<Props, { private: true }>, ExtraOmitFields>;\n required: Merge<Props, { required: true }>;\n sparse: ExtendSchemaBuilder<Merge<Props, { sparse: true }>, ExtraOmitFields>;\n unique: ExtendSchemaBuilder<Merge<Props, { unique: true }>, ExtraOmitFields>;\n}\n\nexport const booleanSchemaBuilder = createBaseSchemaBuilderFactory<BooleanSchemaBuilder>(Boolean);\n"],"mappings":";;AA4CA,MAAa,uBAAuB,+BAAqD,OAAO"}
|
|
@@ -49,24 +49,28 @@ export interface Decimal128SchemaBuilder<Props extends {
|
|
|
49
49
|
required: true;
|
|
50
50
|
}>;
|
|
51
51
|
/**
|
|
52
|
-
*
|
|
53
|
-
* This method allows specifying the number of decimal places and the rounding strategy to be used when
|
|
54
|
-
* saving the field to the database.
|
|
52
|
+
* Configures a setter that rounds decimal values and formats them with a fixed number of decimal places.
|
|
55
53
|
*
|
|
56
|
-
* @
|
|
57
|
-
*
|
|
54
|
+
* @remarks
|
|
55
|
+
* Replaces the configured setter on this builder. The setter converts non-nullish values through Decimal.js
|
|
56
|
+
* and returns a decimal string for Mongoose to cast to `Decimal128`. It returns `undefined` for `null` or
|
|
57
|
+
* `undefined`. Decimal.js validates conversion and formatting when the setter processes a value,
|
|
58
|
+
* not when the setter is configured.
|
|
58
59
|
*
|
|
59
|
-
* @
|
|
60
|
+
* @param places - The non-negative integer number of decimal places. Defaults to `2`.
|
|
61
|
+
* @param rounding - The Decimal.js rounding mode. Defaults to `Decimal.ROUND_DOWN`, which rounds toward zero.
|
|
62
|
+
*
|
|
63
|
+
* @returns This instance for chaining.
|
|
60
64
|
*/
|
|
61
65
|
setRoundAndToFixedSetter: (places?: number, rounding?: Decimal.Rounding) => ExtendSchemaBuilder<Merge<Props, ToStringSetterSchema>, 'setRoundAndToFixedSetter' | ExtraOmitFields>;
|
|
62
66
|
/**
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
+
* Configures a getter that converts decimal values to strings.
|
|
68
|
+
*
|
|
69
|
+
* @remarks
|
|
70
|
+
* Replaces the configured getter on this builder. The getter returns the value's string representation,
|
|
71
|
+
* or `undefined` for `null` or `undefined`. It changes the read representation, not the stored BSON type.
|
|
67
72
|
*
|
|
68
|
-
* @returns
|
|
69
|
-
* when accessed in the application.
|
|
73
|
+
* @returns This instance for chaining.
|
|
70
74
|
*/
|
|
71
75
|
setToStringGetter: ExtendSchemaBuilder<Merge<Props, ToStringGetterSchema>, 'setToStringGetter' | ExtraOmitFields>;
|
|
72
76
|
sparse: ExtendSchemaBuilder<Merge<Props, {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"decimal128.d.ts","names":[],"sources":["../../src/schema-builders/decimal128.ts"],"mappings":";;;;;KAcK,kBAAkB,QAAQ,QAAQ,MAAM;EAAe;;KACvD,oBACD,cAAc,WACd,kCACA,KACA,wBAAwB,OAAO,kBAC/B,wBAAwB;UAGlB;EACN,aAAa,OAAO,MAAM;;UAGpB;EACN;EACA;EACA;EACA;EACA,YAAY,cAAc,MAAM;;iBAGnB,wBACb;EAAgB,aAAa,OAAO,MAAM;;EAAiB,aAAa,OAAO,MAAM;GACrF;EAEA,UACI,YAAY,WAAW,aAAa,YAAY,MAAM,YAAY,WAClE,UAAU,MAAM,YAClB,OAAO,MAAM,oBACX,MAAM;IAAS,SAAS;MACxB;EAGJ,OACI,UACE,aAAa,MAAM;KAChB,eAAe;;IAChB,UAAU;IAAG,QAAQ,aAAa,MAAM;KAC5C,UAAU,MAAM,YAChB,kBACF,OAAO,MAAM,oBACX,MAAM;IAAS,MAAM;MACrB;EAGJ,WAAW,oBAAoB,MAAM;IAAS;MAAoB;EAClE,QAAQ,oBAAoB,iBAAiB,cAAc,OAAO,MAAM,oBACpE,MAAM;IAAS,OAAO;MACtB;EAGJ,MACI,UAAU,IAAI,cAAc,GAAG,KAC/B,UAAU,iBACV,kBACF,OAAO,MAAM,oBACX,MAAM,OAAO,qCACL;EAGZ,MACI,UAAU,IAAI,cAAc,GAAG,KAC/B,UAAU,iBACV,kBACF,OAAO,MAAM,oBACX,MAAM,OAAO,qCACL;EAGZ,aAAa;EACb,SAAS,oBAAoB,MAAM;IAAS;MAAkB;EAC9D,UAAU,MAAM;IAAS
|
|
1
|
+
{"version":3,"file":"decimal128.d.ts","names":[],"sources":["../../src/schema-builders/decimal128.ts"],"mappings":";;;;;KAcK,kBAAkB,QAAQ,QAAQ,MAAM;EAAe;;KACvD,oBACD,cAAc,WACd,kCACA,KACA,wBAAwB,OAAO,kBAC/B,wBAAwB;UAGlB;EACN,aAAa,OAAO,MAAM;;UAGpB;EACN;EACA;EACA;EACA;EACA,YAAY,cAAc,MAAM;;iBAGnB,wBACb;EAAgB,aAAa,OAAO,MAAM;;EAAiB,aAAa,OAAO,MAAM;GACrF;EAEA,UACI,YAAY,WAAW,aAAa,YAAY,MAAM,YAAY,WAClE,UAAU,MAAM,YAClB,OAAO,MAAM,oBACX,MAAM;IAAS,SAAS;MACxB;EAGJ,OACI,UACE,aAAa,MAAM;KAChB,eAAe;;IAChB,UAAU;IAAG,QAAQ,aAAa,MAAM;KAC5C,UAAU,MAAM,YAChB,kBACF,OAAO,MAAM,oBACX,MAAM;IAAS,MAAM;MACrB;EAGJ,WAAW,oBAAoB,MAAM;IAAS;MAAoB;EAClE,QAAQ,oBAAoB,iBAAiB,cAAc,OAAO,MAAM,oBACpE,MAAM;IAAS,OAAO;MACtB;EAGJ,MACI,UAAU,IAAI,cAAc,GAAG,KAC/B,UAAU,iBACV,kBACF,OAAO,MAAM,oBACX,MAAM,OAAO,qCACL;EAGZ,MACI,UAAU,IAAI,cAAc,GAAG,KAC/B,UAAU,iBACV,kBACF,OAAO,MAAM,oBACX,MAAM,OAAO,qCACL;EAGZ,aAAa;EACb,SAAS,oBAAoB,MAAM;IAAS;MAAkB;EAC9D,UAAU,MAAM;IAAS;;;;;;;;;;;;;;;;EAgBzB,2BAA2B,iBAAiB,WAAW,QAAQ,aAAa,oBACxE,MAAM,OAAO,oDACgB;;;;;;;;;;EAYjC,mBAAmB,oBAAoB,MAAM,OAAO,6CAA6C;EACjG,QAAQ,oBAAoB,MAAM;IAAS;MAAiB;EAC5D,QAAQ,oBAAoB,MAAM;IAAS;MAAiB;;UAGtD;EACN,UAAU;;UAGJ;EACN,MAAM,QAAQ,MAAM;;UAGd;EACN,MAAM;IAAU;;;wBAqCJ,2BAyCP"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"decimal128.js","names":[],"sources":["../../src/schema-builders/decimal128.ts"],"sourcesContent":["import { Decimal } from 'decimal.js';\nimport { Schema } from 'mongoose';\nimport type {\n DefaultType,\n IndexDirection,\n IndexOptions,\n Types,\n} from 'mongoose';\nimport type { Merge } from 'type-fest';\n\nimport type { Readonlyable } from '../types/_internals';\n\nimport { createBaseSchemaBuilderFactory } from './base';\n\ntype Decimal128Limit = Decimal.Value | Types.Decimal128 | { toString: () => string };\ntype ExtendSchemaBuilder<\n Props extends BaseProps,\n ExtraOmitFields extends string,\n> = Omit<\n Decimal128SchemaBuilder<Props, ExtraOmitFields>,\n ExtraOmitFields | keyof Props\n>;\n\ninterface BaseProps {\n type: typeof Schema.Types.Decimal128;\n}\n\ninterface Decimal128LimitValidator {\n max?: string;\n message: string;\n min?: string;\n type: 'max' | 'min';\n validator: (value: null | Types.Decimal128 | undefined) => boolean;\n}\n\nexport interface Decimal128SchemaBuilder<\n Props extends { type: typeof Schema.Types.Decimal128 } = { type: typeof Schema.Types.Decimal128 },\n ExtraOmitFields extends string = never,\n> {\n default: <\n T extends ((this: any, doc: any) => DefaultType<D>) | DefaultType<D> | null,\n D extends Types.Decimal128,\n >(value: T) => ExtendSchemaBuilder<\n Merge<Props, { default: T }>,\n ExtraOmitFields\n >;\n\n enum: <\n T extends\n | Readonlyable<Array<D | null>>\n | { [path: string]: D | null }\n | { message?: M; values: Readonlyable<Array<D | null>> },\n D extends Types.Decimal128,\n M extends string,\n >(value: T) => ExtendSchemaBuilder<\n Merge<Props, { enum: T }>,\n ExtraOmitFields\n >;\n\n immutable: ExtendSchemaBuilder<Merge<Props, { immutable: true }>, ExtraOmitFields>;\n index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendSchemaBuilder<\n Merge<Props, { index: T }>,\n ExtraOmitFields\n >;\n\n max: <\n T extends D | Readonlyable<[D, S]>,\n D extends Decimal128Limit,\n S extends string,\n >(value: T) => ExtendSchemaBuilder<\n Merge<Props, Decimal128ValidationSchema>,\n 'max' | ExtraOmitFields\n >;\n\n min: <\n T extends D | Readonlyable<[D, S]>,\n D extends Decimal128Limit,\n S extends string,\n >(value: T) => ExtendSchemaBuilder<\n Merge<Props, Decimal128ValidationSchema>,\n 'min' | ExtraOmitFields\n >;\n\n nonRequired: Props;\n private: ExtendSchemaBuilder<Merge<Props, { private: true }>, ExtraOmitFields>;\n required: Merge<Props, { required: true }>;\n\n /**\n *
|
|
1
|
+
{"version":3,"file":"decimal128.js","names":[],"sources":["../../src/schema-builders/decimal128.ts"],"sourcesContent":["import { Decimal } from 'decimal.js';\nimport { Schema } from 'mongoose';\nimport type {\n DefaultType,\n IndexDirection,\n IndexOptions,\n Types,\n} from 'mongoose';\nimport type { Merge } from 'type-fest';\n\nimport type { Readonlyable } from '../types/_internals';\n\nimport { createBaseSchemaBuilderFactory } from './base';\n\ntype Decimal128Limit = Decimal.Value | Types.Decimal128 | { toString: () => string };\ntype ExtendSchemaBuilder<\n Props extends BaseProps,\n ExtraOmitFields extends string,\n> = Omit<\n Decimal128SchemaBuilder<Props, ExtraOmitFields>,\n ExtraOmitFields | keyof Props\n>;\n\ninterface BaseProps {\n type: typeof Schema.Types.Decimal128;\n}\n\ninterface Decimal128LimitValidator {\n max?: string;\n message: string;\n min?: string;\n type: 'max' | 'min';\n validator: (value: null | Types.Decimal128 | undefined) => boolean;\n}\n\nexport interface Decimal128SchemaBuilder<\n Props extends { type: typeof Schema.Types.Decimal128 } = { type: typeof Schema.Types.Decimal128 },\n ExtraOmitFields extends string = never,\n> {\n default: <\n T extends ((this: any, doc: any) => DefaultType<D>) | DefaultType<D> | null,\n D extends Types.Decimal128,\n >(value: T) => ExtendSchemaBuilder<\n Merge<Props, { default: T }>,\n ExtraOmitFields\n >;\n\n enum: <\n T extends\n | Readonlyable<Array<D | null>>\n | { [path: string]: D | null }\n | { message?: M; values: Readonlyable<Array<D | null>> },\n D extends Types.Decimal128,\n M extends string,\n >(value: T) => ExtendSchemaBuilder<\n Merge<Props, { enum: T }>,\n ExtraOmitFields\n >;\n\n immutable: ExtendSchemaBuilder<Merge<Props, { immutable: true }>, ExtraOmitFields>;\n index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendSchemaBuilder<\n Merge<Props, { index: T }>,\n ExtraOmitFields\n >;\n\n max: <\n T extends D | Readonlyable<[D, S]>,\n D extends Decimal128Limit,\n S extends string,\n >(value: T) => ExtendSchemaBuilder<\n Merge<Props, Decimal128ValidationSchema>,\n 'max' | ExtraOmitFields\n >;\n\n min: <\n T extends D | Readonlyable<[D, S]>,\n D extends Decimal128Limit,\n S extends string,\n >(value: T) => ExtendSchemaBuilder<\n Merge<Props, Decimal128ValidationSchema>,\n 'min' | ExtraOmitFields\n >;\n\n nonRequired: Props;\n private: ExtendSchemaBuilder<Merge<Props, { private: true }>, ExtraOmitFields>;\n required: Merge<Props, { required: true }>;\n\n /**\n * Configures a setter that rounds decimal values and formats them with a fixed number of decimal places.\n *\n * @remarks\n * Replaces the configured setter on this builder. The setter converts non-nullish values through Decimal.js\n * and returns a decimal string for Mongoose to cast to `Decimal128`. It returns `undefined` for `null` or\n * `undefined`. Decimal.js validates conversion and formatting when the setter processes a value,\n * not when the setter is configured.\n *\n * @param places - The non-negative integer number of decimal places. Defaults to `2`.\n * @param rounding - The Decimal.js rounding mode. Defaults to `Decimal.ROUND_DOWN`, which rounds toward zero.\n *\n * @returns This instance for chaining.\n */\n setRoundAndToFixedSetter: (places?: number, rounding?: Decimal.Rounding) => ExtendSchemaBuilder<\n Merge<Props, ToStringSetterSchema>,\n 'setRoundAndToFixedSetter' | ExtraOmitFields\n >;\n\n /**\n * Configures a getter that converts decimal values to strings.\n *\n * @remarks\n * Replaces the configured getter on this builder. The getter returns the value's string representation,\n * or `undefined` for `null` or `undefined`. It changes the read representation, not the stored BSON type.\n *\n * @returns This instance for chaining.\n */\n setToStringGetter: ExtendSchemaBuilder<Merge<Props, ToStringGetterSchema>, 'setToStringGetter' | ExtraOmitFields>;\n sparse: ExtendSchemaBuilder<Merge<Props, { sparse: true }>, ExtraOmitFields>;\n unique: ExtendSchemaBuilder<Merge<Props, { unique: true }>, ExtraOmitFields>;\n}\n\ninterface Decimal128ValidationSchema {\n validate: Decimal128LimitValidator[];\n}\n\ninterface ToStringGetterSchema {\n get: (value?: Types.Decimal128) => string | undefined;\n}\n\ninterface ToStringSetterSchema {\n set: (value?: { toString: () => string }) => string | undefined;\n}\n\n// Constants/Variables\nconst defaultMinValidateMessage = 'Path `{PATH}` ({VALUE}) is less than minimum allowed value ({MIN}).';\nconst defaultMaxValidateMessage = 'Path `{PATH}` ({VALUE}) is more than maximum allowed value ({MAX}).';\n\n// Functions\nconst baseBuilderFactory = createBaseSchemaBuilderFactory(Schema.Types.Decimal128);\n\nfunction createLimitValidator(\n type: 'max' | 'min',\n value: Decimal128Limit | Readonlyable<[Decimal128Limit, string]>,\n): Decimal128LimitValidator {\n const [limit, message] = Array.isArray(value)\n ? value as Readonly<[Decimal128Limit, string]>\n : [\n value,\n undefined,\n ];\n\n const decimalLimit = new Decimal(limit.toString());\n if (!decimalLimit.isFinite()) throw new RangeError(`Decimal128 ${type} limit must be finite`);\n\n return {\n message: message ?? (type === 'min' ? defaultMinValidateMessage : defaultMaxValidateMessage),\n [type]: decimalLimit.toString(),\n type,\n validator: (value) => {\n if (value === null || value === undefined) return true;\n\n const decimalValue = new Decimal(value.toString());\n return type === 'min' ? decimalValue.gte(decimalLimit) : decimalValue.lte(decimalLimit);\n },\n };\n}\n\nexport function decimal128SchemaBuilder() {\n const schema: Record<string, any> = {};\n const baseBuilder = baseBuilderFactory(schema);\n return new Proxy(\n baseBuilder,\n {\n get(target, key, receiver) {\n if (key === 'max' || key === 'min') {\n return (value: Decimal128Limit | Readonlyable<[Decimal128Limit, string]>) => {\n const validator = createLimitValidator(key, value);\n const validators = Array.isArray(schema.validate) ? schema.validate : [];\n schema.validate = [\n ...validators.filter(({ type }) => type !== key),\n validator,\n ];\n\n return receiver;\n };\n }\n\n if (key === 'setRoundAndToFixedSetter') {\n return (places: number = 2, rounding: Decimal.Rounding = Decimal.ROUND_DOWN) => {\n schema.set = (value?: { toString: () => string }) => {\n if (value !== undefined && value !== null) {\n const decimal = Decimal.isDecimal(value) ? value : new Decimal(value.toString());\n return decimal.toFixed(places, rounding);\n }\n };\n\n return receiver;\n };\n }\n\n if (key === 'setToStringGetter') {\n schema.get = (value?: Types.Decimal128) => value?.toString();\n return receiver;\n }\n\n return Reflect.get(target, key, receiver);\n },\n },\n ) as Decimal128SchemaBuilder;\n}\n"],"mappings":";;;;AAqIA,MAAM,4BAA4B;AAClC,MAAM,4BAA4B;AAGlC,MAAM,qBAAqB,+BAA+B,OAAO,MAAM,UAAU;AAEjF,SAAS,qBACL,MACA,OACwB;CACxB,MAAM,CAAC,OAAO,WAAW,MAAM,QAAQ,KAAK,IACtC,QACA,CACE,OACA,KAAA,CACJ;CAEJ,MAAM,eAAe,IAAI,QAAQ,MAAM,SAAS,CAAC;CACjD,IAAI,CAAC,aAAa,SAAS,GAAG,MAAM,IAAI,WAAW,cAAc,KAAK,sBAAsB;CAE5F,OAAO;EACH,SAAS,YAAY,SAAS,QAAQ,4BAA4B;GACjE,OAAO,aAAa,SAAS;EAC9B;EACA,YAAY,UAAU;GAClB,IAAI,UAAU,QAAQ,UAAU,KAAA,GAAW,OAAO;GAElD,MAAM,eAAe,IAAI,QAAQ,MAAM,SAAS,CAAC;GACjD,OAAO,SAAS,QAAQ,aAAa,IAAI,YAAY,IAAI,aAAa,IAAI,YAAY;EAC1F;CACJ;AACJ;AAEA,SAAgB,0BAA0B;CACtC,MAAM,SAA8B,CAAC;CACrC,MAAM,cAAc,mBAAmB,MAAM;CAC7C,OAAO,IAAI,MACP,aACA,EACI,IAAI,QAAQ,KAAK,UAAU;EACvB,IAAI,QAAQ,SAAS,QAAQ,OACzB,QAAQ,UAAqE;GACzE,MAAM,YAAY,qBAAqB,KAAK,KAAK;GACjD,MAAM,aAAa,MAAM,QAAQ,OAAO,QAAQ,IAAI,OAAO,WAAW,CAAC;GACvE,OAAO,WAAW,CACd,GAAG,WAAW,QAAQ,EAAE,WAAW,SAAS,GAAG,GAC/C,SACJ;GAEA,OAAO;EACX;EAGJ,IAAI,QAAQ,4BACR,QAAQ,SAAiB,GAAG,WAA6B,QAAQ,eAAe;GAC5E,OAAO,OAAO,UAAuC;IACjD,IAAI,UAAU,KAAA,KAAa,UAAU,MAEjC,QADgB,QAAQ,UAAU,KAAK,IAAI,QAAQ,IAAI,QAAQ,MAAM,SAAS,CAAC,EAAA,CAChE,QAAQ,QAAQ,QAAQ;GAE/C;GAEA,OAAO;EACX;EAGJ,IAAI,QAAQ,qBAAqB;GAC7B,OAAO,OAAO,UAA6B,OAAO,SAAS;GAC3D,OAAO;EACX;EAEA,OAAO,QAAQ,IAAI,QAAQ,KAAK,QAAQ;CAC5C,EACJ,CACJ;AACJ"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { Readonlyable } from "../types/_internals.js";
|
|
2
|
+
import { IndexDirection, IndexOptions, Schema, Types } from "mongoose";
|
|
3
|
+
import { Merge } from "type-fest";
|
|
4
|
+
//#region src/schema-builders/double.d.ts
|
|
5
|
+
type DoubleLimit = number | Types.Double;
|
|
6
|
+
type ExtendSchemaBuilder<Props extends BaseProps, ExtraOmitFields extends string> = Omit<DoubleSchemaBuilder<Props, ExtraOmitFields>, ExtraOmitFields | keyof Props>;
|
|
7
|
+
interface BaseProps {
|
|
8
|
+
type: typeof Schema.Types.Double;
|
|
9
|
+
}
|
|
10
|
+
interface DoubleLimitValidator {
|
|
11
|
+
max?: number;
|
|
12
|
+
message: string;
|
|
13
|
+
min?: number;
|
|
14
|
+
type: 'max' | 'min';
|
|
15
|
+
validator: (value: null | Types.Double | undefined) => boolean;
|
|
16
|
+
}
|
|
17
|
+
export interface DoubleSchemaBuilder<Props extends BaseProps = BaseProps, ExtraOmitFields extends string = never> {
|
|
18
|
+
default: <T extends ((this: any, doc: any) => D | null | undefined) | D | null | undefined, D extends Types.Double>(value: T) => ExtendSchemaBuilder<Merge<Props, {
|
|
19
|
+
default: T;
|
|
20
|
+
}>, ExtraOmitFields>;
|
|
21
|
+
immutable: ExtendSchemaBuilder<Merge<Props, {
|
|
22
|
+
immutable: true;
|
|
23
|
+
}>, ExtraOmitFields>;
|
|
24
|
+
index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendSchemaBuilder<Merge<Props, {
|
|
25
|
+
index: T;
|
|
26
|
+
}>, ExtraOmitFields>;
|
|
27
|
+
max: <T extends L | Readonlyable<[L, S]>, L extends DoubleLimit, S extends string>(value: T) => ExtendSchemaBuilder<Merge<Props, DoubleValidationSchema>, 'max' | ExtraOmitFields>;
|
|
28
|
+
min: <T extends L | Readonlyable<[L, S]>, L extends DoubleLimit, S extends string>(value: T) => ExtendSchemaBuilder<Merge<Props, DoubleValidationSchema>, 'min' | ExtraOmitFields>;
|
|
29
|
+
nonRequired: Props;
|
|
30
|
+
private: ExtendSchemaBuilder<Merge<Props, {
|
|
31
|
+
private: true;
|
|
32
|
+
}>, ExtraOmitFields>;
|
|
33
|
+
required: Merge<Props, {
|
|
34
|
+
required: true;
|
|
35
|
+
}>;
|
|
36
|
+
sparse: ExtendSchemaBuilder<Merge<Props, {
|
|
37
|
+
sparse: true;
|
|
38
|
+
}>, ExtraOmitFields>;
|
|
39
|
+
unique: ExtendSchemaBuilder<Merge<Props, {
|
|
40
|
+
unique: true;
|
|
41
|
+
}>, ExtraOmitFields>;
|
|
42
|
+
}
|
|
43
|
+
interface DoubleValidationSchema {
|
|
44
|
+
validate: DoubleLimitValidator[];
|
|
45
|
+
}
|
|
46
|
+
export declare function doubleSchemaBuilder(): DoubleSchemaBuilder;
|
|
47
|
+
//#endregion
|
|
48
|
+
//# sourceMappingURL=double.d.ts.map
|