@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/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,6 +1,9 @@
|
|
|
1
1
|
import { CustomMongooseOptions } from "./types/options.js";
|
|
2
|
+
import { Connection } from "mongoose";
|
|
2
3
|
//#region src/_internals.d.ts
|
|
3
|
-
declare const customMongooseOptions: CustomMongooseOptions;
|
|
4
|
+
export declare const customMongooseOptions: CustomMongooseOptions;
|
|
5
|
+
export declare const mongooseConnectionState: {
|
|
6
|
+
defaultConnection?: Connection;
|
|
7
|
+
};
|
|
4
8
|
//#endregion
|
|
5
|
-
export { customMongooseOptions };
|
|
6
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/assertions.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { Document, mongo } from "mongoose";
|
|
2
2
|
//#region src/assertions.d.ts
|
|
3
|
-
declare function assertMongooseUpdateSuccess<T extends Document>(updatePromise: Promise<mongo.UpdateResult<T>>, expectedModifiedCount?: number): Promise<void>;
|
|
3
|
+
export declare function assertMongooseUpdateSuccess<T extends Document>(updatePromise: Promise<mongo.UpdateResult<T>>, expectedModifiedCount?: number): Promise<void>;
|
|
4
4
|
//#endregion
|
|
5
|
-
export { assertMongooseUpdateSuccess };
|
|
6
5
|
//# sourceMappingURL=assertions.d.ts.map
|
package/dist/assertions.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"assertions.d.ts","names":[],"sources":["../src/assertions.ts"],"mappings":";;
|
|
1
|
+
{"version":3,"file":"assertions.d.ts","names":[],"sources":["../src/assertions.ts"],"mappings":";;wBAKsB,4BAA4B,UAAU,UACxD,eAAe,QAAQ,MAAM,aAAa,KAC1C,iCAAyB"}
|
package/dist/builders.d.ts
CHANGED
|
@@ -4,9 +4,8 @@ import { BuildMongooseModelOptions } from "./types/options.js";
|
|
|
4
4
|
import { mongooseAssertionsPlugin } from "./plugins/assertions.js";
|
|
5
5
|
import { Model, Schema } from "mongoose";
|
|
6
6
|
//#region src/builders.d.ts
|
|
7
|
-
type DoNotRemoveOrUseThisType = typeof mongooseAssertionsPlugin;
|
|
8
|
-
declare function buildMongooseModel<DocType, Model extends BaseMongoosePaginateModel<DocType, InstanceMethodsAndOverrides, QueryHelpers>, InstanceMethodsAndOverrides = object, QueryHelpers = object>(collection: string, name: string, schema: Schema<DocType, Model, InstanceMethodsAndOverrides, QueryHelpers>, options?: BuildMongooseModelOptions): Model;
|
|
9
|
-
declare function registerMongooseNormalizePlugin<DocType, Model$1 extends Model<DocType, QueryHelpers, InstanceMethodsAndOverrides>, InstanceMethodsAndOverrides = object, QueryHelpers = object>(schema: Schema<DocType, Model$1, InstanceMethodsAndOverrides, QueryHelpers>, recursive?: boolean, options?: MongooseNormalizePluginOptions): void;
|
|
7
|
+
export type DoNotRemoveOrUseThisType = typeof mongooseAssertionsPlugin;
|
|
8
|
+
export declare function buildMongooseModel<DocType, Model extends BaseMongoosePaginateModel<DocType, InstanceMethodsAndOverrides, QueryHelpers>, InstanceMethodsAndOverrides = object, QueryHelpers = object>(collection: string, name: string, schema: Schema<DocType, Model, InstanceMethodsAndOverrides, QueryHelpers>, options?: BuildMongooseModelOptions): Model;
|
|
9
|
+
export declare function registerMongooseNormalizePlugin<DocType, Model$1 extends Model<DocType, QueryHelpers, InstanceMethodsAndOverrides>, InstanceMethodsAndOverrides = object, QueryHelpers = object>(schema: Schema<DocType, Model$1, InstanceMethodsAndOverrides, QueryHelpers>, recursive?: boolean, options?: MongooseNormalizePluginOptions): void;
|
|
10
10
|
//#endregion
|
|
11
|
-
export { DoNotRemoveOrUseThisType, buildMongooseModel, registerMongooseNormalizePlugin };
|
|
12
11
|
//# sourceMappingURL=builders.d.ts.map
|
package/dist/builders.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"builders.d.ts","names":[],"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
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { CustomMongooseOptions } from "./types/options.js";
|
|
2
2
|
//#region src/options.d.ts
|
|
3
|
-
declare function setCustomMongooseOptions<K extends keyof CustomMongooseOptions>(key: K, value: CustomMongooseOptions[K]): void;
|
|
3
|
+
export declare function setCustomMongooseOptions<K extends keyof CustomMongooseOptions>(key: K, value: CustomMongooseOptions[K]): void;
|
|
4
4
|
//#endregion
|
|
5
|
-
export { setCustomMongooseOptions };
|
|
6
5
|
//# sourceMappingURL=options.d.ts.map
|
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"}
|
|
@@ -8,7 +8,6 @@ declare module 'mongoose' {
|
|
|
8
8
|
assertUpdateSuccess: (filter: QueryFilter<TRawDocType>, update: UpdateQuery<TRawDocType> | UpdateWithAggregationPipeline, options?: (mongo.UpdateOptions & MongooseUpdateQueryOptions<TRawDocType>) | null, expectedModifiedCount?: number) => Promise<void>;
|
|
9
9
|
}
|
|
10
10
|
}
|
|
11
|
-
declare function mongooseAssertionsPlugin<S extends Schema>(schema: S): void;
|
|
11
|
+
export declare function mongooseAssertionsPlugin<S extends Schema>(schema: S): void;
|
|
12
12
|
//#endregion
|
|
13
|
-
export { mongooseAssertionsPlugin };
|
|
14
13
|
//# sourceMappingURL=assertions.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"assertions.d.ts","names":[],"sources":["../../src/plugins/assertions.ts"],"mappings":";;;YAac,SACN,IAAI,MAAM,UACV,qBACA,eACA,YAAY,qBAEZ;IAEA,sBACI,SACI,YAAY,SAAS,GAAG,eAAe,SAAS,WAAW,mBAC3D,+BACJ,iBAAiB,cACjB,mCACC;;YAGC,MACN,aAEA,oBAEA,uBAEA,gBAEA,wBAAwB,iBAAiB,aAAa,mBAAmB,WAAW,eAAe,YAEnG;IAEA,sBACI,QAAQ,YAAY,cACpB,QAAQ,YAAY,eAAe,+BACnC,WAAW,MAAM,gBAAgB,2BAA2B,sBAC5D,mCACC;;;
|
|
1
|
+
{"version":3,"file":"assertions.d.ts","names":[],"sources":["../../src/plugins/assertions.ts"],"mappings":";;;YAac,SACN,IAAI,MAAM,UACV,qBACA,eACA,YAAY,qBAEZ;IAEA,sBACI,SACI,YAAY,SAAS,GAAG,eAAe,SAAS,WAAW,mBAC3D,+BACJ,iBAAiB,cACjB,mCACC;;YAGC,MACN,aAEA,oBAEA,uBAEA,gBAEA,wBAAwB,iBAAiB,aAAa,mBAAmB,WAAW,eAAe,YAEnG;IAEA,sBACI,QAAQ,YAAY,cACpB,QAAQ,YAAY,eAAe,+BACnC,WAAW,MAAM,gBAAgB,2BAA2B,sBAC5D,mCACC;;;wBAIG,yBAAyB,UAAU,QAAQ,QAAQ"}
|
|
@@ -1,35 +1,41 @@
|
|
|
1
1
|
import { Schema } from "mongoose";
|
|
2
2
|
//#region src/plugins/normalize.d.ts
|
|
3
|
-
interface MongooseNormalizePluginOptions {
|
|
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
|
-
declare function mongooseNormalizePlugin<S extends Schema>(schema: S, pluginOptions?: MongooseNormalizePluginOptions): void;
|
|
39
|
+
export declare function mongooseNormalizePlugin<S extends Schema>(schema: S, pluginOptions?: MongooseNormalizePluginOptions): void;
|
|
33
40
|
//#endregion
|
|
34
|
-
export { MongooseNormalizePluginOptions, mongooseNormalizePlugin };
|
|
35
41
|
//# sourceMappingURL=normalize.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"normalize.d.ts","names":[],"sources":["../../src/plugins/normalize.ts"],"mappings":";;
|
|
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 = [];
|
|
@@ -30,14 +32,19 @@ function mongooseNormalizePlugin(schema, pluginOptions) {
|
|
|
30
32
|
let _id = copiedRet._id;
|
|
31
33
|
delete copiedRet._id;
|
|
32
34
|
if (pluginOptions?.toHexIdIfObjectId !== false && _id instanceof Types.ObjectId) _id = _id.toHexString();
|
|
33
|
-
if (_id !== void 0)
|
|
34
|
-
|
|
35
|
+
if (_id !== void 0) {
|
|
36
|
+
if (pluginOptions?.convertIdField !== false) copiedRet.id = _id;
|
|
37
|
+
else copiedRet._id = _id;
|
|
38
|
+
}
|
|
35
39
|
for (const path of privatePaths) unset(copiedRet, path);
|
|
36
40
|
for (const path of decimalPaths) {
|
|
37
41
|
const value = get(copiedRet, path);
|
|
38
42
|
if (value) set(copiedRet, path, value.toString());
|
|
39
43
|
}
|
|
40
|
-
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
|
+
}
|
|
41
48
|
return copiedRet;
|
|
42
49
|
}
|
|
43
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,7 +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;
|
|
4
|
-
declare function createBaseSchemaBuilderFactory<Builder = Readonly<Record<string, any>>>(type: BaseSchemaType): (schema?: Record<string, any>) => Builder;
|
|
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
|
+
export declare function createBaseSchemaBuilderFactory<Builder = Readonly<Record<string, any>>>(type: BaseSchemaType): (schema?: Record<string, any>) => Builder;
|
|
5
5
|
//#endregion
|
|
6
|
-
export { createBaseSchemaBuilderFactory };
|
|
7
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\nconst isFunctionKeys = new Set([\n 'default',\n 'enum',\n 'index',\n 'max',\n 'maxlength',\n 'min',\n 'minlength',\n]);\n\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"}
|
|
@@ -5,7 +5,7 @@ type ExtendSchemaBuilder<Props extends BaseProps, ExtraOmitFields extends string
|
|
|
5
5
|
interface BaseProps {
|
|
6
6
|
type: BooleanSchemaDefinition;
|
|
7
7
|
}
|
|
8
|
-
interface BooleanSchemaBuilder<Props extends BaseProps = BaseProps, ExtraOmitFields extends string = never> {
|
|
8
|
+
export interface BooleanSchemaBuilder<Props extends BaseProps = BaseProps, ExtraOmitFields extends string = never> {
|
|
9
9
|
default: <T extends ((this: any, doc: any) => DefaultType<D>) | DefaultType<D> | null, D extends boolean>(value: T) => ExtendSchemaBuilder<Merge<Props, {
|
|
10
10
|
default: T;
|
|
11
11
|
}>, ExtraOmitFields>;
|
|
@@ -29,7 +29,6 @@ interface BooleanSchemaBuilder<Props extends BaseProps = BaseProps, ExtraOmitFie
|
|
|
29
29
|
unique: true;
|
|
30
30
|
}>, ExtraOmitFields>;
|
|
31
31
|
}
|
|
32
|
-
declare const booleanSchemaBuilder: (schema?: Record<string, any>) => BooleanSchemaBuilder<BaseProps, never>;
|
|
32
|
+
export declare const booleanSchemaBuilder: (schema?: Record<string, any>) => BooleanSchemaBuilder<BaseProps, never>;
|
|
33
33
|
//#endregion
|
|
34
|
-
export { BooleanSchemaBuilder, booleanSchemaBuilder };
|
|
35
34
|
//# sourceMappingURL=boolean.d.ts.map
|
|
@@ -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;;
|
|
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"}
|
|
@@ -6,7 +6,7 @@ type ExtendSchemaBuilder<Props extends BaseProps, ExtraOmitFields extends string
|
|
|
6
6
|
interface BaseProps {
|
|
7
7
|
type: DateSchemaDefinition;
|
|
8
8
|
}
|
|
9
|
-
interface DateSchemaBuilder<Props extends {
|
|
9
|
+
export interface DateSchemaBuilder<Props extends {
|
|
10
10
|
type: DateSchemaDefinition;
|
|
11
11
|
} = {
|
|
12
12
|
type: DateSchemaDefinition;
|
|
@@ -48,9 +48,8 @@ interface DateSchemaBuilder<Props extends {
|
|
|
48
48
|
unique: true;
|
|
49
49
|
}>, ExtraOmitFields>;
|
|
50
50
|
}
|
|
51
|
-
declare const dateSchemaBuilder: (schema?: Record<string, any>) => DateSchemaBuilder<{
|
|
51
|
+
export declare const dateSchemaBuilder: (schema?: Record<string, any>) => DateSchemaBuilder<{
|
|
52
52
|
type: DateSchemaDefinition;
|
|
53
53
|
}, never>;
|
|
54
54
|
//#endregion
|
|
55
|
-
export { DateSchemaBuilder, dateSchemaBuilder };
|
|
56
55
|
//# sourceMappingURL=date.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"date.d.ts","names":[],"sources":["../../src/schema-builders/date.ts"],"mappings":";;;;KAYK,oBACD,cAAc,WACd,kCACA,KACA,kBAAkB,OAAO,kBACzB,wBAAwB;UAGlB;EACN,MAAM;;
|
|
1
|
+
{"version":3,"file":"date.d.ts","names":[],"sources":["../../src/schema-builders/date.ts"],"mappings":";;;;KAYK,oBACD,cAAc,WACd,kCACA,KACA,kBAAkB,OAAO,kBACzB,wBAAwB;UAGlB;EACN,MAAM;;iBAGO,kBACb;EAAgB,MAAM;;EAA2B,MAAM;GACvD;EAEA,UACI,YAAY,WAAW,aAAa,YAAY,MAAM,YAAY,WAClE,UAAU,YACZ,OAAO,MAAM,oBACX,MAAM;IAAS,SAAS;MACxB;EAGJ,OACI,UACE,aAAa,MAAM;KAChB,eAAe;;IAChB,UAAU;IAAG,QAAQ,aAAa,MAAM;KAC5C,UAAU,YACV,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,YACV,kBACF,OAAO,MAAM,oBACX,MAAM;IAAS,KAAK;MACpB;EAGJ,MACI,UAAU,IAAI,cAAc,GAAG,KAC/B,UAAU,YACV,kBACF,OAAO,MAAM,oBACX,MAAM;IAAS,KAAK;MACpB;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,oBAAiB,SAAA,wBAAA;EAtD6B,MAAA"}
|