@kikiutils/mongoose 5.1.4 → 5.1.6
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/plugins/normalize.js.map +1 -1
- package/dist/schema-builders/boolean.d.ts.map +1 -1
- package/dist/schema-builders/number.d.ts.map +1 -1
- package/dist/test.d.ts +1 -0
- package/dist/utils.d.ts +2 -2
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +1 -1
- package/dist/utils.js.map +1 -1
- package/package.json +29 -22
- package/src/_internals.ts +1 -1
- package/src/constants.ts +1 -1
- package/src/options.ts +3 -3
- package/src/plugins/normalize.ts +1 -0
- package/src/test.ts +45 -0
- package/src/types/options.ts +2 -2
- package/src/utils.ts +6 -6
- package/dist/types/data.js +0 -1
- package/dist/types/index.js +0 -1
- package/dist/types/options.js +0 -1
- package/dist/types/paginate.js +0 -1
- package/dist/types/utils.js +0 -1
- /package/dist/{types/aggregate-paginate.js → test.js} +0 -0
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":"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 convert `_id` field to `id`.\n * If true, the `_id` field will be replaced with `id`.\n *\n * @default true\n */\n convertIdField?: boolean;\n\n /**\n * Whether to convert the `_id` field to a hex string if it's an ObjectId.\n *\n * @default true\n */\n toHexIdIfObjectId?: boolean;\n}\n\n/**\n * Mongoose plugin to normalize the JSON output of documents.\n *\n * This plugin modifies the `toJSON` method of Mongoose schemas to:\n * - Add an `id` field equal to the string representation of `_id`\n * - Remove the `_id` field\n * - Exclude fields marked as `private` from the JSON output\n * - Convert `Decimal128` fields to strings\n * - Remove the `__v` version key\n *\n * @template S - The type of the schema.\n *\n * @param schema - The Mongoose schema to apply the plugin to.\n */\nexport function mongooseNormalizePlugin<S extends Schema>(\n schema: S,\n pluginOptions?: MongooseNormalizePluginOptions,\n) {\n const toJson = schema.get('toJSON');\n const toJsonTransform = toJson?.transform;\n schema.set(\n 'toJSON',\n {\n ...toJson,\n transform(doc, ret, options) {\n // eslint-disable-next-line style/object-curly-newline\n let { __v, _id, ...copiedRet } = ret;\n if (pluginOptions?.toHexIdIfObjectId !== false) {\n _id = _id instanceof Types.ObjectId ? _id.toHexString() : _id;\n }\n\n if (_id !== undefined) {\n if (pluginOptions?.convertIdField !== false) copiedRet.id = _id;\n else copiedRet._id = _id;\n }\n\n for (const path in schema.paths) {\n if (schema.paths[path]?.options?.private) {\n unsetProp(copiedRet, path);\n continue;\n }\n\n if (schema.paths[path]?.instance === 'Decimal128') {\n const value = getProp(copiedRet, path) as Types.Decimal128 | undefined;\n if (value) setProp(copiedRet, path, value.toString());\n }\n }\n\n if (toJsonTransform && typeof toJsonTransform !== 'boolean') {\n return toJsonTransform(doc as any, copiedRet as any, options as any);\n }\n\n return copiedRet;\n },\n },\n );\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAuCA,SAAgB,wBACZ,QACA,eACF;CACE,MAAM,SAAS,OAAO,IAAI,SAAS;CACnC,MAAM,kBAAkB,QAAQ;AAChC,QAAO,IACH,UACA;EACI,GAAG;EACH,UAAU,KAAK,KAAK,SAAS;GAEzB,IAAI,EAAE,KAAK,IAAK,GAAG,cAAc;AACjC,OAAI,eAAe,sBAAsB,MACrC,OAAM,eAAe,MAAM,WAAW,IAAI,aAAa,GAAG;AAG9D,OAAI,QAAQ,OACR,KAAI,eAAe,mBAAmB,MAAO,WAAU,KAAK;OACvD,WAAU,MAAM;
|
|
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 convert `_id` field to `id`.\n * If true, the `_id` field will be replaced with `id`.\n *\n * @default true\n */\n convertIdField?: boolean;\n\n /**\n * Whether to convert the `_id` field to a hex string if it's an ObjectId.\n *\n * @default true\n */\n toHexIdIfObjectId?: boolean;\n}\n\n/**\n * Mongoose plugin to normalize the JSON output of documents.\n *\n * This plugin modifies the `toJSON` method of Mongoose schemas to:\n * - Add an `id` field equal to the string representation of `_id`\n * - Remove the `_id` field\n * - Exclude fields marked as `private` from the JSON output\n * - Convert `Decimal128` fields to strings\n * - Remove the `__v` version key\n *\n * @template S - The type of the schema.\n *\n * @param schema - The Mongoose schema to apply the plugin to.\n */\nexport function mongooseNormalizePlugin<S extends Schema>(\n schema: S,\n pluginOptions?: MongooseNormalizePluginOptions,\n) {\n const toJson = schema.get('toJSON');\n const toJsonTransform = toJson?.transform;\n schema.set(\n 'toJSON',\n {\n ...toJson,\n transform(doc, ret, options) {\n // eslint-disable-next-line style/object-curly-newline\n let { __v, _id, ...copiedRet } = ret;\n if (pluginOptions?.toHexIdIfObjectId !== false) {\n _id = _id instanceof Types.ObjectId ? _id.toHexString() : _id;\n }\n\n if (_id !== undefined) {\n if (pluginOptions?.convertIdField !== false) copiedRet.id = _id;\n else copiedRet._id = _id;\n }\n\n // console.log(schema.childSchemas[0]?.schema.childSchemas);\n for (const path in schema.paths) {\n if (schema.paths[path]?.options?.private) {\n unsetProp(copiedRet, path);\n continue;\n }\n\n if (schema.paths[path]?.instance === 'Decimal128') {\n const value = getProp(copiedRet, path) as Types.Decimal128 | undefined;\n if (value) setProp(copiedRet, path, value.toString());\n }\n }\n\n if (toJsonTransform && typeof toJsonTransform !== 'boolean') {\n return toJsonTransform(doc as any, copiedRet as any, options as any);\n }\n\n return copiedRet;\n },\n },\n );\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAuCA,SAAgB,wBACZ,QACA,eACF;CACE,MAAM,SAAS,OAAO,IAAI,SAAS;CACnC,MAAM,kBAAkB,QAAQ;AAChC,QAAO,IACH,UACA;EACI,GAAG;EACH,UAAU,KAAK,KAAK,SAAS;GAEzB,IAAI,EAAE,KAAK,IAAK,GAAG,cAAc;AACjC,OAAI,eAAe,sBAAsB,MACrC,OAAM,eAAe,MAAM,WAAW,IAAI,aAAa,GAAG;AAG9D,OAAI,QAAQ,OACR,KAAI,eAAe,mBAAmB,MAAO,WAAU,KAAK;OACvD,WAAU,MAAM;AAIzB,QAAK,MAAM,QAAQ,OAAO,OAAO;AAC7B,QAAI,OAAO,MAAM,OAAO,SAAS,SAAS;AACtC,WAAU,WAAW,KAAK;AAC1B;;AAGJ,QAAI,OAAO,MAAM,OAAO,aAAa,cAAc;KAC/C,MAAM,QAAQA,IAAQ,WAAW,KAAK;AACtC,SAAI,MAAO,KAAQ,WAAW,MAAM,MAAM,UAAU,CAAC;;;AAI7D,OAAI,mBAAmB,OAAO,oBAAoB,UAC9C,QAAO,gBAAgB,KAAY,WAAkB,QAAe;AAGxE,UAAO;;EAEd,CACJ"}
|
|
@@ -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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MAAA,EAAA,IAAA;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/test.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|
package/dist/utils.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { BuildMongooseModelOptions } from "./types/options.js";
|
|
2
1
|
import { setCustomMongooseOptions } from "./options.js";
|
|
2
|
+
import { BuildMongooseModelOptions } from "./types/options.js";
|
|
3
3
|
import { Schema, Types, mongo } from "mongoose";
|
|
4
4
|
|
|
5
5
|
//#region src/utils.d.ts
|
|
@@ -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):
|
|
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
|
*
|
package/dist/utils.d.ts.map
CHANGED
|
@@ -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
|
|
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
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";
|
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":[],"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
|
+
"version": "5.1.6",
|
|
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,33 +21,40 @@
|
|
|
21
21
|
],
|
|
22
22
|
"sideEffects": false,
|
|
23
23
|
"exports": {
|
|
24
|
-
"
|
|
25
|
-
"./
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
"./
|
|
31
|
-
"./schema-builders
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
"./
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
"./types
|
|
42
|
-
|
|
43
|
-
|
|
24
|
+
"./*": "./dist/*.js",
|
|
25
|
+
"./_internals": {
|
|
26
|
+
"types": null,
|
|
27
|
+
"import": null,
|
|
28
|
+
"require": null
|
|
29
|
+
},
|
|
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
|
|
35
|
+
},
|
|
36
|
+
"./schema-builders/index": {
|
|
37
|
+
"types": null,
|
|
38
|
+
"import": null,
|
|
39
|
+
"require": null
|
|
40
|
+
},
|
|
41
|
+
"./types": {
|
|
42
|
+
"types": "./dist/types/index.d.ts",
|
|
43
|
+
"import": null,
|
|
44
|
+
"require": null
|
|
45
|
+
},
|
|
46
|
+
"./types/index": {
|
|
47
|
+
"types": null,
|
|
48
|
+
"import": null,
|
|
49
|
+
"require": null
|
|
50
|
+
}
|
|
44
51
|
},
|
|
45
52
|
"files": [
|
|
46
53
|
"./dist",
|
|
47
54
|
"./src"
|
|
48
55
|
],
|
|
49
56
|
"engines": {
|
|
50
|
-
"node": ">=22.
|
|
57
|
+
"node": ">=22.12.0"
|
|
51
58
|
},
|
|
52
59
|
"scripts": {
|
|
53
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/plugins/normalize.ts
CHANGED
|
@@ -59,6 +59,7 @@ export function mongooseNormalizePlugin<S extends Schema>(
|
|
|
59
59
|
else copiedRet._id = _id;
|
|
60
60
|
}
|
|
61
61
|
|
|
62
|
+
// console.log(schema.childSchemas[0]?.schema.childSchemas);
|
|
62
63
|
for (const path in schema.paths) {
|
|
63
64
|
if (schema.paths[path]?.options?.private) {
|
|
64
65
|
unsetProp(copiedRet, path);
|
package/src/test.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// // export MONGODB_URI='mongodb://192.168.88.10:27017/mongoose-test?directConnection=true&replicaSet=rs0'
|
|
2
|
+
|
|
3
|
+
// import { Schema } from 'mongoose';
|
|
4
|
+
|
|
5
|
+
// import * as s from './schema-builders';
|
|
6
|
+
// import { buildMongooseModel } from './utils';
|
|
7
|
+
|
|
8
|
+
// type A = BaseMongooseDocType<_A>;
|
|
9
|
+
// type AModel = BaseMongoosePaginateModel<A>;
|
|
10
|
+
// type B = MongooseHydratedDocument<A>;
|
|
11
|
+
// interface _A extends BaseMongooseModelData {
|
|
12
|
+
// z: {
|
|
13
|
+
// password: string;
|
|
14
|
+
// r?: { password: string };
|
|
15
|
+
// };
|
|
16
|
+
// }
|
|
17
|
+
|
|
18
|
+
// const schema = new Schema<A, AModel>({
|
|
19
|
+
// z: {
|
|
20
|
+
// _id: false,
|
|
21
|
+
// required: true,
|
|
22
|
+
// type: {
|
|
23
|
+
// password: s.string().required,
|
|
24
|
+
// r: {
|
|
25
|
+
// _id: false,
|
|
26
|
+
// private: true,
|
|
27
|
+
// required: true,
|
|
28
|
+
// type: { password: s.string().required },
|
|
29
|
+
// },
|
|
30
|
+
// },
|
|
31
|
+
// },
|
|
32
|
+
// });
|
|
33
|
+
|
|
34
|
+
// const AModel = buildMongooseModel<A, AModel>('test.a', 'A', schema);
|
|
35
|
+
|
|
36
|
+
// await AModel.deleteMany();
|
|
37
|
+
// await AModel.create({
|
|
38
|
+
// z: {
|
|
39
|
+
// password: 'awsedasds',
|
|
40
|
+
// r: { password: 'awsedasds' },
|
|
41
|
+
// },
|
|
42
|
+
// });
|
|
43
|
+
|
|
44
|
+
// const a = await AModel.findOne();
|
|
45
|
+
// console.log(a?.toJSON());
|
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
|
|
package/src/utils.ts
CHANGED
|
@@ -6,11 +6,11 @@ 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 { BuildMongooseModelOptions } from '
|
|
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 { BuildMongooseModelOptions } from './types/options';
|
|
14
14
|
|
|
15
15
|
export type DoNotRemoveOrUseThisType = typeof setCustomMongooseOptions;
|
|
16
16
|
|
|
@@ -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
|
}
|
package/dist/types/data.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { };
|
package/dist/types/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { };
|
package/dist/types/options.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { };
|
package/dist/types/paginate.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { };
|
package/dist/types/utils.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { };
|
|
File without changes
|