mongoose-utility-kit 0.2.0 → 0.4.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/.github/workflows/ci.yml +16 -2
- package/CHANGELOG.md +12 -0
- package/README.md +203 -42
- package/dist/index.cjs +56 -4
- package/dist/index.d.cts +120 -11
- package/dist/index.d.ts +120 -11
- package/dist/index.js +52 -4
- package/package.json +6 -2
- package/src/plugins/toMap.ts +25 -3
- package/src/plugins/toRecords.ts +36 -3
- package/src/populated/functions.ts +120 -9
- package/src/types.ts +17 -1
- package/tests/initializers/models/embedded/address.ts +2 -10
- package/tests/initializers/models/user.ts +10 -3
- package/tests/plugins.test.ts +22 -1
- package/tests/populated.test.ts +250 -4
package/dist/index.d.cts
CHANGED
|
@@ -7,14 +7,49 @@ import { HydratedDocument, Schema, QueryWithHelpers, Document, PopulatedDoc, Typ
|
|
|
7
7
|
* @template T The type of the `HydratedDocument`.
|
|
8
8
|
* @returns The type of the document if `T` is a `HydratedDocument`, otherwise `never`.
|
|
9
9
|
*/
|
|
10
|
-
type
|
|
10
|
+
type ExtractRawDocType<T> = T extends HydratedDocument<infer U> ? U : never;
|
|
11
|
+
/**
|
|
12
|
+
* Extracts the virtuals type from a Mongoose `HydratedDocument`.
|
|
13
|
+
* This utility type is useful for accessing the shape of virtual properties
|
|
14
|
+
* defined on Mongoose schemas.
|
|
15
|
+
*
|
|
16
|
+
* @template T The type of the `HydratedDocument`.
|
|
17
|
+
* @returns The type of the virtuals if `T` is a `HydratedDocument`, otherwise `never`.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts
|
|
21
|
+
* type Virtuals = ExtractVirtualsType<HydratedDocument<MyDoc, MyVirtuals>>;
|
|
22
|
+
* // Virtuals is inferred as MyVirtuals
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
type ExtractVirtualsType<T> = T extends HydratedDocument<any, infer U> ? U : never;
|
|
11
26
|
|
|
12
27
|
/**
|
|
13
28
|
* Interface for query helpers that support converting results to a `Map`.
|
|
14
29
|
*
|
|
15
30
|
* @template T The type of the `HydratedDocument`.
|
|
16
31
|
*/
|
|
17
|
-
interface ToMapQueryHelpers<RawDocType, HydratedDocType> {
|
|
32
|
+
interface ToMapQueryHelpers<RawDocType, HydratedDocType, TVirtuals = {}> {
|
|
33
|
+
/**
|
|
34
|
+
* Query helper interface for converting query results into a `Map` keyed by string,
|
|
35
|
+
* including virtual properties.
|
|
36
|
+
*
|
|
37
|
+
* @template RawDocType - The type representing the raw document as returned by MongoDB.
|
|
38
|
+
* @template HydratedDocType - The type representing the hydrated Mongoose document.
|
|
39
|
+
* @template TVirtuals - The type representing virtual properties added to the raw document.
|
|
40
|
+
*
|
|
41
|
+
* @remarks
|
|
42
|
+
* - This overload of `toMap` is used when the query returns documents that include virtuals.
|
|
43
|
+
* - The result is a `Map<string, RawDocType & TVirtuals>`, where the key is typically derived
|
|
44
|
+
* from a document property such as `_id`.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```typescript
|
|
48
|
+
* const docsMap = await Model.find().toMap();
|
|
49
|
+
* // docsMap is a Map<string, RawDocType & TVirtuals>
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
toMap(this: QueryWithHelpers<(RawDocType & TVirtuals)[], HydratedDocType>): QueryWithHelpers<Map<string, RawDocType & TVirtuals>, HydratedDocType>;
|
|
18
53
|
/**
|
|
19
54
|
* Query helper interface for converting query results into a `Map` keyed by string.
|
|
20
55
|
*
|
|
@@ -56,14 +91,35 @@ interface ToMapQueryHelpers<RawDocType, HydratedDocType> {
|
|
|
56
91
|
* @template T The type of the `HydratedDocument`.
|
|
57
92
|
* @param schema The Mongoose schema to extend.
|
|
58
93
|
*/
|
|
59
|
-
declare const toMapPlugin: <T extends unknown>(schema: Schema<any, any, any, ToMapQueryHelpers<
|
|
94
|
+
declare const toMapPlugin: <T extends unknown>(schema: Schema<any, any, any, ToMapQueryHelpers<ExtractRawDocType<T>, T, ExtractVirtualsType<T>>, any, any, any>) => void;
|
|
60
95
|
|
|
61
96
|
/**
|
|
62
97
|
* Interface for query helpers that support converting results to a record (object).
|
|
63
98
|
*
|
|
64
99
|
* @template T The type of the `HydratedDocument`.
|
|
65
100
|
*/
|
|
66
|
-
interface ToRecordsQueryHelpers<RawDocType, HydratedDocType> {
|
|
101
|
+
interface ToRecordsQueryHelpers<RawDocType, HydratedDocType, TVirtuals = {}> {
|
|
102
|
+
/**
|
|
103
|
+
* Query helper for transforming query results into a `Record<string, T>` object,
|
|
104
|
+
* including virtual properties.
|
|
105
|
+
*
|
|
106
|
+
* @template RawDocType - The type representing the raw document as returned by MongoDB.
|
|
107
|
+
* @template HydratedDocType - The type representing the hydrated Mongoose document.
|
|
108
|
+
* @template TVirtuals - The type representing virtual properties added to the raw document.
|
|
109
|
+
*
|
|
110
|
+
* @remarks
|
|
111
|
+
* - This overload is used when the query returns documents that include virtuals.
|
|
112
|
+
* - The result is a plain object (`Record<string, RawDocType & TVirtuals>`) where each key
|
|
113
|
+
* corresponds to the document's `_id` (as a string), and the value includes both
|
|
114
|
+
* raw fields and virtuals.
|
|
115
|
+
*
|
|
116
|
+
* @example
|
|
117
|
+
* ```ts
|
|
118
|
+
* const records = await Model.find().toRecords();
|
|
119
|
+
* // records is of type Record<string, RawDocType & TVirtuals>
|
|
120
|
+
* ```
|
|
121
|
+
*/
|
|
122
|
+
toRecords(this: QueryWithHelpers<(RawDocType & TVirtuals)[], HydratedDocType>): QueryWithHelpers<Record<string, RawDocType & TVirtuals>, HydratedDocType>;
|
|
67
123
|
/**
|
|
68
124
|
* Query helpers for transforming query results into a record (object) keyed by document IDs.
|
|
69
125
|
*
|
|
@@ -103,7 +159,7 @@ interface ToRecordsQueryHelpers<RawDocType, HydratedDocType> {
|
|
|
103
159
|
* @template T The type of the `HydratedDocument`.
|
|
104
160
|
* @param schema The Mongoose schema to extend.
|
|
105
161
|
*/
|
|
106
|
-
declare const toRecordsPlugin: <T extends unknown>(schema: Schema<any, any, any, ToRecordsQueryHelpers<
|
|
162
|
+
declare const toRecordsPlugin: <T extends unknown>(schema: Schema<any, any, any, ToRecordsQueryHelpers<ExtractRawDocType<T>, T, ExtractVirtualsType<T>>, any, any, any>) => void;
|
|
107
163
|
|
|
108
164
|
/**
|
|
109
165
|
* Extracts the ID from a populated Mongoose document.
|
|
@@ -130,7 +186,8 @@ declare const getPopulatedDocumentId: <HydratedDocType extends Document>(populat
|
|
|
130
186
|
* through the `err` parameter.
|
|
131
187
|
*
|
|
132
188
|
* @template HydratedDocType - The type of the hydrated document, extending `Document`.
|
|
133
|
-
* @param {HydratedDocType | Types.ObjectId} docOrId
|
|
189
|
+
* @param {HydratedDocType | Types.ObjectId | string} docOrId
|
|
190
|
+
* - The document instance, ObjectId, or string ID to find.
|
|
134
191
|
* @param {Model<any, any, any, any, HydratedDocType>} MongooseModel
|
|
135
192
|
* - The Mongoose model used to query the database.
|
|
136
193
|
* @param {Error | (() => Error)} [err]
|
|
@@ -138,7 +195,7 @@ declare const getPopulatedDocumentId: <HydratedDocType extends Document>(populat
|
|
|
138
195
|
* @returns {Promise<HydratedDocType>} A promise that resolves to the hydrated document instance.
|
|
139
196
|
* @throws {Error} - Throws an error if the document is not found and no custom error is provided.
|
|
140
197
|
*/
|
|
141
|
-
declare const findOrReturnInstance: <HydratedDocType extends Document>(docOrId: HydratedDocType | Types.ObjectId, MongooseModel: Model<any, any, any, any, HydratedDocType>, err?: Error | (() => Error)) => Promise<HydratedDocType>;
|
|
198
|
+
declare const findOrReturnInstance: <HydratedDocType extends Document>(docOrId: HydratedDocType | Types.ObjectId | string, MongooseModel: Model<any, any, any, any, HydratedDocType>, err?: Error | (() => Error)) => Promise<HydratedDocType>;
|
|
142
199
|
/**
|
|
143
200
|
* Finds multiple Mongoose documents by their IDs or returns the provided instances.
|
|
144
201
|
*
|
|
@@ -150,13 +207,65 @@ declare const findOrReturnInstance: <HydratedDocType extends Document>(docOrId:
|
|
|
150
207
|
* that are not already represented by instances.
|
|
151
208
|
*
|
|
152
209
|
* @template HydratedDocType - The type of the hydrated documents, extending `Document`.
|
|
153
|
-
* @param {(HydratedDocType | Types.ObjectId)[]} docOrIds
|
|
154
|
-
* - An array of document instances or
|
|
210
|
+
* @param {(HydratedDocType | Types.ObjectId | string)[]} docOrIds
|
|
211
|
+
* - An array of document instances, ObjectIds, or string IDs to find.
|
|
155
212
|
* @param {Model<any, any, any, any, HydratedDocType>} MongooseModel
|
|
156
213
|
* - The Mongoose model used to query the database.
|
|
157
214
|
* @returns {Promise<HydratedDocType[]>}
|
|
158
215
|
* A promise that resolves to an array of hydrated document instances.
|
|
159
216
|
*/
|
|
160
|
-
declare const findOrReturnManyInstances: <HydratedDocType extends Document>(docOrIds: (HydratedDocType | Types.ObjectId)[], MongooseModel: Model<any, any, any, any, HydratedDocType>) => Promise<HydratedDocType[]>;
|
|
217
|
+
declare const findOrReturnManyInstances: <HydratedDocType extends Document>(docOrIds: (HydratedDocType | Types.ObjectId | string)[], MongooseModel: Model<any, any, any, any, HydratedDocType>) => Promise<HydratedDocType[]>;
|
|
218
|
+
/**
|
|
219
|
+
* Type guard that checks if a potentially populated Mongoose field
|
|
220
|
+
* is actually a populated document rather than an ObjectId.
|
|
221
|
+
*
|
|
222
|
+
* This is useful for narrowing the type of a `PopulatedDoc<HydratedDocType>`
|
|
223
|
+
* field after conditionally populating a query.
|
|
224
|
+
*
|
|
225
|
+
* @template HydratedDocType - The type of the populated document, extending `Document`.
|
|
226
|
+
* @param {PopulatedDoc<HydratedDocType>} value
|
|
227
|
+
* - The value to check, which may be either a populated document or an ObjectId.
|
|
228
|
+
* @returns {value is HydratedDocType} `true` if the value is a populated document.
|
|
229
|
+
*/
|
|
230
|
+
declare const isPopulated: <HydratedDocType extends Document>(value: PopulatedDoc<HydratedDocType>) => value is HydratedDocType;
|
|
231
|
+
/**
|
|
232
|
+
* Converts a string, ObjectId, or Document to a `Types.ObjectId`.
|
|
233
|
+
*
|
|
234
|
+
* If the value is already an `ObjectId`, it is returned as-is.
|
|
235
|
+
* If the value is a Document, its `_id` is returned.
|
|
236
|
+
* If the value is a string, a new `Types.ObjectId` is constructed from it.
|
|
237
|
+
*
|
|
238
|
+
* @param {string | Types.ObjectId | Document} value - The value to convert.
|
|
239
|
+
* @returns {Types.ObjectId} The resulting ObjectId.
|
|
240
|
+
* @throws {Error} If the string is not a valid 24-character hex string.
|
|
241
|
+
*/
|
|
242
|
+
declare const toObjectId: (value: string | Types.ObjectId | Document) => Types.ObjectId;
|
|
243
|
+
/**
|
|
244
|
+
* Compares two values by their underlying MongoDB document ID.
|
|
245
|
+
*
|
|
246
|
+
* Each value can be a string, `ObjectId`, `Document`, or `PopulatedDoc`.
|
|
247
|
+
* Returns `false` if either value is `null` or `undefined`.
|
|
248
|
+
*
|
|
249
|
+
* @param {string | Types.ObjectId | Document | PopulatedDoc<any> | null | undefined} a
|
|
250
|
+
* - The first value to compare.
|
|
251
|
+
* @param {string | Types.ObjectId | Document | PopulatedDoc<any> | null | undefined} b
|
|
252
|
+
* - The second value to compare.
|
|
253
|
+
* @returns {boolean} `true` if both values resolve to the same ID string.
|
|
254
|
+
*/
|
|
255
|
+
declare const areIdsEqual: (a: string | Types.ObjectId | Document | PopulatedDoc<any> | null | undefined, b: string | Types.ObjectId | Document | PopulatedDoc<any> | null | undefined) => boolean;
|
|
256
|
+
/**
|
|
257
|
+
* Asserts that a potentially populated Mongoose field is actually a populated document.
|
|
258
|
+
*
|
|
259
|
+
* This is the assertion counterpart of `isPopulated`. If the value is not populated,
|
|
260
|
+
* an error is thrown. Otherwise, the value is returned with its type narrowed to
|
|
261
|
+
* `HydratedDocType`.
|
|
262
|
+
*
|
|
263
|
+
* @template HydratedDocType - The type of the populated document, extending `Document`.
|
|
264
|
+
* @param {PopulatedDoc<HydratedDocType>} value
|
|
265
|
+
* - The value to check, which may be either a populated document or an ObjectId.
|
|
266
|
+
* @returns {HydratedDocType} The populated document.
|
|
267
|
+
* @throws {Error} If the value is not a populated document.
|
|
268
|
+
*/
|
|
269
|
+
declare const ensurePopulated: <HydratedDocType extends Document>(value: PopulatedDoc<HydratedDocType>) => HydratedDocType;
|
|
161
270
|
|
|
162
|
-
export { type ToMapQueryHelpers, type ToRecordsQueryHelpers, findOrReturnInstance, findOrReturnManyInstances, getPopulatedDocumentId, toMapPlugin as toMap, toRecordsPlugin as toRecords };
|
|
271
|
+
export { type ToMapQueryHelpers, type ToRecordsQueryHelpers, areIdsEqual, ensurePopulated, findOrReturnInstance, findOrReturnManyInstances, getPopulatedDocumentId, isPopulated, toMapPlugin as toMap, toObjectId, toRecordsPlugin as toRecords };
|
package/dist/index.d.ts
CHANGED
|
@@ -7,14 +7,49 @@ import { HydratedDocument, Schema, QueryWithHelpers, Document, PopulatedDoc, Typ
|
|
|
7
7
|
* @template T The type of the `HydratedDocument`.
|
|
8
8
|
* @returns The type of the document if `T` is a `HydratedDocument`, otherwise `never`.
|
|
9
9
|
*/
|
|
10
|
-
type
|
|
10
|
+
type ExtractRawDocType<T> = T extends HydratedDocument<infer U> ? U : never;
|
|
11
|
+
/**
|
|
12
|
+
* Extracts the virtuals type from a Mongoose `HydratedDocument`.
|
|
13
|
+
* This utility type is useful for accessing the shape of virtual properties
|
|
14
|
+
* defined on Mongoose schemas.
|
|
15
|
+
*
|
|
16
|
+
* @template T The type of the `HydratedDocument`.
|
|
17
|
+
* @returns The type of the virtuals if `T` is a `HydratedDocument`, otherwise `never`.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts
|
|
21
|
+
* type Virtuals = ExtractVirtualsType<HydratedDocument<MyDoc, MyVirtuals>>;
|
|
22
|
+
* // Virtuals is inferred as MyVirtuals
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
type ExtractVirtualsType<T> = T extends HydratedDocument<any, infer U> ? U : never;
|
|
11
26
|
|
|
12
27
|
/**
|
|
13
28
|
* Interface for query helpers that support converting results to a `Map`.
|
|
14
29
|
*
|
|
15
30
|
* @template T The type of the `HydratedDocument`.
|
|
16
31
|
*/
|
|
17
|
-
interface ToMapQueryHelpers<RawDocType, HydratedDocType> {
|
|
32
|
+
interface ToMapQueryHelpers<RawDocType, HydratedDocType, TVirtuals = {}> {
|
|
33
|
+
/**
|
|
34
|
+
* Query helper interface for converting query results into a `Map` keyed by string,
|
|
35
|
+
* including virtual properties.
|
|
36
|
+
*
|
|
37
|
+
* @template RawDocType - The type representing the raw document as returned by MongoDB.
|
|
38
|
+
* @template HydratedDocType - The type representing the hydrated Mongoose document.
|
|
39
|
+
* @template TVirtuals - The type representing virtual properties added to the raw document.
|
|
40
|
+
*
|
|
41
|
+
* @remarks
|
|
42
|
+
* - This overload of `toMap` is used when the query returns documents that include virtuals.
|
|
43
|
+
* - The result is a `Map<string, RawDocType & TVirtuals>`, where the key is typically derived
|
|
44
|
+
* from a document property such as `_id`.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```typescript
|
|
48
|
+
* const docsMap = await Model.find().toMap();
|
|
49
|
+
* // docsMap is a Map<string, RawDocType & TVirtuals>
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
toMap(this: QueryWithHelpers<(RawDocType & TVirtuals)[], HydratedDocType>): QueryWithHelpers<Map<string, RawDocType & TVirtuals>, HydratedDocType>;
|
|
18
53
|
/**
|
|
19
54
|
* Query helper interface for converting query results into a `Map` keyed by string.
|
|
20
55
|
*
|
|
@@ -56,14 +91,35 @@ interface ToMapQueryHelpers<RawDocType, HydratedDocType> {
|
|
|
56
91
|
* @template T The type of the `HydratedDocument`.
|
|
57
92
|
* @param schema The Mongoose schema to extend.
|
|
58
93
|
*/
|
|
59
|
-
declare const toMapPlugin: <T extends unknown>(schema: Schema<any, any, any, ToMapQueryHelpers<
|
|
94
|
+
declare const toMapPlugin: <T extends unknown>(schema: Schema<any, any, any, ToMapQueryHelpers<ExtractRawDocType<T>, T, ExtractVirtualsType<T>>, any, any, any>) => void;
|
|
60
95
|
|
|
61
96
|
/**
|
|
62
97
|
* Interface for query helpers that support converting results to a record (object).
|
|
63
98
|
*
|
|
64
99
|
* @template T The type of the `HydratedDocument`.
|
|
65
100
|
*/
|
|
66
|
-
interface ToRecordsQueryHelpers<RawDocType, HydratedDocType> {
|
|
101
|
+
interface ToRecordsQueryHelpers<RawDocType, HydratedDocType, TVirtuals = {}> {
|
|
102
|
+
/**
|
|
103
|
+
* Query helper for transforming query results into a `Record<string, T>` object,
|
|
104
|
+
* including virtual properties.
|
|
105
|
+
*
|
|
106
|
+
* @template RawDocType - The type representing the raw document as returned by MongoDB.
|
|
107
|
+
* @template HydratedDocType - The type representing the hydrated Mongoose document.
|
|
108
|
+
* @template TVirtuals - The type representing virtual properties added to the raw document.
|
|
109
|
+
*
|
|
110
|
+
* @remarks
|
|
111
|
+
* - This overload is used when the query returns documents that include virtuals.
|
|
112
|
+
* - The result is a plain object (`Record<string, RawDocType & TVirtuals>`) where each key
|
|
113
|
+
* corresponds to the document's `_id` (as a string), and the value includes both
|
|
114
|
+
* raw fields and virtuals.
|
|
115
|
+
*
|
|
116
|
+
* @example
|
|
117
|
+
* ```ts
|
|
118
|
+
* const records = await Model.find().toRecords();
|
|
119
|
+
* // records is of type Record<string, RawDocType & TVirtuals>
|
|
120
|
+
* ```
|
|
121
|
+
*/
|
|
122
|
+
toRecords(this: QueryWithHelpers<(RawDocType & TVirtuals)[], HydratedDocType>): QueryWithHelpers<Record<string, RawDocType & TVirtuals>, HydratedDocType>;
|
|
67
123
|
/**
|
|
68
124
|
* Query helpers for transforming query results into a record (object) keyed by document IDs.
|
|
69
125
|
*
|
|
@@ -103,7 +159,7 @@ interface ToRecordsQueryHelpers<RawDocType, HydratedDocType> {
|
|
|
103
159
|
* @template T The type of the `HydratedDocument`.
|
|
104
160
|
* @param schema The Mongoose schema to extend.
|
|
105
161
|
*/
|
|
106
|
-
declare const toRecordsPlugin: <T extends unknown>(schema: Schema<any, any, any, ToRecordsQueryHelpers<
|
|
162
|
+
declare const toRecordsPlugin: <T extends unknown>(schema: Schema<any, any, any, ToRecordsQueryHelpers<ExtractRawDocType<T>, T, ExtractVirtualsType<T>>, any, any, any>) => void;
|
|
107
163
|
|
|
108
164
|
/**
|
|
109
165
|
* Extracts the ID from a populated Mongoose document.
|
|
@@ -130,7 +186,8 @@ declare const getPopulatedDocumentId: <HydratedDocType extends Document>(populat
|
|
|
130
186
|
* through the `err` parameter.
|
|
131
187
|
*
|
|
132
188
|
* @template HydratedDocType - The type of the hydrated document, extending `Document`.
|
|
133
|
-
* @param {HydratedDocType | Types.ObjectId} docOrId
|
|
189
|
+
* @param {HydratedDocType | Types.ObjectId | string} docOrId
|
|
190
|
+
* - The document instance, ObjectId, or string ID to find.
|
|
134
191
|
* @param {Model<any, any, any, any, HydratedDocType>} MongooseModel
|
|
135
192
|
* - The Mongoose model used to query the database.
|
|
136
193
|
* @param {Error | (() => Error)} [err]
|
|
@@ -138,7 +195,7 @@ declare const getPopulatedDocumentId: <HydratedDocType extends Document>(populat
|
|
|
138
195
|
* @returns {Promise<HydratedDocType>} A promise that resolves to the hydrated document instance.
|
|
139
196
|
* @throws {Error} - Throws an error if the document is not found and no custom error is provided.
|
|
140
197
|
*/
|
|
141
|
-
declare const findOrReturnInstance: <HydratedDocType extends Document>(docOrId: HydratedDocType | Types.ObjectId, MongooseModel: Model<any, any, any, any, HydratedDocType>, err?: Error | (() => Error)) => Promise<HydratedDocType>;
|
|
198
|
+
declare const findOrReturnInstance: <HydratedDocType extends Document>(docOrId: HydratedDocType | Types.ObjectId | string, MongooseModel: Model<any, any, any, any, HydratedDocType>, err?: Error | (() => Error)) => Promise<HydratedDocType>;
|
|
142
199
|
/**
|
|
143
200
|
* Finds multiple Mongoose documents by their IDs or returns the provided instances.
|
|
144
201
|
*
|
|
@@ -150,13 +207,65 @@ declare const findOrReturnInstance: <HydratedDocType extends Document>(docOrId:
|
|
|
150
207
|
* that are not already represented by instances.
|
|
151
208
|
*
|
|
152
209
|
* @template HydratedDocType - The type of the hydrated documents, extending `Document`.
|
|
153
|
-
* @param {(HydratedDocType | Types.ObjectId)[]} docOrIds
|
|
154
|
-
* - An array of document instances or
|
|
210
|
+
* @param {(HydratedDocType | Types.ObjectId | string)[]} docOrIds
|
|
211
|
+
* - An array of document instances, ObjectIds, or string IDs to find.
|
|
155
212
|
* @param {Model<any, any, any, any, HydratedDocType>} MongooseModel
|
|
156
213
|
* - The Mongoose model used to query the database.
|
|
157
214
|
* @returns {Promise<HydratedDocType[]>}
|
|
158
215
|
* A promise that resolves to an array of hydrated document instances.
|
|
159
216
|
*/
|
|
160
|
-
declare const findOrReturnManyInstances: <HydratedDocType extends Document>(docOrIds: (HydratedDocType | Types.ObjectId)[], MongooseModel: Model<any, any, any, any, HydratedDocType>) => Promise<HydratedDocType[]>;
|
|
217
|
+
declare const findOrReturnManyInstances: <HydratedDocType extends Document>(docOrIds: (HydratedDocType | Types.ObjectId | string)[], MongooseModel: Model<any, any, any, any, HydratedDocType>) => Promise<HydratedDocType[]>;
|
|
218
|
+
/**
|
|
219
|
+
* Type guard that checks if a potentially populated Mongoose field
|
|
220
|
+
* is actually a populated document rather than an ObjectId.
|
|
221
|
+
*
|
|
222
|
+
* This is useful for narrowing the type of a `PopulatedDoc<HydratedDocType>`
|
|
223
|
+
* field after conditionally populating a query.
|
|
224
|
+
*
|
|
225
|
+
* @template HydratedDocType - The type of the populated document, extending `Document`.
|
|
226
|
+
* @param {PopulatedDoc<HydratedDocType>} value
|
|
227
|
+
* - The value to check, which may be either a populated document or an ObjectId.
|
|
228
|
+
* @returns {value is HydratedDocType} `true` if the value is a populated document.
|
|
229
|
+
*/
|
|
230
|
+
declare const isPopulated: <HydratedDocType extends Document>(value: PopulatedDoc<HydratedDocType>) => value is HydratedDocType;
|
|
231
|
+
/**
|
|
232
|
+
* Converts a string, ObjectId, or Document to a `Types.ObjectId`.
|
|
233
|
+
*
|
|
234
|
+
* If the value is already an `ObjectId`, it is returned as-is.
|
|
235
|
+
* If the value is a Document, its `_id` is returned.
|
|
236
|
+
* If the value is a string, a new `Types.ObjectId` is constructed from it.
|
|
237
|
+
*
|
|
238
|
+
* @param {string | Types.ObjectId | Document} value - The value to convert.
|
|
239
|
+
* @returns {Types.ObjectId} The resulting ObjectId.
|
|
240
|
+
* @throws {Error} If the string is not a valid 24-character hex string.
|
|
241
|
+
*/
|
|
242
|
+
declare const toObjectId: (value: string | Types.ObjectId | Document) => Types.ObjectId;
|
|
243
|
+
/**
|
|
244
|
+
* Compares two values by their underlying MongoDB document ID.
|
|
245
|
+
*
|
|
246
|
+
* Each value can be a string, `ObjectId`, `Document`, or `PopulatedDoc`.
|
|
247
|
+
* Returns `false` if either value is `null` or `undefined`.
|
|
248
|
+
*
|
|
249
|
+
* @param {string | Types.ObjectId | Document | PopulatedDoc<any> | null | undefined} a
|
|
250
|
+
* - The first value to compare.
|
|
251
|
+
* @param {string | Types.ObjectId | Document | PopulatedDoc<any> | null | undefined} b
|
|
252
|
+
* - The second value to compare.
|
|
253
|
+
* @returns {boolean} `true` if both values resolve to the same ID string.
|
|
254
|
+
*/
|
|
255
|
+
declare const areIdsEqual: (a: string | Types.ObjectId | Document | PopulatedDoc<any> | null | undefined, b: string | Types.ObjectId | Document | PopulatedDoc<any> | null | undefined) => boolean;
|
|
256
|
+
/**
|
|
257
|
+
* Asserts that a potentially populated Mongoose field is actually a populated document.
|
|
258
|
+
*
|
|
259
|
+
* This is the assertion counterpart of `isPopulated`. If the value is not populated,
|
|
260
|
+
* an error is thrown. Otherwise, the value is returned with its type narrowed to
|
|
261
|
+
* `HydratedDocType`.
|
|
262
|
+
*
|
|
263
|
+
* @template HydratedDocType - The type of the populated document, extending `Document`.
|
|
264
|
+
* @param {PopulatedDoc<HydratedDocType>} value
|
|
265
|
+
* - The value to check, which may be either a populated document or an ObjectId.
|
|
266
|
+
* @returns {HydratedDocType} The populated document.
|
|
267
|
+
* @throws {Error} If the value is not a populated document.
|
|
268
|
+
*/
|
|
269
|
+
declare const ensurePopulated: <HydratedDocType extends Document>(value: PopulatedDoc<HydratedDocType>) => HydratedDocType;
|
|
161
270
|
|
|
162
|
-
export { type ToMapQueryHelpers, type ToRecordsQueryHelpers, findOrReturnInstance, findOrReturnManyInstances, getPopulatedDocumentId, toMapPlugin as toMap, toRecordsPlugin as toRecords };
|
|
271
|
+
export { type ToMapQueryHelpers, type ToRecordsQueryHelpers, areIdsEqual, ensurePopulated, findOrReturnInstance, findOrReturnManyInstances, getPopulatedDocumentId, isPopulated, toMapPlugin as toMap, toObjectId, toRecordsPlugin as toRecords };
|
package/dist/index.js
CHANGED
|
@@ -42,11 +42,11 @@ var getPopulatedDocumentId = (populatedDocument) => {
|
|
|
42
42
|
if (populatedDocument instanceof Types.ObjectId) {
|
|
43
43
|
return populatedDocument.toString();
|
|
44
44
|
}
|
|
45
|
-
const
|
|
46
|
-
if (
|
|
47
|
-
return
|
|
45
|
+
const _id = populatedDocument?._id;
|
|
46
|
+
if (_id) {
|
|
47
|
+
return _id.toString();
|
|
48
48
|
}
|
|
49
|
-
throw new Error(
|
|
49
|
+
throw new Error(`Expected a populated document or ObjectId, got: ${typeof populatedDocument}`);
|
|
50
50
|
};
|
|
51
51
|
var findOrReturnInstance = async (docOrId, MongooseModel, err) => {
|
|
52
52
|
if (docOrId instanceof MongooseModel) {
|
|
@@ -61,6 +61,8 @@ var findOrReturnManyInstances = async (docOrIds, MongooseModel) => {
|
|
|
61
61
|
acc[0].push(docOrId);
|
|
62
62
|
} else if (docOrId instanceof Types.ObjectId) {
|
|
63
63
|
acc[1].push(docOrId);
|
|
64
|
+
} else if (typeof docOrId === "string") {
|
|
65
|
+
acc[1].push(new Types.ObjectId(docOrId));
|
|
64
66
|
}
|
|
65
67
|
return acc;
|
|
66
68
|
}, [[], []]);
|
|
@@ -74,10 +76,56 @@ var findOrReturnManyInstances = async (docOrIds, MongooseModel) => {
|
|
|
74
76
|
}
|
|
75
77
|
return result;
|
|
76
78
|
};
|
|
79
|
+
var isPopulated = (value) => {
|
|
80
|
+
if (value instanceof Types.ObjectId) {
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
if (value != null && typeof value === "object" && ("_id" in value || "id" in value)) {
|
|
84
|
+
return true;
|
|
85
|
+
}
|
|
86
|
+
return false;
|
|
87
|
+
};
|
|
88
|
+
var toObjectId = (value) => {
|
|
89
|
+
if (value instanceof Types.ObjectId) {
|
|
90
|
+
return value;
|
|
91
|
+
}
|
|
92
|
+
if (typeof value === "object" && value !== null && "_id" in value) {
|
|
93
|
+
return value._id;
|
|
94
|
+
}
|
|
95
|
+
return new Types.ObjectId(value);
|
|
96
|
+
};
|
|
97
|
+
var areIdsEqual = (a, b) => {
|
|
98
|
+
if (a == null || b == null) {
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
101
|
+
const normalize = (val) => {
|
|
102
|
+
if (typeof val === "string") {
|
|
103
|
+
return val;
|
|
104
|
+
}
|
|
105
|
+
if (val instanceof Types.ObjectId) {
|
|
106
|
+
return val.toString();
|
|
107
|
+
}
|
|
108
|
+
if (typeof val === "object" && "_id" in val) {
|
|
109
|
+
return val._id.toString();
|
|
110
|
+
}
|
|
111
|
+
return String(val);
|
|
112
|
+
};
|
|
113
|
+
return normalize(a) === normalize(b);
|
|
114
|
+
};
|
|
115
|
+
var ensurePopulated = (value) => {
|
|
116
|
+
if (isPopulated(value)) {
|
|
117
|
+
return value;
|
|
118
|
+
}
|
|
119
|
+
throw new Error(`Expected a populated document, got: ${typeof value}`);
|
|
120
|
+
};
|
|
77
121
|
export {
|
|
122
|
+
areIdsEqual,
|
|
123
|
+
ensurePopulated,
|
|
78
124
|
findOrReturnInstance,
|
|
79
125
|
findOrReturnManyInstances,
|
|
80
126
|
getPopulatedDocumentId,
|
|
127
|
+
isPopulated,
|
|
81
128
|
toMap_default as toMap,
|
|
129
|
+
toObjectId,
|
|
82
130
|
toRecords_default as toRecords
|
|
83
131
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mongoose-utility-kit",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "A set of utility functions for the Mongoose library",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"mongoose",
|
|
@@ -14,6 +14,9 @@
|
|
|
14
14
|
"repository": "git@github.com:nikzanda/mongoose-utility-kit.git",
|
|
15
15
|
"author": "Nicolò Zandarin <nikzanda@hotmail.it>",
|
|
16
16
|
"license": "MIT",
|
|
17
|
+
"engines": {
|
|
18
|
+
"node": ">=18"
|
|
19
|
+
},
|
|
17
20
|
"exports": {
|
|
18
21
|
"./package.json": "./package.json",
|
|
19
22
|
".": {
|
|
@@ -41,7 +44,8 @@
|
|
|
41
44
|
"eslint-plugin-import": "^2.31.0",
|
|
42
45
|
"jest": "^29.7.0",
|
|
43
46
|
"mongodb-memory-server": "^10.1.4",
|
|
44
|
-
"mongoose": "
|
|
47
|
+
"mongoose": "8",
|
|
48
|
+
"mongoose-lean-virtuals": "^2.0.0",
|
|
45
49
|
"ts-jest": "^29.2.5",
|
|
46
50
|
"tsup": "^8.3.6",
|
|
47
51
|
"typescript": "^5.7.3"
|
package/src/plugins/toMap.ts
CHANGED
|
@@ -1,13 +1,35 @@
|
|
|
1
1
|
/* eslint-disable max-len */
|
|
2
2
|
import { HydratedDocument, QueryWithHelpers, Schema } from 'mongoose';
|
|
3
|
-
import {
|
|
3
|
+
import { ExtractRawDocType, ExtractVirtualsType } from '../types';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Interface for query helpers that support converting results to a `Map`.
|
|
7
7
|
*
|
|
8
8
|
* @template T The type of the `HydratedDocument`.
|
|
9
9
|
*/
|
|
10
|
-
export interface ToMapQueryHelpers<RawDocType, HydratedDocType> {
|
|
10
|
+
export interface ToMapQueryHelpers<RawDocType, HydratedDocType, TVirtuals = {}> {
|
|
11
|
+
/**
|
|
12
|
+
* Query helper interface for converting query results into a `Map` keyed by string,
|
|
13
|
+
* including virtual properties.
|
|
14
|
+
*
|
|
15
|
+
* @template RawDocType - The type representing the raw document as returned by MongoDB.
|
|
16
|
+
* @template HydratedDocType - The type representing the hydrated Mongoose document.
|
|
17
|
+
* @template TVirtuals - The type representing virtual properties added to the raw document.
|
|
18
|
+
*
|
|
19
|
+
* @remarks
|
|
20
|
+
* - This overload of `toMap` is used when the query returns documents that include virtuals.
|
|
21
|
+
* - The result is a `Map<string, RawDocType & TVirtuals>`, where the key is typically derived
|
|
22
|
+
* from a document property such as `_id`.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```typescript
|
|
26
|
+
* const docsMap = await Model.find().toMap();
|
|
27
|
+
* // docsMap is a Map<string, RawDocType & TVirtuals>
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
toMap(
|
|
31
|
+
this: QueryWithHelpers<(RawDocType & TVirtuals)[], HydratedDocType>
|
|
32
|
+
): QueryWithHelpers<Map<string, RawDocType & TVirtuals>, HydratedDocType>;
|
|
11
33
|
/**
|
|
12
34
|
* Query helper interface for converting query results into a `Map` keyed by string.
|
|
13
35
|
*
|
|
@@ -55,7 +77,7 @@ export interface ToMapQueryHelpers<RawDocType, HydratedDocType> {
|
|
|
55
77
|
* @param schema The Mongoose schema to extend.
|
|
56
78
|
*/
|
|
57
79
|
const toMapPlugin = <T extends HydratedDocument<any>>(
|
|
58
|
-
schema: Schema<any, any, any, ToMapQueryHelpers<
|
|
80
|
+
schema: Schema<any, any, any, ToMapQueryHelpers<ExtractRawDocType<T>, T, ExtractVirtualsType<T>>, any, any, any>,
|
|
59
81
|
) => {
|
|
60
82
|
// eslint-disable-next-line no-param-reassign
|
|
61
83
|
schema.query.toMap = function (this) {
|
package/src/plugins/toRecords.ts
CHANGED
|
@@ -1,12 +1,35 @@
|
|
|
1
1
|
import { HydratedDocument, QueryWithHelpers, Schema } from 'mongoose';
|
|
2
|
-
import {
|
|
2
|
+
import { ExtractRawDocType, ExtractVirtualsType } from '../types';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Interface for query helpers that support converting results to a record (object).
|
|
6
6
|
*
|
|
7
7
|
* @template T The type of the `HydratedDocument`.
|
|
8
8
|
*/
|
|
9
|
-
export interface ToRecordsQueryHelpers<RawDocType, HydratedDocType> {
|
|
9
|
+
export interface ToRecordsQueryHelpers<RawDocType, HydratedDocType, TVirtuals = {}> {
|
|
10
|
+
/**
|
|
11
|
+
* Query helper for transforming query results into a `Record<string, T>` object,
|
|
12
|
+
* including virtual properties.
|
|
13
|
+
*
|
|
14
|
+
* @template RawDocType - The type representing the raw document as returned by MongoDB.
|
|
15
|
+
* @template HydratedDocType - The type representing the hydrated Mongoose document.
|
|
16
|
+
* @template TVirtuals - The type representing virtual properties added to the raw document.
|
|
17
|
+
*
|
|
18
|
+
* @remarks
|
|
19
|
+
* - This overload is used when the query returns documents that include virtuals.
|
|
20
|
+
* - The result is a plain object (`Record<string, RawDocType & TVirtuals>`) where each key
|
|
21
|
+
* corresponds to the document's `_id` (as a string), and the value includes both
|
|
22
|
+
* raw fields and virtuals.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```ts
|
|
26
|
+
* const records = await Model.find().toRecords();
|
|
27
|
+
* // records is of type Record<string, RawDocType & TVirtuals>
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
toRecords(
|
|
31
|
+
this: QueryWithHelpers<(RawDocType & TVirtuals)[], HydratedDocType>
|
|
32
|
+
): QueryWithHelpers<Record<string, RawDocType & TVirtuals>, HydratedDocType>;
|
|
10
33
|
/**
|
|
11
34
|
* Query helpers for transforming query results into a record (object) keyed by document IDs.
|
|
12
35
|
*
|
|
@@ -52,7 +75,17 @@ export interface ToRecordsQueryHelpers<RawDocType, HydratedDocType> {
|
|
|
52
75
|
* @param schema The Mongoose schema to extend.
|
|
53
76
|
*/
|
|
54
77
|
const toRecordsPlugin = <T extends HydratedDocument<any>>(
|
|
55
|
-
schema: Schema<
|
|
78
|
+
schema: Schema<
|
|
79
|
+
any,
|
|
80
|
+
any,
|
|
81
|
+
any,
|
|
82
|
+
ToRecordsQueryHelpers<ExtractRawDocType<T>,
|
|
83
|
+
T,
|
|
84
|
+
ExtractVirtualsType<T>>,
|
|
85
|
+
any,
|
|
86
|
+
any,
|
|
87
|
+
any
|
|
88
|
+
>,
|
|
56
89
|
) => {
|
|
57
90
|
// eslint-disable-next-line no-param-reassign
|
|
58
91
|
schema.query.toRecords = function toRecords(this) {
|