mongoose-utility-kit 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # mongoose-utility-kit
2
2
 
3
+ ## 0.2.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 9217086: fixed ToMapQueryHelpers & ToRecordsQueryHelpers interfaces
8
+
3
9
  ## 0.1.0
4
10
 
5
11
  ### Minor Changes
package/README.md CHANGED
@@ -138,7 +138,7 @@ These plugins provide helper methods for Mongoose queries to transform the resul
138
138
 
139
139
  type MyModelType = Model<
140
140
  IMyModel,
141
- ToMapQueryHelpers<MyModelHydratedDocument> & ToRecordsQueryHelpers<MyModelHydratedDocument>,
141
+ ToMapQueryHelpers<IMyModel, MyModelHydratedDocument> & ToRecordsQueryHelpers<IMyModel, MyModelHydratedDocument>,
142
142
  {},
143
143
  {},
144
144
  MyModelHydratedDocument
package/dist/index.cjs CHANGED
@@ -29,42 +29,38 @@ __export(index_exports, {
29
29
  module.exports = __toCommonJS(index_exports);
30
30
 
31
31
  // src/plugins/toMap.ts
32
- function toMap() {
33
- const result = this.transform((docs) => {
34
- if (!docs) {
35
- return /* @__PURE__ */ new Map();
36
- }
37
- if (Array.isArray(docs)) {
38
- return new Map(docs.map((doc) => [doc._id.toString(), doc]));
39
- }
40
- return /* @__PURE__ */ new Map([[docs._id.toString(), docs]]);
41
- });
42
- return result;
43
- }
44
32
  var toMapPlugin = (schema) => {
45
- schema.query.toMap = toMap;
33
+ schema.query.toMap = function() {
34
+ const result = this.transform((docs) => {
35
+ if (!docs) {
36
+ return /* @__PURE__ */ new Map();
37
+ }
38
+ if (Array.isArray(docs)) {
39
+ return new Map(docs.map((doc) => [doc._id.toString(), doc]));
40
+ }
41
+ return /* @__PURE__ */ new Map([[docs._id.toString(), docs]]);
42
+ });
43
+ return result;
44
+ };
46
45
  };
47
46
  var toMap_default = toMapPlugin;
48
47
 
49
48
  // src/plugins/toRecords.ts
50
- function toRecords() {
51
- const result = this.transform((docs) => {
52
- if (!docs) {
53
- return {};
54
- }
55
- if (Array.isArray(docs)) {
56
- return Object.fromEntries(
57
- docs.map((doc) => [doc._id.toString(), doc])
58
- );
59
- }
60
- return {
61
- [docs._id.toString()]: docs
62
- };
63
- });
64
- return result;
65
- }
66
49
  var toRecordsPlugin = (schema) => {
67
- schema.query.toRecords = toRecords;
50
+ schema.query.toRecords = function toRecords() {
51
+ const result = this.transform((docs) => {
52
+ if (!docs) {
53
+ return {};
54
+ }
55
+ if (Array.isArray(docs)) {
56
+ return Object.fromEntries(docs.map((doc) => [doc._id.toString(), doc]));
57
+ }
58
+ return {
59
+ [docs._id.toString()]: docs
60
+ };
61
+ });
62
+ return result;
63
+ };
68
64
  };
69
65
  var toRecords_default = toRecordsPlugin;
70
66
 
package/dist/index.d.cts CHANGED
@@ -14,13 +14,41 @@ type ExtractDocType<T> = T extends HydratedDocument<infer U> ? U : never;
14
14
  *
15
15
  * @template T The type of the `HydratedDocument`.
16
16
  */
17
- interface ToMapQueryHelpers<T extends HydratedDocument<any>> {
17
+ interface ToMapQueryHelpers<RawDocType, HydratedDocType> {
18
18
  /**
19
- * Converts the query results into a `Map` where the key is the document's _id.
19
+ * Query helper interface for converting query results into a `Map` keyed by string.
20
20
  *
21
- * @returns A QueryWithHelpers instance that returns a `Map` of documents.
21
+ * @template RawDocType - The type representing the raw document as returned by MongoDB.
22
+ * @template HydratedDocType - The type representing the hydrated Mongoose document.
23
+ *
24
+ * @remarks
25
+ * - The `toMap` method transforms an array of documents returned by a query into a `Map<string, T>`,
26
+ * where the key is typically derived from a document property (e.g., `_id` as a string).
27
+ *
28
+ * @example
29
+ * ```typescript
30
+ * const docsMap = await Model.find().toMap();
31
+ * // docsMap is a Map<string, HydratedDocType>
32
+ * ```
33
+ */
34
+ toMap(this: QueryWithHelpers<HydratedDocType[], HydratedDocType>): QueryWithHelpers<Map<string, HydratedDocType>, HydratedDocType>;
35
+ /**
36
+ * Query helper interface for converting query results into a `Map` keyed by string.
37
+ *
38
+ * @template RawDocType - The type representing the raw document as returned by MongoDB.
39
+ * @template HydratedDocType - The type representing the hydrated Mongoose document.
40
+ *
41
+ * @remarks
42
+ * - The `toMap` method transforms an array of documents returned by a query into a `Map<string, T>`,
43
+ * where the key is typically derived from a document property (e.g., `_id` as a string).
44
+ *
45
+ * @example
46
+ * ```typescript
47
+ * const docsMap = await Model.find().toMap();
48
+ * // docsMap is a Map<string, RawDocType>
49
+ * ```
22
50
  */
23
- toMap(): QueryWithHelpers<Map<string, T>, T, ToMapQueryHelpers<T>, ExtractDocType<T>>;
51
+ toMap(this: QueryWithHelpers<RawDocType[], HydratedDocType>): QueryWithHelpers<Map<string, RawDocType>, HydratedDocType>;
24
52
  }
25
53
  /**
26
54
  * Mongoose plugin to add the `toMap` method to queries.
@@ -28,20 +56,46 @@ interface ToMapQueryHelpers<T extends HydratedDocument<any>> {
28
56
  * @template T The type of the `HydratedDocument`.
29
57
  * @param schema The Mongoose schema to extend.
30
58
  */
31
- declare const toMapPlugin: <T extends unknown>(schema: Schema<any, any, any, ToMapQueryHelpers<T>, any, any, any>) => void;
59
+ declare const toMapPlugin: <T extends unknown>(schema: Schema<any, any, any, ToMapQueryHelpers<ExtractDocType<T>, T>, any, any, any>) => void;
32
60
 
33
61
  /**
34
62
  * Interface for query helpers that support converting results to a record (object).
35
63
  *
36
64
  * @template T The type of the `HydratedDocument`.
37
65
  */
38
- interface ToRecordsQueryHelpers<T extends HydratedDocument<any>> {
66
+ interface ToRecordsQueryHelpers<RawDocType, HydratedDocType> {
39
67
  /**
40
- * Converts the query results into a record (object) where the key is the document's _id.
68
+ * Query helpers for transforming query results into a record (object) keyed by document IDs.
69
+ *
70
+ * @template RawDocType - The type representing the raw document as returned by MongoDB.
71
+ * @template HydratedDocType - The type representing the hydrated Mongoose document.
72
+ *
73
+ * Provides overloaded `toRecords` methods for use with Mongoose queries, allowing the result
74
+ * array to be converted into a record where each key is the document's `_id` (as a string)
75
+ * and the value is the corresponding document.
76
+ *
77
+ * @example
78
+ * // Usage in a Mongoose query:
79
+ * const records = await Model.find().toRecords();
80
+ * // records is of type Record<string, HydratedDocType>
81
+ */
82
+ toRecords(this: QueryWithHelpers<HydratedDocType[], HydratedDocType>): QueryWithHelpers<Record<string, HydratedDocType>, HydratedDocType>;
83
+ /**
84
+ * Query helpers for transforming query results into a record (object) keyed by document IDs.
85
+ *
86
+ * @template RawDocType - The type representing the raw document as returned by MongoDB.
87
+ * @template HydratedDocType - The type representing the hydrated Mongoose document.
88
+ *
89
+ * Provides overloaded `toRecords` methods for use with Mongoose queries, allowing the result
90
+ * array to be converted into a record where each key is the document's `_id` (as a string)
91
+ * and the value is the corresponding document.
41
92
  *
42
- * @returns A QueryWithHelpers instance that returns a Record of documents.
93
+ * @example
94
+ * // Usage in a Mongoose query:
95
+ * const records = await Model.find().toRecords();
96
+ * // records is of type Record<string, RawDocType>
43
97
  */
44
- toRecords(): QueryWithHelpers<Record<string, T>, T, ToRecordsQueryHelpers<T>, ExtractDocType<T>>;
98
+ toRecords(this: QueryWithHelpers<RawDocType[], HydratedDocType>): QueryWithHelpers<Record<string, RawDocType>, HydratedDocType>;
45
99
  }
46
100
  /**
47
101
  * Mongoose plugin to add the `toRecords` method to queries.
@@ -49,7 +103,7 @@ interface ToRecordsQueryHelpers<T extends HydratedDocument<any>> {
49
103
  * @template T The type of the `HydratedDocument`.
50
104
  * @param schema The Mongoose schema to extend.
51
105
  */
52
- declare const toRecordsPlugin: <T extends unknown>(schema: Schema<any, any, any, ToRecordsQueryHelpers<T>, any, any, any>) => void;
106
+ declare const toRecordsPlugin: <T extends unknown>(schema: Schema<any, any, any, ToRecordsQueryHelpers<ExtractDocType<T>, T>, any, any, any>) => void;
53
107
 
54
108
  /**
55
109
  * Extracts the ID from a populated Mongoose document.
package/dist/index.d.ts CHANGED
@@ -14,13 +14,41 @@ type ExtractDocType<T> = T extends HydratedDocument<infer U> ? U : never;
14
14
  *
15
15
  * @template T The type of the `HydratedDocument`.
16
16
  */
17
- interface ToMapQueryHelpers<T extends HydratedDocument<any>> {
17
+ interface ToMapQueryHelpers<RawDocType, HydratedDocType> {
18
18
  /**
19
- * Converts the query results into a `Map` where the key is the document's _id.
19
+ * Query helper interface for converting query results into a `Map` keyed by string.
20
20
  *
21
- * @returns A QueryWithHelpers instance that returns a `Map` of documents.
21
+ * @template RawDocType - The type representing the raw document as returned by MongoDB.
22
+ * @template HydratedDocType - The type representing the hydrated Mongoose document.
23
+ *
24
+ * @remarks
25
+ * - The `toMap` method transforms an array of documents returned by a query into a `Map<string, T>`,
26
+ * where the key is typically derived from a document property (e.g., `_id` as a string).
27
+ *
28
+ * @example
29
+ * ```typescript
30
+ * const docsMap = await Model.find().toMap();
31
+ * // docsMap is a Map<string, HydratedDocType>
32
+ * ```
33
+ */
34
+ toMap(this: QueryWithHelpers<HydratedDocType[], HydratedDocType>): QueryWithHelpers<Map<string, HydratedDocType>, HydratedDocType>;
35
+ /**
36
+ * Query helper interface for converting query results into a `Map` keyed by string.
37
+ *
38
+ * @template RawDocType - The type representing the raw document as returned by MongoDB.
39
+ * @template HydratedDocType - The type representing the hydrated Mongoose document.
40
+ *
41
+ * @remarks
42
+ * - The `toMap` method transforms an array of documents returned by a query into a `Map<string, T>`,
43
+ * where the key is typically derived from a document property (e.g., `_id` as a string).
44
+ *
45
+ * @example
46
+ * ```typescript
47
+ * const docsMap = await Model.find().toMap();
48
+ * // docsMap is a Map<string, RawDocType>
49
+ * ```
22
50
  */
23
- toMap(): QueryWithHelpers<Map<string, T>, T, ToMapQueryHelpers<T>, ExtractDocType<T>>;
51
+ toMap(this: QueryWithHelpers<RawDocType[], HydratedDocType>): QueryWithHelpers<Map<string, RawDocType>, HydratedDocType>;
24
52
  }
25
53
  /**
26
54
  * Mongoose plugin to add the `toMap` method to queries.
@@ -28,20 +56,46 @@ interface ToMapQueryHelpers<T extends HydratedDocument<any>> {
28
56
  * @template T The type of the `HydratedDocument`.
29
57
  * @param schema The Mongoose schema to extend.
30
58
  */
31
- declare const toMapPlugin: <T extends unknown>(schema: Schema<any, any, any, ToMapQueryHelpers<T>, any, any, any>) => void;
59
+ declare const toMapPlugin: <T extends unknown>(schema: Schema<any, any, any, ToMapQueryHelpers<ExtractDocType<T>, T>, any, any, any>) => void;
32
60
 
33
61
  /**
34
62
  * Interface for query helpers that support converting results to a record (object).
35
63
  *
36
64
  * @template T The type of the `HydratedDocument`.
37
65
  */
38
- interface ToRecordsQueryHelpers<T extends HydratedDocument<any>> {
66
+ interface ToRecordsQueryHelpers<RawDocType, HydratedDocType> {
39
67
  /**
40
- * Converts the query results into a record (object) where the key is the document's _id.
68
+ * Query helpers for transforming query results into a record (object) keyed by document IDs.
69
+ *
70
+ * @template RawDocType - The type representing the raw document as returned by MongoDB.
71
+ * @template HydratedDocType - The type representing the hydrated Mongoose document.
72
+ *
73
+ * Provides overloaded `toRecords` methods for use with Mongoose queries, allowing the result
74
+ * array to be converted into a record where each key is the document's `_id` (as a string)
75
+ * and the value is the corresponding document.
76
+ *
77
+ * @example
78
+ * // Usage in a Mongoose query:
79
+ * const records = await Model.find().toRecords();
80
+ * // records is of type Record<string, HydratedDocType>
81
+ */
82
+ toRecords(this: QueryWithHelpers<HydratedDocType[], HydratedDocType>): QueryWithHelpers<Record<string, HydratedDocType>, HydratedDocType>;
83
+ /**
84
+ * Query helpers for transforming query results into a record (object) keyed by document IDs.
85
+ *
86
+ * @template RawDocType - The type representing the raw document as returned by MongoDB.
87
+ * @template HydratedDocType - The type representing the hydrated Mongoose document.
88
+ *
89
+ * Provides overloaded `toRecords` methods for use with Mongoose queries, allowing the result
90
+ * array to be converted into a record where each key is the document's `_id` (as a string)
91
+ * and the value is the corresponding document.
41
92
  *
42
- * @returns A QueryWithHelpers instance that returns a Record of documents.
93
+ * @example
94
+ * // Usage in a Mongoose query:
95
+ * const records = await Model.find().toRecords();
96
+ * // records is of type Record<string, RawDocType>
43
97
  */
44
- toRecords(): QueryWithHelpers<Record<string, T>, T, ToRecordsQueryHelpers<T>, ExtractDocType<T>>;
98
+ toRecords(this: QueryWithHelpers<RawDocType[], HydratedDocType>): QueryWithHelpers<Record<string, RawDocType>, HydratedDocType>;
45
99
  }
46
100
  /**
47
101
  * Mongoose plugin to add the `toRecords` method to queries.
@@ -49,7 +103,7 @@ interface ToRecordsQueryHelpers<T extends HydratedDocument<any>> {
49
103
  * @template T The type of the `HydratedDocument`.
50
104
  * @param schema The Mongoose schema to extend.
51
105
  */
52
- declare const toRecordsPlugin: <T extends unknown>(schema: Schema<any, any, any, ToRecordsQueryHelpers<T>, any, any, any>) => void;
106
+ declare const toRecordsPlugin: <T extends unknown>(schema: Schema<any, any, any, ToRecordsQueryHelpers<ExtractDocType<T>, T>, any, any, any>) => void;
53
107
 
54
108
  /**
55
109
  * Extracts the ID from a populated Mongoose document.
package/dist/index.js CHANGED
@@ -1,40 +1,36 @@
1
1
  // src/plugins/toMap.ts
2
- function toMap() {
3
- const result = this.transform((docs) => {
4
- if (!docs) {
5
- return /* @__PURE__ */ new Map();
6
- }
7
- if (Array.isArray(docs)) {
8
- return new Map(docs.map((doc) => [doc._id.toString(), doc]));
9
- }
10
- return /* @__PURE__ */ new Map([[docs._id.toString(), docs]]);
11
- });
12
- return result;
13
- }
14
2
  var toMapPlugin = (schema) => {
15
- schema.query.toMap = toMap;
3
+ schema.query.toMap = function() {
4
+ const result = this.transform((docs) => {
5
+ if (!docs) {
6
+ return /* @__PURE__ */ new Map();
7
+ }
8
+ if (Array.isArray(docs)) {
9
+ return new Map(docs.map((doc) => [doc._id.toString(), doc]));
10
+ }
11
+ return /* @__PURE__ */ new Map([[docs._id.toString(), docs]]);
12
+ });
13
+ return result;
14
+ };
16
15
  };
17
16
  var toMap_default = toMapPlugin;
18
17
 
19
18
  // src/plugins/toRecords.ts
20
- function toRecords() {
21
- const result = this.transform((docs) => {
22
- if (!docs) {
23
- return {};
24
- }
25
- if (Array.isArray(docs)) {
26
- return Object.fromEntries(
27
- docs.map((doc) => [doc._id.toString(), doc])
28
- );
29
- }
30
- return {
31
- [docs._id.toString()]: docs
32
- };
33
- });
34
- return result;
35
- }
36
19
  var toRecordsPlugin = (schema) => {
37
- schema.query.toRecords = toRecords;
20
+ schema.query.toRecords = function toRecords() {
21
+ const result = this.transform((docs) => {
22
+ if (!docs) {
23
+ return {};
24
+ }
25
+ if (Array.isArray(docs)) {
26
+ return Object.fromEntries(docs.map((doc) => [doc._id.toString(), doc]));
27
+ }
28
+ return {
29
+ [docs._id.toString()]: docs
30
+ };
31
+ });
32
+ return result;
33
+ };
38
34
  };
39
35
  var toRecords_default = toRecordsPlugin;
40
36
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mongoose-utility-kit",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "A set of utility functions for the Mongoose library",
5
5
  "keywords": [
6
6
  "mongoose",
@@ -1,3 +1,4 @@
1
+ /* eslint-disable max-len */
1
2
  import { HydratedDocument, QueryWithHelpers, Schema } from 'mongoose';
2
3
  import { ExtractDocType } from '../types';
3
4
 
@@ -6,45 +7,45 @@ import { ExtractDocType } from '../types';
6
7
  *
7
8
  * @template T The type of the `HydratedDocument`.
8
9
  */
9
- export interface ToMapQueryHelpers<T extends HydratedDocument<any>> {
10
+ export interface ToMapQueryHelpers<RawDocType, HydratedDocType> {
10
11
  /**
11
- * Converts the query results into a `Map` where the key is the document's _id.
12
+ * Query helper interface for converting query results into a `Map` keyed by string.
12
13
  *
13
- * @returns A QueryWithHelpers instance that returns a `Map` of documents.
14
+ * @template RawDocType - The type representing the raw document as returned by MongoDB.
15
+ * @template HydratedDocType - The type representing the hydrated Mongoose document.
16
+ *
17
+ * @remarks
18
+ * - The `toMap` method transforms an array of documents returned by a query into a `Map<string, T>`,
19
+ * where the key is typically derived from a document property (e.g., `_id` as a string).
20
+ *
21
+ * @example
22
+ * ```typescript
23
+ * const docsMap = await Model.find().toMap();
24
+ * // docsMap is a Map<string, HydratedDocType>
25
+ * ```
14
26
  */
15
- toMap(): QueryWithHelpers<
16
- Map<string, T>,
17
- T,
18
- ToMapQueryHelpers<T>,
19
- ExtractDocType<T>
20
- >,
21
- }
22
-
23
- /**
24
- * Function to transform query results into a `Map`.
25
- *
26
- * @template T The type of the `HydratedDocument`.
27
- * @this QueryWithHelpers instance.
28
- * @returns The transformed query result as a `Map`.
29
- */
30
- function toMap<T extends HydratedDocument<any>>(
31
- this: QueryWithHelpers<
32
- Map<string, T>,
33
- T,
34
- ToMapQueryHelpers<T>,
35
- ExtractDocType<T>
36
- >,
37
- ) {
38
- const result = this.transform((docs: any) => {
39
- if (!docs) {
40
- return new Map();
41
- }
42
- if (Array.isArray(docs)) {
43
- return new Map(docs.map((doc: any) => [doc._id.toString(), doc]));
44
- }
45
- return new Map([[docs._id.toString(), docs]]);
46
- });
47
- return result;
27
+ toMap(
28
+ this: QueryWithHelpers<HydratedDocType[], HydratedDocType>
29
+ ): QueryWithHelpers<Map<string, HydratedDocType>, HydratedDocType>;
30
+ /**
31
+ * Query helper interface for converting query results into a `Map` keyed by string.
32
+ *
33
+ * @template RawDocType - The type representing the raw document as returned by MongoDB.
34
+ * @template HydratedDocType - The type representing the hydrated Mongoose document.
35
+ *
36
+ * @remarks
37
+ * - The `toMap` method transforms an array of documents returned by a query into a `Map<string, T>`,
38
+ * where the key is typically derived from a document property (e.g., `_id` as a string).
39
+ *
40
+ * @example
41
+ * ```typescript
42
+ * const docsMap = await Model.find().toMap();
43
+ * // docsMap is a Map<string, RawDocType>
44
+ * ```
45
+ */
46
+ toMap(
47
+ this: QueryWithHelpers<RawDocType[], HydratedDocType>
48
+ ): QueryWithHelpers<Map<string, RawDocType>, HydratedDocType>;
48
49
  }
49
50
 
50
51
  /**
@@ -54,10 +55,21 @@ function toMap<T extends HydratedDocument<any>>(
54
55
  * @param schema The Mongoose schema to extend.
55
56
  */
56
57
  const toMapPlugin = <T extends HydratedDocument<any>>(
57
- schema: Schema<any, any, any, ToMapQueryHelpers<T>, any, any, any>,
58
+ schema: Schema<any, any, any, ToMapQueryHelpers<ExtractDocType<T>, T>, any, any, any>,
58
59
  ) => {
59
60
  // eslint-disable-next-line no-param-reassign
60
- schema.query.toMap = toMap;
61
+ schema.query.toMap = function (this) {
62
+ const result = this.transform((docs: any) => {
63
+ if (!docs) {
64
+ return new Map();
65
+ }
66
+ if (Array.isArray(docs)) {
67
+ return new Map(docs.map((doc: any) => [doc._id.toString(), doc]));
68
+ }
69
+ return new Map([[docs._id.toString(), docs]]);
70
+ });
71
+ return result;
72
+ };
61
73
  };
62
74
 
63
75
  export default toMapPlugin;
@@ -6,49 +6,43 @@ import { ExtractDocType } from '../types';
6
6
  *
7
7
  * @template T The type of the `HydratedDocument`.
8
8
  */
9
- export interface ToRecordsQueryHelpers<T extends HydratedDocument<any>> {
9
+ export interface ToRecordsQueryHelpers<RawDocType, HydratedDocType> {
10
10
  /**
11
- * Converts the query results into a record (object) where the key is the document's _id.
11
+ * Query helpers for transforming query results into a record (object) keyed by document IDs.
12
12
  *
13
- * @returns A QueryWithHelpers instance that returns a Record of documents.
13
+ * @template RawDocType - The type representing the raw document as returned by MongoDB.
14
+ * @template HydratedDocType - The type representing the hydrated Mongoose document.
15
+ *
16
+ * Provides overloaded `toRecords` methods for use with Mongoose queries, allowing the result
17
+ * array to be converted into a record where each key is the document's `_id` (as a string)
18
+ * and the value is the corresponding document.
19
+ *
20
+ * @example
21
+ * // Usage in a Mongoose query:
22
+ * const records = await Model.find().toRecords();
23
+ * // records is of type Record<string, HydratedDocType>
14
24
  */
15
- toRecords(): QueryWithHelpers<
16
- Record<string, T>,
17
- T,
18
- ToRecordsQueryHelpers<T>,
19
- ExtractDocType<T>
20
- >,
21
- }
22
-
23
- /**
24
- * Function to transform query results into a record (object).
25
- *
26
- * @template T The type of the `HydratedDocument`.
27
- * @this QueryWithHelpers instance.
28
- * @returns The transformed query result as a Record.
29
- */
30
- function toRecords<T extends HydratedDocument<any>>(
31
- this: QueryWithHelpers<
32
- Record<string, T>,
33
- T,
34
- ToRecordsQueryHelpers<T>,
35
- ExtractDocType<T>
36
- >,
37
- ) {
38
- const result = this.transform((docs: any) => {
39
- if (!docs) {
40
- return {};
41
- }
42
- if (Array.isArray(docs)) {
43
- return Object.fromEntries(
44
- docs.map((doc: any) => [doc._id.toString(), doc]),
45
- ) as Record<string, T>;
46
- }
47
- return {
48
- [docs._id.toString()]: docs,
49
- };
50
- });
51
- return result;
25
+ toRecords(
26
+ this: QueryWithHelpers<HydratedDocType[], HydratedDocType>
27
+ ): QueryWithHelpers<Record<string, HydratedDocType>, HydratedDocType>;
28
+ /**
29
+ * Query helpers for transforming query results into a record (object) keyed by document IDs.
30
+ *
31
+ * @template RawDocType - The type representing the raw document as returned by MongoDB.
32
+ * @template HydratedDocType - The type representing the hydrated Mongoose document.
33
+ *
34
+ * Provides overloaded `toRecords` methods for use with Mongoose queries, allowing the result
35
+ * array to be converted into a record where each key is the document's `_id` (as a string)
36
+ * and the value is the corresponding document.
37
+ *
38
+ * @example
39
+ * // Usage in a Mongoose query:
40
+ * const records = await Model.find().toRecords();
41
+ * // records is of type Record<string, RawDocType>
42
+ */
43
+ toRecords(
44
+ this: QueryWithHelpers<RawDocType[], HydratedDocType>
45
+ ): QueryWithHelpers<Record<string, RawDocType>, HydratedDocType>;
52
46
  }
53
47
 
54
48
  /**
@@ -58,10 +52,23 @@ function toRecords<T extends HydratedDocument<any>>(
58
52
  * @param schema The Mongoose schema to extend.
59
53
  */
60
54
  const toRecordsPlugin = <T extends HydratedDocument<any>>(
61
- schema: Schema<any, any, any, ToRecordsQueryHelpers<T>, any, any, any>,
55
+ schema: Schema<any, any, any, ToRecordsQueryHelpers<ExtractDocType<T>, T>, any, any, any>,
62
56
  ) => {
63
57
  // eslint-disable-next-line no-param-reassign
64
- schema.query.toRecords = toRecords;
58
+ schema.query.toRecords = function toRecords(this) {
59
+ const result = this.transform((docs: any) => {
60
+ if (!docs) {
61
+ return {};
62
+ }
63
+ if (Array.isArray(docs)) {
64
+ return Object.fromEntries(docs.map((doc: any) => [doc._id.toString(), doc]));
65
+ }
66
+ return {
67
+ [docs._id.toString()]: docs,
68
+ };
69
+ });
70
+ return result;
71
+ };
65
72
  };
66
73
 
67
74
  export default toRecordsPlugin;
@@ -39,7 +39,8 @@ export type UserHydratedDocument = HydratedDocument<IUser, IUserDocumentOverride
39
39
 
40
40
  export type UserModelType = Model<
41
41
  IUser,
42
- ToMapQueryHelpers<UserHydratedDocument> & ToRecordsQueryHelpers<UserHydratedDocument>,
42
+ ToMapQueryHelpers<IUser, UserHydratedDocument> &
43
+ ToRecordsQueryHelpers<IUser, UserHydratedDocument>,
43
44
  IUserDocumentOverrides,
44
45
  IUserVirtuals,
45
46
  UserHydratedDocument
@@ -38,25 +38,6 @@ describe('toMap', () => {
38
38
  expect(users.get(user._id.toString())!).not.toBeInstanceOf(User);
39
39
  });
40
40
 
41
- it('should correctly handle a single document', async () => {
42
- const userFound = await User
43
- .findById(user._id)
44
- .toMap();
45
- expect(userFound).toBeTruthy();
46
- expect(userFound.has(user._id.toString())).toBe(true);
47
- expect(userFound.get(user._id.toString())!).toBeInstanceOf(User);
48
- });
49
-
50
- it('should correctly handle a single document with lean', async () => {
51
- const userFound = await User
52
- .findById(user._id)
53
- .lean()
54
- .toMap();
55
- expect(userFound).toBeTruthy();
56
- expect(userFound.has(user._id.toString())).toBe(true);
57
- expect(userFound.get(user._id.toString())!).not.toBeInstanceOf(User);
58
- });
59
-
60
41
  it('should return an empty map if no documents are found', async () => {
61
42
  const users = await User
62
43
  .find({ email: 'non-existent@email.com' })
@@ -84,23 +65,6 @@ describe('toRecords', () => {
84
65
  expect(users[user._id.toString()]).not.toBeInstanceOf(User);
85
66
  });
86
67
 
87
- it('should correctly handle a single document', async () => {
88
- const userFound = await User
89
- .findById(user._id)
90
- .toRecords();
91
- expect(userFound).toBeTruthy();
92
- expect(userFound[user._id.toString()]).toBeInstanceOf(User);
93
- });
94
-
95
- it('should correctly handle a single document with lean', async () => {
96
- const userFound = await User
97
- .findById(user._id)
98
- .lean()
99
- .toRecords();
100
- expect(userFound).toBeTruthy();
101
- expect(userFound[user._id.toString()]).not.toBeInstanceOf(User);
102
- });
103
-
104
68
  it('should return an empty map if no documents are found', async () => {
105
69
  const users = await User
106
70
  .find({ email: 'non-existent@email.com' })