@kikiutils/mongoose 5.1.5 → 5.2.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 +2 -2
- package/dist/_internals.d.ts +7 -0
- package/dist/_internals.d.ts.map +1 -0
- package/dist/_internals.js.map +1 -1
- package/dist/constants.js.map +1 -1
- package/dist/options.js.map +1 -1
- package/dist/schema-builders/boolean.d.ts.map +1 -1
- package/dist/types/options.d.ts +12 -0
- package/dist/types/options.d.ts.map +1 -1
- package/dist/utils.d.ts +4 -2
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +7 -3
- package/dist/utils.js.map +1 -1
- package/package.json +25 -30
- package/src/_internals.ts +1 -1
- package/src/constants.ts +1 -1
- package/src/options.ts +3 -3
- package/src/types/options.ts +16 -2
- package/src/utils.ts +28 -6
package/README.md
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_internals.d.ts","names":[],"sources":["../src/_internals.ts"],"sourcesContent":[],"mappings":";;;cAEa,uBAAuB"}
|
package/dist/_internals.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"_internals.js","names":["customMongooseOptions: CustomMongooseOptions"],"sources":["../src/_internals.ts"],"sourcesContent":["import type { CustomMongooseOptions } from '
|
|
1
|
+
{"version":3,"file":"_internals.js","names":["customMongooseOptions: CustomMongooseOptions"],"sources":["../src/_internals.ts"],"sourcesContent":["import type { CustomMongooseOptions } from './types/options';\n\nexport const customMongooseOptions: CustomMongooseOptions = {};\n"],"mappings":";AAEA,MAAaA,wBAA+C,EAAE"}
|
package/dist/constants.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"constants.js","names":["mongooseConnections: MongooseConnections"],"sources":["../src/constants.ts"],"sourcesContent":["import type { MongooseConnections } from '
|
|
1
|
+
{"version":3,"file":"constants.js","names":["mongooseConnections: MongooseConnections"],"sources":["../src/constants.ts"],"sourcesContent":["import type { MongooseConnections } from './types';\n\n/**\n * Mongoose connections.\n *\n * This constant is a record of named Mongoose connections, allowing multiple\n * database connections to be managed within the application. An optional `default`\n * connection can also be specified for use when a specific connection is not provided.\n */\nexport const mongooseConnections: MongooseConnections = {};\n"],"mappings":";;;;;;;;AASA,MAAaA,sBAA2C,EAAE"}
|
package/dist/options.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"options.js","names":[],"sources":["../src/options.ts"],"sourcesContent":["import { customMongooseOptions } from '
|
|
1
|
+
{"version":3,"file":"options.js","names":[],"sources":["../src/options.ts"],"sourcesContent":["import { customMongooseOptions } from './_internals';\nimport type { CustomMongooseOptions } from './types/options';\nimport type { buildMongooseModel } from './utils';\n\nexport type DoNotRemoveOrUseThisType = typeof buildMongooseModel;\n\n/**\n * Sets custom options.\n *\n * For example, you can set {@link CustomMongooseOptions.beforeModelBuild} to define a function that will be called\n * every time {@link buildMongooseModel} is used. Inside this function, you can modify\n * the schema, allowing you to apply common settings to all global schemas.\n */\nexport function setCustomMongooseOptions<K extends keyof CustomMongooseOptions>(\n key: K,\n value: CustomMongooseOptions[K],\n) {\n customMongooseOptions[key] = value;\n}\n"],"mappings":";;;;;;;;;;AAaA,SAAgB,yBACZ,KACA,OACF;AACE,uBAAsB,OAAO"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"boolean.d.ts","names":[],"sources":["../../src/schema-builders/boolean.ts"],"sourcesContent":[],"mappings":";;;;KAUY,yCACM,0DAEd,KACA,qBAAqB,OAAO,kBAC5B,wBAAwB;UAGX,sBAAA;EARL,IAAA,EASF,uBAT4B;;AACpB,UAWD,oBAXC,CAAA,cAYA,sBAZA,GAYyB,sBAZzB,EAAA,wBAAA,MAAA,GAAA,KAAA,CAAA,CAAA;SAGO,EAAA,CAAA,UAAA,CAAA,CAAA,IAAA,EAAA,GAAA,EAAA,GAAA,EAAA,GAAA,EAAA,GAamB,WAbnB,CAa+B,CAb/B,CAAA,CAAA,GAaqC,WAbrC,CAaiD,CAbjD,CAAA,GAAA,IAAA,EAAA,UAAA,OAAA,CAAA,CAAA,KAAA,EAeZ,CAfY,EAAA,GAeN,0BAfM,CAgBjB,KAhBiB,CAgBX,KAhBW,EAAA;IAAO,OAAA,EAgBA,CAhBA;MAiBxB,eAjBJ,CAAA;OACA,EAAA,CAAA,UAAA,OAAA,GAmB4B,cAnB5B,GAmB6C,YAnB7C,CAAA,CAAA,KAAA,EAmBkE,CAnBlE,EAAA,GAmBwE,0BAnBxE,CAoBI,KApBJ,CAoBU,KApBV,EAAA;IAAwB,KAAA,EAoBE,CApBF;MAqBpB,eAvBJ,CAAA;EAAI,WAAA,EA0BS,KA1BT;EAKS,OAAA,EAsBJ,
|
|
1
|
+
{"version":3,"file":"boolean.d.ts","names":[],"sources":["../../src/schema-builders/boolean.ts"],"sourcesContent":[],"mappings":";;;;KAUY,yCACM,0DAEd,KACA,qBAAqB,OAAO,kBAC5B,wBAAwB;UAGX,sBAAA;EARL,IAAA,EASF,uBAT4B;;AACpB,UAWD,oBAXC,CAAA,cAYA,sBAZA,GAYyB,sBAZzB,EAAA,wBAAA,MAAA,GAAA,KAAA,CAAA,CAAA;SAGO,EAAA,CAAA,UAAA,CAAA,CAAA,IAAA,EAAA,GAAA,EAAA,GAAA,EAAA,GAAA,EAAA,GAamB,WAbnB,CAa+B,CAb/B,CAAA,CAAA,GAaqC,WAbrC,CAaiD,CAbjD,CAAA,GAAA,IAAA,EAAA,UAAA,OAAA,CAAA,CAAA,KAAA,EAeZ,CAfY,EAAA,GAeN,0BAfM,CAgBjB,KAhBiB,CAgBX,KAhBW,EAAA;IAAO,OAAA,EAgBA,CAhBA;MAiBxB,eAjBJ,CAAA;OACA,EAAA,CAAA,UAAA,OAAA,GAmB4B,cAnB5B,GAmB6C,YAnB7C,CAAA,CAAA,KAAA,EAmBkE,CAnBlE,EAAA,GAmBwE,0BAnBxE,CAoBI,KApBJ,CAoBU,KApBV,EAAA;IAAwB,KAAA,EAoBE,CApBF;MAqBpB,eAvBJ,CAAA;EAAI,WAAA,EA0BS,KA1BT;EAKS,OAAA,EAsBJ,0BAtB0B,CAsBC,KArB9B,CAqBoC,KArBpC,EAAA;IAGO,OAAA,EAAA,IAAA;EAAoB,CAAA,CAAA,EAkBoC,eAlBpC,CAAA;UACnB,EAkBJ,KAlBI,CAkBE,KAlBF,EAAA;IAAyB,QAAA,EAAA,IAAA;;QAIC,EAehC,0BAfgC,CAeL,KAfK,CAeC,KAfD,EAAA;IAA8B,MAAA,EAAA,IAAA;MAeH,eAfT,CAAA;QAEjD,EAcD,0BAdC,CAc0B,KAd1B,CAcgC,KAdhC,EAAA;IACC,MAAA,EAAA,IAAA;MAayD,eAbvC,CAAA;;AACxB,cAeK,oBAfL,EAAA,CAAA,MAAA,CAAA,EAeyB,MAfzB,CAAA,MAAA,EAAA,GAAA,CAAA,EAAA,GAeyB,oBAfzB,CAeyB,sBAfzB,EAAA,KAAA,CAAA"}
|
package/dist/types/options.d.ts
CHANGED
|
@@ -28,7 +28,19 @@ interface BuildMongooseModelOptions {
|
|
|
28
28
|
* @default true
|
|
29
29
|
*/
|
|
30
30
|
enableNormalizePlugin?: boolean;
|
|
31
|
+
/**
|
|
32
|
+
* Options for the normalize plugin.
|
|
33
|
+
*/
|
|
31
34
|
normalizePluginOptions?: MongooseNormalizePluginOptions;
|
|
35
|
+
/**
|
|
36
|
+
* Whether to recursively register the normalize plugin on child schemas.
|
|
37
|
+
*
|
|
38
|
+
* If `true`, the plugin will be automatically applied to all nested schemas.
|
|
39
|
+
* If `false`, only the root schema will be affected.
|
|
40
|
+
*
|
|
41
|
+
* @default true
|
|
42
|
+
*/
|
|
43
|
+
normalizePluginRecursive?: boolean;
|
|
32
44
|
/**
|
|
33
45
|
* Configures Mongoose timestamps.
|
|
34
46
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"options.d.ts","names":[],"sources":["../../src/types/options.ts"],"sourcesContent":[],"mappings":";;;;;KASY,wBAAA,UAAkC;;AAA9C;AASA;;;;;
|
|
1
|
+
{"version":3,"file":"options.d.ts","names":[],"sources":["../../src/types/options.ts"],"sourcesContent":[],"mappings":";;;;;KASY,wBAAA,UAAkC;;AAA9C;AASA;;;;;AA8CiD,UA9ChC,yBAAA,CA8CgC;EAGhC;;;;;;YAWL,CAAA,EArDK,UAqDL;;;;;;;;;;;;;2BAtCiB;;;;;;;;;;;;;;;;;;;;;;yBAwBF;;UAGV,qBAAA;;;;6CAMK,0BAA0B,SAAS,6BAA6B,oFAItE,OACJ,SACA,OACA,6BACA"}
|
package/dist/utils.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { MongooseNormalizePluginOptions } from "./plugins/normalize.js";
|
|
2
2
|
import { setCustomMongooseOptions } from "./options.js";
|
|
3
|
+
import { BuildMongooseModelOptions } from "./types/options.js";
|
|
3
4
|
import { Schema, Types, mongo } from "mongoose";
|
|
4
5
|
|
|
5
6
|
//#region src/utils.d.ts
|
|
@@ -65,6 +66,7 @@ declare function buildMongooseModel<DocType, Model extends BaseMongoosePaginateM
|
|
|
65
66
|
* @returns The corresponding document or null if not found.
|
|
66
67
|
*/
|
|
67
68
|
declare function mongooseDocumentOrObjectIdToDocument<D extends MongooseHydratedDocument<DocType, InstanceMethodsAndOverrides, QueryHelpers>, DocType, InstanceMethodsAndOverrides, QueryHelpers>(documentOrObjectId: MongooseDocumentOrObjectId<D>, model: BaseMongoosePaginateModel<DocType, InstanceMethodsAndOverrides, QueryHelpers>, selectFields?: string[]): Promise<D | null>;
|
|
69
|
+
declare function registerMongooseNormalizePlugin<DocType, Model extends BaseMongoosePaginateModel<DocType, InstanceMethodsAndOverrides, QueryHelpers>, InstanceMethodsAndOverrides = object, QueryHelpers = object>(schema: Schema<DocType, Model, InstanceMethodsAndOverrides, QueryHelpers>, recursive?: boolean, options?: MongooseNormalizePluginOptions): void;
|
|
68
70
|
/**
|
|
69
71
|
* Converts the input, which can be a MongoDB ObjectId or a string,
|
|
70
72
|
* into a 24-character hexadecimal string representation of the ObjectId.
|
|
@@ -75,5 +77,5 @@ declare function mongooseDocumentOrObjectIdToDocument<D extends MongooseHydrated
|
|
|
75
77
|
*/
|
|
76
78
|
declare function toObjectIdHexString(id: string | Types.ObjectId): string;
|
|
77
79
|
//#endregion
|
|
78
|
-
export { DoNotRemoveOrUseThisType, assertMongooseUpdateSuccess, buildMongooseModel, mongooseDocumentOrObjectIdToDocument, toObjectIdHexString };
|
|
80
|
+
export { DoNotRemoveOrUseThisType, assertMongooseUpdateSuccess, buildMongooseModel, mongooseDocumentOrObjectIdToDocument, registerMongooseNormalizePlugin, toObjectIdHexString };
|
|
79
81
|
//# sourceMappingURL=utils.d.ts.map
|
package/dist/utils.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","names":[],"sources":["../src/utils.ts"],"sourcesContent":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"utils.d.ts","names":[],"sources":["../src/utils.ts"],"sourcesContent":[],"mappings":";;;;;;KAeY,wBAAA,UAAkC;AAA9C;AAkBA;;;;;;;;AAuCA;;;;;AAEkB,iBAzCI,2BAyCJ,CAAA,UAzC0C,KAAA,CAAM,IAAA,CAAK,QAyCrD,CAAA,CAAA,aAAA,EAxCC,OAwCD,CAxCS,KAAA,CAAM,YAwCf,CAxC4B,CAwC5B,CAAA,CAAA,EAAA,qBAAA,CAAA,EAAA,MAAA,CAAA,EAvCW,OAuCX,CAAA,IAAA,CAAA;;;;;;;;;AAuClB;;;;;;;;;;;;;;;AAiBA;;;;AAEkF,iBA5DlE,kBA4DkE,CAAA,OAAA,EAAA,cA1DhE,yBA0DgE,CA1DtC,OA0DsC,EA1D7B,2BA0D6B,EA1DA,YA0DA,CAAA,EAAA,8BAAA,MAAA,EAAA,eAAA,MAAA,CAAA,CAAA,UAAA,EAAA,MAAA,EAAA,IAAA,EAAA,MAAA,EAAA,MAAA,EApDtE,MAoDsE,CApD/D,OAoD+D,EApDtD,KAoDsD,EApD/C,2BAoD+C,EApDlB,YAoDkB,CAAA,EAAA,OAAA,CAAA,EAnDpE,yBAmDoE,CAAA,EAlD/E,KAkD+E;;;;;;;;;AAyBlF;;;;;;;;;iBA5CsB,+CACR,yBAAyB,SAAS,6BAA6B,uFAKrD,2BAA2B,WACxC,0BAA0B,SAAS,6BAA6B,yCAChD,QAAA;iBASX,uDAEE,0BAA0B,SAAS,6BAA6B,oFAItE,OAAO,SAAS,OAAO,6BAA6B,8CAElD;;;;;;;;;iBAmBE,mBAAA,cAAiC,KAAA,CAAM"}
|
package/dist/utils.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { mongooseConnections } from "./constants.js";
|
|
2
1
|
import { customMongooseOptions } from "./_internals.js";
|
|
2
|
+
import { mongooseConnections } from "./constants.js";
|
|
3
3
|
import { mongooseNormalizePlugin } from "./plugins/normalize.js";
|
|
4
4
|
import mongoose, { Types } from "mongoose";
|
|
5
5
|
import mongooseAggregatePaginate from "mongoose-aggregate-paginate-v2";
|
|
@@ -53,7 +53,7 @@ async function assertMongooseUpdateSuccess(updatePromise, expectedModifiedCount
|
|
|
53
53
|
* @returns The created Mongoose model.
|
|
54
54
|
*/
|
|
55
55
|
function buildMongooseModel(collection, name, schema, options) {
|
|
56
|
-
if (options?.enableNormalizePlugin !== false) schema
|
|
56
|
+
if (options?.enableNormalizePlugin !== false) registerMongooseNormalizePlugin(schema, options?.normalizePluginRecursive, options?.normalizePluginOptions);
|
|
57
57
|
schema.plugin(mongooseAggregatePaginate);
|
|
58
58
|
schema.plugin(mongoosePaginate);
|
|
59
59
|
schema.set("timestamps", options?.timestamps ?? schema.get("timestamps") ?? true);
|
|
@@ -81,6 +81,10 @@ async function mongooseDocumentOrObjectIdToDocument(documentOrObjectId, model, s
|
|
|
81
81
|
if (typeof documentOrObjectId === "string" || documentOrObjectId instanceof Types.ObjectId) return await model.findById(documentOrObjectId).select(selectFields || []);
|
|
82
82
|
return documentOrObjectId;
|
|
83
83
|
}
|
|
84
|
+
function registerMongooseNormalizePlugin(schema, recursive = true, options) {
|
|
85
|
+
schema.plugin(mongooseNormalizePlugin, options);
|
|
86
|
+
if (recursive) schema.childSchemas.forEach(({ schema: schema$1 }) => registerMongooseNormalizePlugin(schema$1, recursive, options));
|
|
87
|
+
}
|
|
84
88
|
/**
|
|
85
89
|
* Converts the input, which can be a MongoDB ObjectId or a string,
|
|
86
90
|
* into a 24-character hexadecimal string representation of the ObjectId.
|
|
@@ -94,5 +98,5 @@ function toObjectIdHexString(id) {
|
|
|
94
98
|
}
|
|
95
99
|
|
|
96
100
|
//#endregion
|
|
97
|
-
export { assertMongooseUpdateSuccess, buildMongooseModel, mongooseDocumentOrObjectIdToDocument, toObjectIdHexString };
|
|
101
|
+
export { assertMongooseUpdateSuccess, buildMongooseModel, mongooseDocumentOrObjectIdToDocument, registerMongooseNormalizePlugin, toObjectIdHexString };
|
|
98
102
|
//# sourceMappingURL=utils.js.map
|
package/dist/utils.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","names":[],"sources":["../src/utils.ts"],"sourcesContent":["import mongoose, { Types } from 'mongoose';\nimport type {\n mongo,\n Schema,\n} from 'mongoose';\nimport mongooseAggregatePaginate from 'mongoose-aggregate-paginate-v2';\nimport mongoosePaginate from 'mongoose-paginate-v2';\n\nimport { customMongooseOptions } from '
|
|
1
|
+
{"version":3,"file":"utils.js","names":["schema"],"sources":["../src/utils.ts"],"sourcesContent":["import mongoose, { Types } from 'mongoose';\nimport type {\n mongo,\n Schema,\n} from 'mongoose';\nimport mongooseAggregatePaginate from 'mongoose-aggregate-paginate-v2';\nimport mongoosePaginate from 'mongoose-paginate-v2';\n\nimport { customMongooseOptions } from './_internals';\nimport { mongooseConnections } from './constants';\nimport type { setCustomMongooseOptions } from './options';\nimport { mongooseNormalizePlugin } from './plugins/normalize';\nimport type { MongooseNormalizePluginOptions } from './plugins/normalize';\nimport type { BuildMongooseModelOptions } from './types/options';\n\nexport type DoNotRemoveOrUseThisType = typeof setCustomMongooseOptions;\n\n// const normalizePluginRegisteredSymbol = Symbol('normalizePluginRegistered');\n\n/**\n * Asserts that a Mongoose update operation was successful by checking that it was acknowledged\n * and that at least the expected number of documents were modified.\n *\n * This function is intended for use with `updateOne`, `updateMany`, or any update operation\n * that returns a MongoDB `UpdateResult`.\n *\n * @template T - The shape of the document being updated.\n * @param updatePromise - A promise returned from a Mongoose update operation.\n * @param expectedModifiedCount - The minimum number of documents expected to be modified. Defaults to 1.\n * @throws Will throw an error if:\n * - The operation was not acknowledged by MongoDB,\n * - Fewer documents were modified than expected.\n */\nexport async function assertMongooseUpdateSuccess<T extends mongo.BSON.Document>(\n updatePromise: Promise<mongo.UpdateResult<T>>,\n expectedModifiedCount = 1,\n) {\n const updateResult = await updatePromise;\n if (!updateResult.acknowledged) throw new Error('Update was not acknowledged');\n if (updateResult.modifiedCount < expectedModifiedCount) {\n // eslint-disable-next-line style/max-len\n throw new Error(`Expected to modify at least ${expectedModifiedCount} document(s), but modified ${updateResult.modifiedCount}`);\n }\n}\n\n/**\n * Builds and returns a Mongoose model with specified schema and options.\n *\n * This function sets up a Mongoose model with optional plugins and timestamp settings.\n * It can optionally use a specified connection or the default connection.\n *\n * Determine the connection to use:\n * 1. Use the connection provided in the options, if available.\n * 2. Otherwise, use the default connection stored in `mongooseConnections`.\n * 3. If there's no default connection, create one using the MongoDB URI from the environment variables,\n * or fallback to a local MongoDB instance.\n *\n * You can set a function to be executed before the final build by using {@link setCustomMongooseOptions}.\n * However, ensure that this option is configured before calling {@link buildMongooseModel}.\n *\n * @template DocType - The type of the document.\n * @template Model - The type of the model, which extends `BaseMongoosePaginateModel`.\n * @template InstanceMethodsAndOverrides - The type for instance methods and overrides.\n * @template QueryHelpers - The type for additional query helpers.\n *\n * @param collection - The name of the collection.\n * @param name - The name of the model.\n * @param schema - The Mongoose schema to use for the model.\n * @param options - Optional settings for building the model.\n *\n * @returns The created Mongoose model.\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(mongoosePaginate);\n schema.set('timestamps', options?.timestamps ?? schema.get('timestamps') ?? true);\n customMongooseOptions.beforeModelBuild?.(schema);\n // eslint-disable-next-line style/max-len\n const connection = options?.connection || (mongooseConnections.default ||= mongoose.createConnection(process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017'));\n return connection.model<DocType, Model, QueryHelpers>(name, schema, collection);\n}\n\n/**\n * Converts a document or ObjectId to a document using the specified model.\n *\n * This function takes a document or ObjectId and returns the corresponding document\n * from the database using the specified model. Optionally, specific fields can be selected.\n *\n * @template D - The type of the hydrated document.\n * @template DocType - The type of the document.\n * @template InstanceMethodsAndOverrides - The type for instance methods and overrides.\n * @template QueryHelpers - The type for additional query helpers.\n *\n * @param documentOrObjectId - The document or ObjectId to convert.\n * @param model - The Mongoose model to use for the conversion.\n * @param selectFields - Optional fields to select from the document.\n *\n * @returns The corresponding document or null if not found.\n */\nexport async function mongooseDocumentOrObjectIdToDocument<\n D extends MongooseHydratedDocument<DocType, InstanceMethodsAndOverrides, QueryHelpers>,\n DocType,\n InstanceMethodsAndOverrides,\n QueryHelpers,\n>(\n documentOrObjectId: MongooseDocumentOrObjectId<D>,\n model: BaseMongoosePaginateModel<DocType, InstanceMethodsAndOverrides, QueryHelpers>,\n selectFields?: string[],\n) {\n if (typeof documentOrObjectId === 'string' || documentOrObjectId instanceof Types.ObjectId) {\n return (await model.findById(documentOrObjectId).select(selectFields || [])) as D | null;\n }\n\n return documentOrObjectId;\n}\n\nexport function registerMongooseNormalizePlugin<\n DocType,\n Model extends BaseMongoosePaginateModel<DocType, InstanceMethodsAndOverrides, QueryHelpers>,\n InstanceMethodsAndOverrides = object,\n QueryHelpers = object,\n>(\n schema: Schema<DocType, Model, InstanceMethodsAndOverrides, QueryHelpers>,\n recursive: boolean = true,\n options?: MongooseNormalizePluginOptions,\n) {\n // TODO: Check if plugin is already registered, Do you really need to check?\n // if ((schema as any)[normalizePluginRegisteredSymbol]) return;\n // (schema as any)[normalizePluginRegisteredSymbol] = true;\n schema.plugin(mongooseNormalizePlugin, options);\n if (recursive) {\n schema.childSchemas.forEach(({ schema }) => registerMongooseNormalizePlugin(schema, recursive, options));\n }\n}\n\n/**\n * Converts the input, which can be a MongoDB ObjectId or a string,\n * into a 24-character hexadecimal string representation of the ObjectId.\n *\n * @param {string | Types.ObjectId} id - The ObjectId instance or its string representation\n *\n * @returns {string} The hexadecimal string representation of the ObjectId\n */\nexport function toObjectIdHexString(id: string | Types.ObjectId) {\n return id instanceof Types.ObjectId ? id.toHexString() : id;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAiCA,eAAsB,4BAClB,eACA,wBAAwB,GAC1B;CACE,MAAM,eAAe,MAAM;AAC3B,KAAI,CAAC,aAAa,aAAc,OAAM,IAAI,MAAM,8BAA8B;AAC9E,KAAI,aAAa,gBAAgB,sBAE7B,OAAM,IAAI,MAAM,+BAA+B,sBAAsB,6BAA6B,aAAa,gBAAgB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+BvI,SAAgB,mBAMZ,YACA,MACA,QACA,SACK;AACL,KAAI,SAAS,0BAA0B,MACnC,iCAAgC,QAAQ,SAAS,0BAA0B,SAAS,uBAAuB;AAG/G,QAAO,OAAO,0BAA0B;AACxC,QAAO,OAAO,iBAAiB;AAC/B,QAAO,IAAI,cAAc,SAAS,cAAc,OAAO,IAAI,aAAa,IAAI,KAAK;AACjF,uBAAsB,mBAAmB,OAAO;AAGhD,SADmB,SAAS,eAAe,oBAAoB,YAAY,SAAS,iBAAiB,QAAQ,IAAI,eAAe,4BAA4B,GAC1I,MAAoC,MAAM,QAAQ,WAAW;;;;;;;;;;;;;;;;;;;AAoBnF,eAAsB,qCAMlB,oBACA,OACA,cACF;AACE,KAAI,OAAO,uBAAuB,YAAY,8BAA8B,MAAM,SAC9E,QAAQ,MAAM,MAAM,SAAS,mBAAmB,CAAC,OAAO,gBAAgB,EAAE,CAAC;AAG/E,QAAO;;AAGX,SAAgB,gCAMZ,QACA,YAAqB,MACrB,SACF;AAIE,QAAO,OAAO,yBAAyB,QAAQ;AAC/C,KAAI,UACA,QAAO,aAAa,SAAS,EAAE,uBAAa,gCAAgCA,UAAQ,WAAW,QAAQ,CAAC;;;;;;;;;;AAYhH,SAAgB,oBAAoB,IAA6B;AAC7D,QAAO,cAAc,MAAM,WAAW,GAAG,aAAa,GAAG"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kikiutils/mongoose",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "5.
|
|
4
|
+
"version": "5.2.0",
|
|
5
5
|
"description": "A Mongoose plugin for enhanced JSON normalization and common schema creation, with built-in support for pagination and automatic Decimal128 conversion.",
|
|
6
6
|
"author": "kiki-kanri",
|
|
7
7
|
"license": "MIT",
|
|
@@ -21,45 +21,40 @@
|
|
|
21
21
|
],
|
|
22
22
|
"sideEffects": false,
|
|
23
23
|
"exports": {
|
|
24
|
-
"
|
|
25
|
-
"./
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
"./schema-builders/boolean": "./dist/schema-builders/boolean.js",
|
|
30
|
-
"./schema-builders/date": "./dist/schema-builders/date.js",
|
|
31
|
-
"./schema-builders/decimal128": "./dist/schema-builders/decimal128.js",
|
|
32
|
-
"./schema-builders/number": "./dist/schema-builders/number.js",
|
|
33
|
-
"./schema-builders/object-id": "./dist/schema-builders/object-id.js",
|
|
34
|
-
"./schema-builders/ref": "./dist/schema-builders/ref.js",
|
|
35
|
-
"./schema-builders/string": "./dist/schema-builders/string.js",
|
|
36
|
-
"./types": {
|
|
37
|
-
"types": "./dist/types/index.d.ts"
|
|
38
|
-
},
|
|
39
|
-
"./types/aggregate-paginate": {
|
|
40
|
-
"types": "./dist/types/aggregate-paginate.d.ts"
|
|
24
|
+
"./*": "./dist/*.js",
|
|
25
|
+
"./_internals": {
|
|
26
|
+
"types": null,
|
|
27
|
+
"import": null,
|
|
28
|
+
"require": null
|
|
41
29
|
},
|
|
42
|
-
"./
|
|
43
|
-
|
|
30
|
+
"./package.json": "./package.json",
|
|
31
|
+
"./schema-builders": {
|
|
32
|
+
"types": "./dist/schema-builders/index.d.ts",
|
|
33
|
+
"import": "./dist/schema-builders/index.js",
|
|
34
|
+
"require": null
|
|
44
35
|
},
|
|
45
|
-
"./
|
|
46
|
-
"types":
|
|
36
|
+
"./schema-builders/index": {
|
|
37
|
+
"types": null,
|
|
38
|
+
"import": null,
|
|
39
|
+
"require": null
|
|
47
40
|
},
|
|
48
|
-
"./types
|
|
49
|
-
"types": "./dist/types/
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
"types": "./dist/types/utils.d.ts"
|
|
41
|
+
"./types": {
|
|
42
|
+
"types": "./dist/types/index.d.ts",
|
|
43
|
+
"import": null,
|
|
44
|
+
"require": null
|
|
53
45
|
},
|
|
54
|
-
"./
|
|
55
|
-
|
|
46
|
+
"./types/index": {
|
|
47
|
+
"types": null,
|
|
48
|
+
"import": null,
|
|
49
|
+
"require": null
|
|
50
|
+
}
|
|
56
51
|
},
|
|
57
52
|
"files": [
|
|
58
53
|
"./dist",
|
|
59
54
|
"./src"
|
|
60
55
|
],
|
|
61
56
|
"engines": {
|
|
62
|
-
"node": ">=22.
|
|
57
|
+
"node": ">=22.12.0"
|
|
63
58
|
},
|
|
64
59
|
"scripts": {
|
|
65
60
|
"build": "tsdown",
|
package/src/_internals.ts
CHANGED
package/src/constants.ts
CHANGED
package/src/options.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { customMongooseOptions } from '
|
|
2
|
-
import type { CustomMongooseOptions } from '
|
|
3
|
-
import type { buildMongooseModel } from '
|
|
1
|
+
import { customMongooseOptions } from './_internals';
|
|
2
|
+
import type { CustomMongooseOptions } from './types/options';
|
|
3
|
+
import type { buildMongooseModel } from './utils';
|
|
4
4
|
|
|
5
5
|
export type DoNotRemoveOrUseThisType = typeof buildMongooseModel;
|
|
6
6
|
|
package/src/types/options.ts
CHANGED
|
@@ -4,8 +4,8 @@ import type {
|
|
|
4
4
|
SchemaTimestampsConfig,
|
|
5
5
|
} from 'mongoose';
|
|
6
6
|
|
|
7
|
-
import type { MongooseNormalizePluginOptions } from '
|
|
8
|
-
import type { buildMongooseModel } from '
|
|
7
|
+
import type { MongooseNormalizePluginOptions } from '../plugins/normalize';
|
|
8
|
+
import type { buildMongooseModel } from '../utils';
|
|
9
9
|
|
|
10
10
|
export type DoNotRemoveOrUseThisType = typeof buildMongooseModel;
|
|
11
11
|
|
|
@@ -34,8 +34,22 @@ export interface BuildMongooseModelOptions {
|
|
|
34
34
|
* @default true
|
|
35
35
|
*/
|
|
36
36
|
enableNormalizePlugin?: boolean;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Options for the normalize plugin.
|
|
40
|
+
*/
|
|
37
41
|
normalizePluginOptions?: MongooseNormalizePluginOptions;
|
|
38
42
|
|
|
43
|
+
/**
|
|
44
|
+
* Whether to recursively register the normalize plugin on child schemas.
|
|
45
|
+
*
|
|
46
|
+
* If `true`, the plugin will be automatically applied to all nested schemas.
|
|
47
|
+
* If `false`, only the root schema will be affected.
|
|
48
|
+
*
|
|
49
|
+
* @default true
|
|
50
|
+
*/
|
|
51
|
+
normalizePluginRecursive?: boolean;
|
|
52
|
+
|
|
39
53
|
/**
|
|
40
54
|
* Configures Mongoose timestamps.
|
|
41
55
|
*
|
package/src/utils.ts
CHANGED
|
@@ -6,14 +6,17 @@ import type {
|
|
|
6
6
|
import mongooseAggregatePaginate from 'mongoose-aggregate-paginate-v2';
|
|
7
7
|
import mongoosePaginate from 'mongoose-paginate-v2';
|
|
8
8
|
|
|
9
|
-
import { customMongooseOptions } from '
|
|
10
|
-
import { mongooseConnections } from '
|
|
11
|
-
import type { setCustomMongooseOptions } from '
|
|
12
|
-
import { mongooseNormalizePlugin } from '
|
|
13
|
-
import type {
|
|
9
|
+
import { customMongooseOptions } from './_internals';
|
|
10
|
+
import { mongooseConnections } from './constants';
|
|
11
|
+
import type { setCustomMongooseOptions } from './options';
|
|
12
|
+
import { mongooseNormalizePlugin } from './plugins/normalize';
|
|
13
|
+
import type { MongooseNormalizePluginOptions } from './plugins/normalize';
|
|
14
|
+
import type { BuildMongooseModelOptions } from './types/options';
|
|
14
15
|
|
|
15
16
|
export type DoNotRemoveOrUseThisType = typeof setCustomMongooseOptions;
|
|
16
17
|
|
|
18
|
+
// const normalizePluginRegisteredSymbol = Symbol('normalizePluginRegistered');
|
|
19
|
+
|
|
17
20
|
/**
|
|
18
21
|
* Asserts that a Mongoose update operation was successful by checking that it was acknowledged
|
|
19
22
|
* and that at least the expected number of documents were modified.
|
|
@@ -79,7 +82,7 @@ export function buildMongooseModel<
|
|
|
79
82
|
options?: BuildMongooseModelOptions,
|
|
80
83
|
): Model {
|
|
81
84
|
if (options?.enableNormalizePlugin !== false) {
|
|
82
|
-
schema
|
|
85
|
+
registerMongooseNormalizePlugin(schema, options?.normalizePluginRecursive, options?.normalizePluginOptions);
|
|
83
86
|
}
|
|
84
87
|
|
|
85
88
|
schema.plugin(mongooseAggregatePaginate);
|
|
@@ -125,6 +128,25 @@ export async function mongooseDocumentOrObjectIdToDocument<
|
|
|
125
128
|
return documentOrObjectId;
|
|
126
129
|
}
|
|
127
130
|
|
|
131
|
+
export function registerMongooseNormalizePlugin<
|
|
132
|
+
DocType,
|
|
133
|
+
Model extends BaseMongoosePaginateModel<DocType, InstanceMethodsAndOverrides, QueryHelpers>,
|
|
134
|
+
InstanceMethodsAndOverrides = object,
|
|
135
|
+
QueryHelpers = object,
|
|
136
|
+
>(
|
|
137
|
+
schema: Schema<DocType, Model, InstanceMethodsAndOverrides, QueryHelpers>,
|
|
138
|
+
recursive: boolean = true,
|
|
139
|
+
options?: MongooseNormalizePluginOptions,
|
|
140
|
+
) {
|
|
141
|
+
// TODO: Check if plugin is already registered, Do you really need to check?
|
|
142
|
+
// if ((schema as any)[normalizePluginRegisteredSymbol]) return;
|
|
143
|
+
// (schema as any)[normalizePluginRegisteredSymbol] = true;
|
|
144
|
+
schema.plugin(mongooseNormalizePlugin, options);
|
|
145
|
+
if (recursive) {
|
|
146
|
+
schema.childSchemas.forEach(({ schema }) => registerMongooseNormalizePlugin(schema, recursive, options));
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
128
150
|
/**
|
|
129
151
|
* Converts the input, which can be a MongoDB ObjectId or a string,
|
|
130
152
|
* into a 24-character hexadecimal string representation of the ObjectId.
|