@kikiutils/mongoose 5.1.4 → 5.1.5

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.
@@ -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,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,IA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AAA;MA8ByC,eA9B9D,CAAA;QACA,EA8BI,yBA9BJ,CA8B8B,KA9B9B,CA8BoC,KA9BpC,EAAA;IAFW,MAAA,EAAA,IAAA;MAgCmD,eA3BtC,CAAA;;AAAsC,cA8BzD,mBA9ByD,EAAA,CAAA,MAAA,CAAA,EA8BtC,MA9BsC,CAAA,MAAA,EAAA,GAAA,CAAA,EAAA,GA8BtC,mBA9BsC,CAAA;MACxD,EAxB+C,sBAwB/C;QAAgB,CAAA"}
package/dist/utils.d.ts CHANGED
@@ -46,7 +46,7 @@ declare function assertMongooseUpdateSuccess<T extends mongo.BSON.Document>(upda
46
46
  *
47
47
  * @returns The created Mongoose model.
48
48
  */
49
- 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): any;
49
+ 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;
50
50
  /**
51
51
  * Converts a document or ObjectId to a document using the specified model.
52
52
  *
@@ -1 +1 @@
1
- {"version":3,"file":"utils.d.ts","names":[],"sources":["../src/utils.ts"],"sourcesContent":[],"mappings":";;;;;KAcY,wBAAA,UAAkC;AAA9C;AAgBA;;;;;;;;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;;;;;;;;;;;;;;;AAyBA;;;;;iBAlEgB,0CAEE,0BAA0B,SAAS,6BAA6B,sHAMtE,OAAO,SAAS,OAAO,6BAA6B,yBAClD;;;;;;;;;;;;;;;;;;iBAgCQ,+CACR,yBAAyB,SAAS,6BAA6B,uFAKrD,2BAA2B,WACxC,0BAA0B,SAAS,6BAA6B,yCAChD,QAAA;;;;;;;;;iBAiBX,mBAAA,cAAiC,KAAA,CAAM"}
1
+ {"version":3,"file":"utils.d.ts","names":[],"sources":["../src/utils.ts"],"sourcesContent":[],"mappings":";;;;;KAcY,wBAAA,UAAkC;AAA9C;AAgBA;;;;;;;;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;;;;;;;;;;;;;;;AAyBA;;;;iBAlEgB,0CAEE,0BAA0B,SAAS,6BAA6B,sHAMtE,OAAO,SAAS,OAAO,6BAA6B,yBAClD,4BACX;;;;;;;;;;;;;;;;;;iBA+BmB,+CACR,yBAAyB,SAAS,6BAA6B,uFAKrD,2BAA2B,WACxC,0BAA0B,SAAS,6BAA6B,yCAChD,QAAA;;;;;;;;;iBAiBX,mBAAA,cAAiC,KAAA,CAAM"}
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 '@/_internals';\nimport { mongooseConnections } from '@/constants';\nimport type { setCustomMongooseOptions } from '@/options';\nimport { mongooseNormalizePlugin } from '@/plugins/normalize';\nimport type { BuildMongooseModelOptions } from '@/types/options';\n\nexport type DoNotRemoveOrUseThisType = typeof setCustomMongooseOptions;\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) {\n if (options?.enableNormalizePlugin !== false) {\n schema.plugin(mongooseNormalizePlugin, 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\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":";;;;;;;;;;;;;;;;;;;;;;AA8BA,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,SACF;AACE,KAAI,SAAS,0BAA0B,MACnC,QAAO,OAAO,yBAAyB,SAAS,uBAAuB;AAG3E,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;;;;;;;;;;AAWX,SAAgB,oBAAoB,IAA6B;AAC7D,QAAO,cAAc,MAAM,WAAW,GAAG,aAAa,GAAG"}
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 '@/_internals';\nimport { mongooseConnections } from '@/constants';\nimport type { setCustomMongooseOptions } from '@/options';\nimport { mongooseNormalizePlugin } from '@/plugins/normalize';\nimport type { BuildMongooseModelOptions } from '@/types/options';\n\nexport type DoNotRemoveOrUseThisType = typeof setCustomMongooseOptions;\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 schema.plugin(mongooseNormalizePlugin, 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\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":";;;;;;;;;;;;;;;;;;;;;;AA8BA,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,QAAO,OAAO,yBAAyB,SAAS,uBAAuB;AAG3E,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;;;;;;;;;;AAWX,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.1.4",
4
+ "version": "5.1.5",
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",
@@ -33,12 +33,24 @@
33
33
  "./schema-builders/object-id": "./dist/schema-builders/object-id.js",
34
34
  "./schema-builders/ref": "./dist/schema-builders/ref.js",
35
35
  "./schema-builders/string": "./dist/schema-builders/string.js",
36
- "./types": "./dist/types/index.js",
37
- "./types/aggregate-paginate": "./dist/types/aggregate-paginate.js",
38
- "./types/data": "./dist/types/data.js",
39
- "./types/options": "./dist/types/options.js",
40
- "./types/paginate": "./dist/types/paginate.js",
41
- "./types/utils": "./dist/types/utils.js",
36
+ "./types": {
37
+ "types": "./dist/types/index.d.ts"
38
+ },
39
+ "./types/aggregate-paginate": {
40
+ "types": "./dist/types/aggregate-paginate.d.ts"
41
+ },
42
+ "./types/data": {
43
+ "types": "./dist/types/data.d.ts"
44
+ },
45
+ "./types/options": {
46
+ "types": "./dist/types/options.d.ts"
47
+ },
48
+ "./types/paginate": {
49
+ "types": "./dist/types/paginate.d.ts"
50
+ },
51
+ "./types/utils": {
52
+ "types": "./dist/types/utils.d.ts"
53
+ },
42
54
  "./utils": "./dist/utils.js",
43
55
  "./package.json": "./package.json"
44
56
  },
package/src/utils.ts CHANGED
@@ -77,7 +77,7 @@ export function buildMongooseModel<
77
77
  name: string,
78
78
  schema: Schema<DocType, Model, InstanceMethodsAndOverrides, QueryHelpers>,
79
79
  options?: BuildMongooseModelOptions,
80
- ) {
80
+ ): Model {
81
81
  if (options?.enableNormalizePlugin !== false) {
82
82
  schema.plugin(mongooseNormalizePlugin, options?.normalizePluginOptions);
83
83
  }
@@ -1 +0,0 @@
1
- export { };
@@ -1 +0,0 @@
1
- export { };
@@ -1 +0,0 @@
1
- export { };
@@ -1 +0,0 @@
1
- export { };
@@ -1 +0,0 @@
1
- export { };
@@ -1 +0,0 @@
1
- export { };