@kikiutils/mongoose 3.2.0 → 4.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 +1 -1
- package/dist/types/data.d.ts +8 -6
- package/dist/types/data.d.ts.map +1 -1
- package/dist/types/index.d.ts +1 -1
- package/dist/utils.cjs +12 -0
- package/dist/utils.cjs.map +1 -1
- package/dist/utils.d.ts +10 -0
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.mjs +12 -1
- package/dist/utils.mjs.map +1 -1
- package/package.json +6 -6
- package/src/types/data.ts +21 -7
- package/src/types/index.ts +1 -1
- package/src/utils.ts +12 -0
package/README.md
CHANGED
package/dist/types/data.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { SetFieldType } from 'type-fest';
|
|
1
2
|
import type { IfElse } from './utils';
|
|
2
3
|
declare global {
|
|
3
4
|
/**
|
|
@@ -13,22 +14,23 @@ declare global {
|
|
|
13
14
|
}, unknown> & {
|
|
14
15
|
id: string;
|
|
15
16
|
};
|
|
17
|
+
type DataToBaseMongooseDocType<T, ObjectIdFields extends keyof T = never, DateFields extends Exclude<keyof T, ObjectIdFields> = never, CreatedAtField extends boolean = true, UpdatedAtField extends boolean = true> = BaseMongooseDocType<SetFieldsToObjectId<SetFieldType<T, DateFields, Date>, ObjectIdFields>, CreatedAtField, UpdatedAtField>;
|
|
16
18
|
/**
|
|
17
19
|
* Type definition to omit timestamp fields (`createdAt`, `updatedAt`)
|
|
18
|
-
* and other specified fields (`
|
|
20
|
+
* and other specified fields (`K`) from a given type `T`.
|
|
19
21
|
*
|
|
20
22
|
* @template T - The type from which to omit fields.
|
|
21
|
-
* @template
|
|
23
|
+
* @template K - Additional fields to omit, specified as a union of keys in `T`.
|
|
22
24
|
*/
|
|
23
|
-
type ExceptMongooseTimestampAndOtherFields<T,
|
|
25
|
+
type ExceptMongooseTimestampAndOtherFields<T, K extends keyof T> = Omit<T, 'createdAt' | 'updatedAt' | K>;
|
|
24
26
|
/**
|
|
25
27
|
* Type definition to omit timestamp fields (`createdAt`, `updatedAt`)
|
|
26
|
-
* and other specified fields (`
|
|
28
|
+
* and other specified fields (`K`) from a given type `T`.
|
|
27
29
|
*
|
|
28
30
|
* @template T - The type from which to omit fields.
|
|
29
|
-
* @template
|
|
31
|
+
* @template K - Additional fields to omit, specified as a union of string, number, or symbol keys.
|
|
30
32
|
*/
|
|
31
|
-
type OmitMongooseTimestampAndOtherFields<T,
|
|
33
|
+
type OmitMongooseTimestampAndOtherFields<T, K extends number | string | symbol = never> = Omit<T, 'createdAt' | 'updatedAt' | K>;
|
|
32
34
|
/**
|
|
33
35
|
* Type definition for including timestamp fields conditionally based on the provided flags.
|
|
34
36
|
*
|
package/dist/types/data.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"data.d.ts","sourceRoot":"","sources":["../../src/types/data.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAEtC,OAAO,CAAC,MAAM,CAAC;IACX;;;;;OAKG;IACH,KAAK,qBAAqB,CACtB,cAAc,SAAS,OAAO,GAAG,IAAI,EACrC,cAAc,SAAS,OAAO,GAAG,IAAI,IAErC,MAAM,CAAC,cAAc,EAAE;QAAE,SAAS,EAAE,MAAM,CAAA;KAAE,EAAE,OAAO,CAAC,GACtD,MAAM,CAAC,cAAc,EAAE;QAAE,SAAS,EAAE,MAAM,CAAA;KAAE,EAAE,OAAO,CAAC,GACtD;QAAE,EAAE,EAAE,MAAM,CAAA;KAAE,CAAC;IAEnB;;;;;;OAMG;IACH,KAAK,qCAAqC,CAAC,CAAC,EAAE,
|
|
1
|
+
{"version":3,"file":"data.d.ts","sourceRoot":"","sources":["../../src/types/data.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AAE9C,OAAO,KAAK,EAAE,MAAM,EAAE,M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KAAE,EAAE,MAAM,CAAC,GAChD,MAAM,CAAC,SAAS,EAAE;QAAE,SAAS,EAAE,MAAM,CAAA;KAAE,EAAE,MAAM,CAAC,CAAC;CACxD"}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -75,6 +75,6 @@ declare global {
|
|
|
75
75
|
* @template QueryHelpers - Optional type parameter for additional query helper methods.
|
|
76
76
|
*/
|
|
77
77
|
type MongooseHydratedDocument<DocType, InstanceMethodsAndOverrides = object, QueryHelpers = object> = HydratedDocument<DocType, InstanceMethodsAndOverrides, QueryHelpers>;
|
|
78
|
-
type SetFieldsToObjectId<T,
|
|
78
|
+
type SetFieldsToObjectId<T, K extends keyof T> = SetFieldType<T, K, Types.ObjectId>;
|
|
79
79
|
}
|
|
80
80
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/utils.cjs
CHANGED
|
@@ -98,8 +98,20 @@ async function mongooseDocumentOrObjectIdToDocument(documentOrObjectId, model, s
|
|
|
98
98
|
}
|
|
99
99
|
return documentOrObjectId;
|
|
100
100
|
}
|
|
101
|
+
/**
|
|
102
|
+
* Converts the input, which can be a MongoDB ObjectId or a string,
|
|
103
|
+
* into a 24-character hexadecimal string representation of the ObjectId.
|
|
104
|
+
*
|
|
105
|
+
* @param {string | Types.ObjectId} id - The ObjectId instance or its string representation
|
|
106
|
+
*
|
|
107
|
+
* @returns {string} The hexadecimal string representation of the ObjectId
|
|
108
|
+
*/
|
|
109
|
+
function toObjectIdHexString(id) {
|
|
110
|
+
return id instanceof mongoose.Types.ObjectId ? id.toHexString() : id;
|
|
111
|
+
}
|
|
101
112
|
|
|
102
113
|
exports.assertMongooseUpdateSuccess = assertMongooseUpdateSuccess;
|
|
103
114
|
exports.buildMongooseModel = buildMongooseModel;
|
|
104
115
|
exports.mongooseDocumentOrObjectIdToDocument = mongooseDocumentOrObjectIdToDocument;
|
|
116
|
+
exports.toObjectIdHexString = toObjectIdHexString;
|
|
105
117
|
//# sourceMappingURL=utils.cjs.map
|
package/dist/utils.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.cjs","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"],"names":["mongooseNormalizePlugin","mongooseAggregatePaginate","mongoosePaginate","customMongooseOptions","mongooseConnections","mongoose","Types"],"mappings":";;;;;;;;;;;;;;;AAgBA;;;;;;;;;;;;;AAaG;AACI,eAAe,2BAA2B,CAC7C,aAA6C,EAC7C,qBAAqB,GAAG,CAAC,EAAA;AAEzB,IAAA,MAAM,YAAY,GAAG,MAAM,aAAa;IACxC,IAAI,CAAC,YAAY,CAAC,YAAY;AAAE,QAAA,MAAM,IAAI,KAAK,CAAC,6BAA6B,CAAC;AAC9E,IAAA,IAAI,YAAY,CAAC,aAAa,GAAG,qBAAqB,EAAE;;QAEpD,MAAM,IAAI,KAAK,CAAC,CAAA,4BAAA,EAA+B,qBAAqB,CAAA,2BAAA,EAA8B,YAAY,CAAC,aAAa,CAAA,CAAE,CAAC;IACnI;AACJ;AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;AA0BG;AACG,SAAU,kBAAkB,CAM9B,UAAkB,EAClB,IAAY,EACZ,MAAyE,EACzE,OAAmC,EAAA;AAEnC,IAAA,IAAI,OAAO,EAAE,qBAAqB,KAAK,KAAK,EAAE;QAC1C,MAAM,CAAC,MAAM,CAACA,iCAAuB,EAAE,OAAO,EAAE,sBAAsB,CAAC;IAC3E;AAEA,IAAA,MAAM,CAAC,MAAM,CAACC,kCAAyB,CAAC;AACxC,IAAA,MAAM,CAAC,MAAM,CAACC,yBAAgB,CAAC;AAC/B,IAAA,MAAM,CAAC,GAAG,CAAC,YAAY,EAAE,OAAO,EAAE,UAAU,IAAI,MAAM,CAAC,GAAG,CAAC,YAAY,CAAC,IAAI,IAAI,CAAC;AACjF,IAAAC,gCAAqB,CAAC,gBAAgB,GAAG,MAAM,CAAC;;IAEhD,MAAM,UAAU,GAAG,OAAO,EAAE,UAAU,KAAKC,6BAAmB,CAAC,OAAO,KAAKC,iBAAQ,CAAC,gBAAgB,CAAC,OAAO,CAAC,GAAG,CAAC,WAAW,IAAI,2BAA2B,CAAC,CAAC;IAC7J,OAAO,UAAU,CAAC,KAAK,CAA+B,IAAI,EAAE,MAAM,EAAE,UAAU,CAAC;AACnF;AAEA;;;;;;;;;;;;;;;;AAgBG;AACI,eAAe,oCAAoC,CAMtD,kBAAiD,EACjD,KAAoF,EACpF,YAAuB,EAAA;IAEvB,IAAI,OAAO,kBAAkB,KAAK,QAAQ,IAAI,kBAAkB,YAAYC,cAAK,CAAC,QAAQ,EAAE;AACxF,QAAA,QAAQ,MAAM,KAAK,CAAC,QAAQ,CAAC,kBAAkB,CAAC,CAAC,MAAM,CAAC,YAAY,IAAI,EAAE,CAAC;IAC/E;AAEA,IAAA,OAAO,kBAAkB;AAC7B
|
|
1
|
+
{"version":3,"file":"utils.cjs","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"],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O,UAAU,CAAC,KAAK,CAA+B,IAAI,EAAE,MAAM,EAAE,UAAU,CAAC;AACnF;AAEA;;;;;;;;;;;;;;;;AAgBG;AACI,eAAe,oCAAoC,CAMtD,kBAAiD,EACjD,KAAoF,EACpF,YAAuB,EAAA;IAEvB,IAAI,OAAO,kBAAkB,KAAK,QAAQ,IAAI,kBAAkB,YAAYC,cAAK,CAAC,QAAQ,EAAE;AACxF,QAAA,QAAQ,MAAM,KAAK,CAAC,QAAQ,CAAC,kBAAkB,CAAC,CAAC,MAAM,CAAC,YAAY,IAAI,EAAE,CAAC;IAC/E;AAEA,IAAA,OAAO,kBAAkB;AAC7B;AAEA;;;;;;;AAOG;AACG,SAAU,mBAAmB,CAAC,EAA2B,EAAA;AAC3D,IAAA,OAAO,EAAE,YAAYA,cAAK,CAAC,QAAQ,GAAG,EAAE,CAAC,WAAW,EAAE,GAAG,EAAE;AAC/D;;;;;;;"}
|
package/dist/utils.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Types } from 'mongoose';
|
|
1
2
|
import type { mongo, Schema } from 'mongoose';
|
|
2
3
|
import type { setCustomMongooseOptions } from './options';
|
|
3
4
|
import type { BuildMongooseModelOptions } from './types/options';
|
|
@@ -63,4 +64,13 @@ export declare function buildMongooseModel<DocType, Model extends BaseMongoosePa
|
|
|
63
64
|
* @returns The corresponding document or null if not found.
|
|
64
65
|
*/
|
|
65
66
|
export 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>;
|
|
67
|
+
/**
|
|
68
|
+
* Converts the input, which can be a MongoDB ObjectId or a string,
|
|
69
|
+
* into a 24-character hexadecimal string representation of the ObjectId.
|
|
70
|
+
*
|
|
71
|
+
* @param {string | Types.ObjectId} id - The ObjectId instance or its string representation
|
|
72
|
+
*
|
|
73
|
+
* @returns {string} The hexadecimal string representation of the ObjectId
|
|
74
|
+
*/
|
|
75
|
+
export declare function toObjectIdHexString(id: string | Types.ObjectId): string;
|
|
66
76
|
//# sourceMappingURL=utils.d.ts.map
|
package/dist/utils.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAiB,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC;AAC3C,OAAO,KAAK,EACR,KAAK,EACL,MAAM,EACT,MAAM,UAAU,CAAC;AAMlB,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,WAAW,CAAC;AAE1D,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,iBAAiB,CAAC;AAEjE,MAAM,MAAM,wBAAwB,GAAG,OAAO,wBAAwB,CAAC;AAEvE;;;;;;;;;;;;;GAaG;AACH,wBAAsB,2BAA2B,CAAC,CAAC,SAAS,KAAK,CAAC,IAAI,CAAC,QAAQ,EAC3E,aAAa,EAAE,OAAO,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,EAC7C,qBAAqB,SAAI,iBAQ5B;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAgB,kBAAkB,CAC9B,OAAO,EACP,KAAK,SAAS,yBAAyB,CAAC,OAAO,EAAE,2BAA2B,EAAE,YAAY,CAAC,EAC3F,2BAA2B,GAAG,MAAM,EACpC,YAAY,GAAG,MAAM,EAErB,UAAU,EAAE,MAAM,EAClB,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,MAAM,CAAC,OAAO,EAAE,KAAK,EAAE,2BAA2B,EAAE,YAAY,CAAC,EACzE,OAAO,CAAC,EAAE,yBAAyB,SAatC;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAsB,oCAAoC,CACtD,CAAC,SAAS,wBAAwB,CAAC,OAAO,EAAE,2BAA2B,EAAE,YAAY,CAAC,EACtF,OAAO,EACP,2BAA2B,EAC3B,YAAY,EAEZ,kBAAkB,EAAE,0BAA0B,CAAC,CAAC,CAAC,EACjD,KAAK,EAAE,yBAAyB,CAAC,OAAO,EAAE,2BAA2B,EAAE,YAAY,CAAC,EACpF,YAAY,CAAC,EAAE,MAAM,EAAE,qBAO1B;AAED;;;;;;;GAOG;AACH,wBAAgB,mBAAmB,CAAC,EAAE,EAAE,MAAM,GAAG,KAAK,CAAC,QAAQ,UAE9D"}
|
package/dist/utils.mjs
CHANGED
|
@@ -90,6 +90,17 @@ async function mongooseDocumentOrObjectIdToDocument(documentOrObjectId, model, s
|
|
|
90
90
|
}
|
|
91
91
|
return documentOrObjectId;
|
|
92
92
|
}
|
|
93
|
+
/**
|
|
94
|
+
* Converts the input, which can be a MongoDB ObjectId or a string,
|
|
95
|
+
* into a 24-character hexadecimal string representation of the ObjectId.
|
|
96
|
+
*
|
|
97
|
+
* @param {string | Types.ObjectId} id - The ObjectId instance or its string representation
|
|
98
|
+
*
|
|
99
|
+
* @returns {string} The hexadecimal string representation of the ObjectId
|
|
100
|
+
*/
|
|
101
|
+
function toObjectIdHexString(id) {
|
|
102
|
+
return id instanceof Types.ObjectId ? id.toHexString() : id;
|
|
103
|
+
}
|
|
93
104
|
|
|
94
|
-
export { assertMongooseUpdateSuccess, buildMongooseModel, mongooseDocumentOrObjectIdToDocument };
|
|
105
|
+
export { assertMongooseUpdateSuccess, buildMongooseModel, mongooseDocumentOrObjectIdToDocument, toObjectIdHexString };
|
|
95
106
|
//# sourceMappingURL=utils.mjs.map
|
package/dist/utils.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.mjs","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"],"names":[],"mappings":";;;;;;;AAgBA;;;;;;;;;;;;;AAaG;AACI,eAAe,2BAA2B,CAC7C,aAA6C,EAC7C,qBAAqB,GAAG,CAAC,EAAA;AAEzB,IAAA,MAAM,YAAY,GAAG,MAAM,aAAa;IACxC,IAAI,CAAC,YAAY,CAAC,YAAY;AAAE,QAAA,MAAM,IAAI,KAAK,CAAC,6BAA6B,CAAC;AAC9E,IAAA,IAAI,YAAY,CAAC,aAAa,GAAG,qBAAqB,EAAE;;QAEpD,MAAM,IAAI,KAAK,CAAC,CAAA,4BAAA,EAA+B,qBAAqB,CAAA,2BAAA,EAA8B,YAAY,CAAC,aAAa,CAAA,CAAE,CAAC;IACnI;AACJ;AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;AA0BG;AACG,SAAU,kBAAkB,CAM9B,UAAkB,EAClB,IAAY,EACZ,MAAyE,EACzE,OAAmC,EAAA;AAEnC,IAAA,IAAI,OAAO,EAAE,qBAAqB,KAAK,KAAK,EAAE;QAC1C,MAAM,CAAC,MAAM,CAAC,uBAAuB,EAAE,OAAO,EAAE,sBAAsB,CAAC;IAC3E;AAEA,IAAA,MAAM,CAAC,MAAM,CAAC,yBAAyB,CAAC;AACxC,IAAA,MAAM,CAAC,MAAM,CAAC,gBAAgB,CAAC;AAC/B,IAAA,MAAM,CAAC,GAAG,CAAC,YAAY,EAAE,OAAO,EAAE,UAAU,IAAI,MAAM,CAAC,GAAG,CAAC,YAAY,CAAC,IAAI,IAAI,CAAC;AACjF,IAAA,qBAAqB,CAAC,gBAAgB,GAAG,MAAM,CAAC;;IAEhD,MAAM,UAAU,GAAG,OAAO,EAAE,UAAU,KAAK,mBAAmB,CAAC,OAAO,KAAK,QAAQ,CAAC,gBAAgB,CAAC,OAAO,CAAC,GAAG,CAAC,WAAW,IAAI,2BAA2B,CAAC,CAAC;IAC7J,OAAO,UAAU,CAAC,KAAK,CAA+B,IAAI,EAAE,MAAM,EAAE,UAAU,CAAC;AACnF;AAEA;;;;;;;;;;;;;;;;AAgBG;AACI,eAAe,oCAAoC,CAMtD,kBAAiD,EACjD,KAAoF,EACpF,YAAuB,EAAA;IAEvB,IAAI,OAAO,kBAAkB,KAAK,QAAQ,IAAI,kBAAkB,YAAY,KAAK,CAAC,QAAQ,EAAE;AACxF,QAAA,QAAQ,MAAM,KAAK,CAAC,QAAQ,CAAC,kBAAkB,CAAC,CAAC,MAAM,CAAC,YAAY,IAAI,EAAE,CAAC;IAC/E;AAEA,IAAA,OAAO,kBAAkB;AAC7B;;;;"}
|
|
1
|
+
{"version":3,"file":"utils.mjs","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"],"names":[],"mappings":";;;;;;;AAgBA;;;;;;;;;;;;;AAaG;AACI,eAAe,2BAA2B,CAC7C,aAA6C,EAC7C,qBAAqB,GAAG,CAAC,EAAA;AAEzB,IAAA,MAAM,YAAY,GAAG,MAAM,aAAa;IACxC,IAAI,CAAC,YAAY,CAAC,Y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AM,KAAK,CAAC,QAAQ,CAAC,kBAAkB,CAAC,CAAC,MAAM,CAAC,YAAY,IAAI,EAAE,CAAC;IAC/E;AAEA,IAAA,OAAO,kBAAkB;AAC7B;AAEA;;;;;;;AAOG;AACG,SAAU,mBAAmB,CAAC,EAA2B,EAAA;AAC3D,IAAA,OAAO,EAAE,YAAY,KAAK,CAAC,QAAQ,GAAG,EAAE,CAAC,WAAW,EAAE,GAAG,EAAE;AAC/D;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kikiutils/mongoose",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0",
|
|
4
4
|
"description": "A Mongoose plugin for enhanced JSON normalization and common schema creation, with built-in support for pagination and automatic Decimal128 conversion.",
|
|
5
5
|
"author": "kiki-kanri",
|
|
6
6
|
"license": "MIT",
|
|
@@ -47,7 +47,7 @@
|
|
|
47
47
|
"./src"
|
|
48
48
|
],
|
|
49
49
|
"engines": {
|
|
50
|
-
"node": ">=
|
|
50
|
+
"node": ">=20.0.0"
|
|
51
51
|
},
|
|
52
52
|
"scripts": {
|
|
53
53
|
"build": "ts-project-builder './src/**/*.ts' --clean --preserve-modules --sourcemaps",
|
|
@@ -65,22 +65,22 @@
|
|
|
65
65
|
"dependencies": {
|
|
66
66
|
"decimal.js": "^10.6.0",
|
|
67
67
|
"lodash": "^4.17.21",
|
|
68
|
-
"mongoose": "^8.
|
|
68
|
+
"mongoose": "^8.17.1",
|
|
69
69
|
"mongoose-aggregate-paginate-v2": "^1.1.4",
|
|
70
70
|
"mongoose-paginate-v2": "^1.9.1",
|
|
71
71
|
"type-fest": "^4.41.0"
|
|
72
72
|
},
|
|
73
73
|
"devDependencies": {
|
|
74
74
|
"@kikiutils/changelogen": "^0.8.1",
|
|
75
|
-
"@kikiutils/eslint-config": "^
|
|
75
|
+
"@kikiutils/eslint-config": "^2.0.0",
|
|
76
76
|
"@kikiutils/tsconfigs": "^5.0.4",
|
|
77
77
|
"@types/lodash": "^4.17.20",
|
|
78
|
-
"@types/node": "^24.0
|
|
78
|
+
"@types/node": "^24.2.0",
|
|
79
79
|
"@vitest/coverage-v8": "^3.2.4",
|
|
80
80
|
"depcheck": "^1.4.7",
|
|
81
81
|
"ts-project-builder": "^5.0.2",
|
|
82
82
|
"ts-unused-exports": "^11.0.1",
|
|
83
|
-
"typescript": "^5.
|
|
83
|
+
"typescript": "^5.9.2",
|
|
84
84
|
"vitest": "^3.2.4"
|
|
85
85
|
},
|
|
86
86
|
"pnpm": {
|
package/src/types/data.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import type { SetFieldType } from 'type-fest';
|
|
2
|
+
|
|
1
3
|
import type { IfElse } from './utils';
|
|
2
4
|
|
|
3
5
|
declare global {
|
|
@@ -15,28 +17,40 @@ declare global {
|
|
|
15
17
|
& IfElse<UpdatedAtField, { updatedAt: string }, unknown>
|
|
16
18
|
& { id: string };
|
|
17
19
|
|
|
20
|
+
type DataToBaseMongooseDocType<
|
|
21
|
+
T,
|
|
22
|
+
ObjectIdFields extends keyof T = never,
|
|
23
|
+
DateFields extends Exclude<keyof T, ObjectIdFields> = never,
|
|
24
|
+
CreatedAtField extends boolean = true,
|
|
25
|
+
UpdatedAtField extends boolean = true,
|
|
26
|
+
> = BaseMongooseDocType<
|
|
27
|
+
SetFieldsToObjectId<SetFieldType<T, DateFields, Date>, ObjectIdFields>,
|
|
28
|
+
CreatedAtField,
|
|
29
|
+
UpdatedAtField
|
|
30
|
+
>;
|
|
31
|
+
|
|
18
32
|
/**
|
|
19
33
|
* Type definition to omit timestamp fields (`createdAt`, `updatedAt`)
|
|
20
|
-
* and other specified fields (`
|
|
34
|
+
* and other specified fields (`K`) from a given type `T`.
|
|
21
35
|
*
|
|
22
36
|
* @template T - The type from which to omit fields.
|
|
23
|
-
* @template
|
|
37
|
+
* @template K - Additional fields to omit, specified as a union of keys in `T`.
|
|
24
38
|
*/
|
|
25
|
-
type ExceptMongooseTimestampAndOtherFields<T,
|
|
39
|
+
type ExceptMongooseTimestampAndOtherFields<T, K extends keyof T> = Omit<T, 'createdAt' | 'updatedAt' | K>;
|
|
26
40
|
|
|
27
41
|
/**
|
|
28
42
|
* Type definition to omit timestamp fields (`createdAt`, `updatedAt`)
|
|
29
|
-
* and other specified fields (`
|
|
43
|
+
* and other specified fields (`K`) from a given type `T`.
|
|
30
44
|
*
|
|
31
45
|
* @template T - The type from which to omit fields.
|
|
32
|
-
* @template
|
|
46
|
+
* @template K - Additional fields to omit, specified as a union of string, number, or symbol keys.
|
|
33
47
|
*/
|
|
34
48
|
type OmitMongooseTimestampAndOtherFields<
|
|
35
49
|
T,
|
|
36
|
-
|
|
50
|
+
K extends number | string | symbol = never,
|
|
37
51
|
> = Omit<
|
|
38
52
|
T,
|
|
39
|
-
'createdAt' | 'updatedAt' |
|
|
53
|
+
'createdAt' | 'updatedAt' | K
|
|
40
54
|
>;
|
|
41
55
|
|
|
42
56
|
/**
|
package/src/types/index.ts
CHANGED
package/src/utils.ts
CHANGED
|
@@ -124,3 +124,15 @@ export async function mongooseDocumentOrObjectIdToDocument<
|
|
|
124
124
|
|
|
125
125
|
return documentOrObjectId;
|
|
126
126
|
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Converts the input, which can be a MongoDB ObjectId or a string,
|
|
130
|
+
* into a 24-character hexadecimal string representation of the ObjectId.
|
|
131
|
+
*
|
|
132
|
+
* @param {string | Types.ObjectId} id - The ObjectId instance or its string representation
|
|
133
|
+
*
|
|
134
|
+
* @returns {string} The hexadecimal string representation of the ObjectId
|
|
135
|
+
*/
|
|
136
|
+
export function toObjectIdHexString(id: string | Types.ObjectId) {
|
|
137
|
+
return id instanceof Types.ObjectId ? id.toHexString() : id;
|
|
138
|
+
}
|