mongoose-utility-kit 0.2.0 → 0.3.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 +6 -0
- package/dist/index.d.cts +61 -5
- package/dist/index.d.ts +61 -5
- package/package.json +2 -1
- package/src/plugins/toMap.ts +25 -3
- package/src/plugins/toRecords.ts +36 -3
- package/src/types.ts +17 -1
- package/tests/initializers/models/user.ts +5 -3
- package/tests/plugins.test.ts +22 -1
package/CHANGELOG.md
CHANGED
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.
|
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.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mongoose-utility-kit",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "A set of utility functions for the Mongoose library",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"mongoose",
|
|
@@ -42,6 +42,7 @@
|
|
|
42
42
|
"jest": "^29.7.0",
|
|
43
43
|
"mongodb-memory-server": "^10.1.4",
|
|
44
44
|
"mongoose": ">=8",
|
|
45
|
+
"mongoose-lean-virtuals": "^2.0.0",
|
|
45
46
|
"ts-jest": "^29.2.5",
|
|
46
47
|
"tsup": "^8.3.6",
|
|
47
48
|
"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) {
|
package/src/types.ts
CHANGED
|
@@ -7,4 +7,20 @@ import { HydratedDocument } from 'mongoose';
|
|
|
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
|
-
export type
|
|
10
|
+
export type ExtractRawDocType<T> = T extends HydratedDocument<infer U> ? U : never;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Extracts the virtuals type from a Mongoose `HydratedDocument`.
|
|
14
|
+
* This utility type is useful for accessing the shape of virtual properties
|
|
15
|
+
* defined on Mongoose schemas.
|
|
16
|
+
*
|
|
17
|
+
* @template T The type of the `HydratedDocument`.
|
|
18
|
+
* @returns The type of the virtuals if `T` is a `HydratedDocument`, otherwise `never`.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* type Virtuals = ExtractVirtualsType<HydratedDocument<MyDoc, MyVirtuals>>;
|
|
23
|
+
* // Virtuals is inferred as MyVirtuals
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export type ExtractVirtualsType<T> = T extends HydratedDocument<any, infer U> ? U : never;
|
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
HydratedDocument,
|
|
8
8
|
model,
|
|
9
9
|
} from 'mongoose';
|
|
10
|
+
import mongooseLeanVirtuals from 'mongoose-lean-virtuals';
|
|
10
11
|
import addressSchema, { AddressInstance, IAddress } from './embedded/address';
|
|
11
12
|
import {
|
|
12
13
|
toMap,
|
|
@@ -30,7 +31,7 @@ interface IUserDocumentOverrides {
|
|
|
30
31
|
address: AddressInstance;
|
|
31
32
|
}
|
|
32
33
|
|
|
33
|
-
interface IUserVirtuals {
|
|
34
|
+
export interface IUserVirtuals {
|
|
34
35
|
id: string;
|
|
35
36
|
fullName: string;
|
|
36
37
|
}
|
|
@@ -39,8 +40,8 @@ export type UserHydratedDocument = HydratedDocument<IUser, IUserDocumentOverride
|
|
|
39
40
|
|
|
40
41
|
export type UserModelType = Model<
|
|
41
42
|
IUser,
|
|
42
|
-
ToMapQueryHelpers<IUser, UserHydratedDocument> &
|
|
43
|
-
ToRecordsQueryHelpers<IUser, UserHydratedDocument>,
|
|
43
|
+
ToMapQueryHelpers<IUser, UserHydratedDocument, IUserVirtuals> &
|
|
44
|
+
ToRecordsQueryHelpers<IUser, UserHydratedDocument, IUserVirtuals>,
|
|
44
45
|
IUserDocumentOverrides,
|
|
45
46
|
IUserVirtuals,
|
|
46
47
|
UserHydratedDocument
|
|
@@ -80,6 +81,7 @@ userSchema.virtual('fullName')
|
|
|
80
81
|
return `${this.name} ${this.surname}`;
|
|
81
82
|
});
|
|
82
83
|
|
|
84
|
+
userSchema.plugin(mongooseLeanVirtuals);
|
|
83
85
|
userSchema.plugin(toMap);
|
|
84
86
|
userSchema.plugin(toRecords);
|
|
85
87
|
|
package/tests/plugins.test.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import User, { UserHydratedDocument } from './initializers/models/user';
|
|
1
|
+
import User, { IUser, IUserVirtuals, UserHydratedDocument } from './initializers/models/user';
|
|
2
2
|
|
|
3
3
|
let user: UserHydratedDocument;
|
|
4
4
|
|
|
@@ -38,6 +38,17 @@ describe('toMap', () => {
|
|
|
38
38
|
expect(users.get(user._id.toString())!).not.toBeInstanceOf(User);
|
|
39
39
|
});
|
|
40
40
|
|
|
41
|
+
it('should return a map with lean documents and virtuals', async () => {
|
|
42
|
+
const users = await User
|
|
43
|
+
.find()
|
|
44
|
+
.lean<(IUser & IUserVirtuals)[]>({ virtuals: true })
|
|
45
|
+
.toMap();
|
|
46
|
+
expect(users).toBeTruthy();
|
|
47
|
+
expect(users.has(user._id.toString())).toBe(true);
|
|
48
|
+
expect(users.get(user._id.toString())!.fullName).toBeTruthy();
|
|
49
|
+
expect(users.get(user._id.toString())!).not.toBeInstanceOf(User);
|
|
50
|
+
});
|
|
51
|
+
|
|
41
52
|
it('should return an empty map if no documents are found', async () => {
|
|
42
53
|
const users = await User
|
|
43
54
|
.find({ email: 'non-existent@email.com' })
|
|
@@ -65,6 +76,16 @@ describe('toRecords', () => {
|
|
|
65
76
|
expect(users[user._id.toString()]).not.toBeInstanceOf(User);
|
|
66
77
|
});
|
|
67
78
|
|
|
79
|
+
it('should return a map with lean documents and virtuals', async () => {
|
|
80
|
+
const users = await User
|
|
81
|
+
.find()
|
|
82
|
+
.lean<(IUser & IUserVirtuals)[]>({ virtuals: true })
|
|
83
|
+
.toRecords();
|
|
84
|
+
expect(users).toBeTruthy();
|
|
85
|
+
expect(users[user._id.toString()].fullName).toBeTruthy();
|
|
86
|
+
expect(users[user._id.toString()]).not.toBeInstanceOf(User);
|
|
87
|
+
});
|
|
88
|
+
|
|
68
89
|
it('should return an empty map if no documents are found', async () => {
|
|
69
90
|
const users = await User
|
|
70
91
|
.find({ email: 'non-existent@email.com' })
|