@gabroberge/nestjs-dataloader 1.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 +56 -0
- package/LICENSE +21 -0
- package/README.md +98 -0
- package/dist/index.d.mts +34 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.js +162 -0
- package/dist/index.mjs +135 -0
- package/package.json +60 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# @gabroberge/nestjs-dataloader
|
|
2
|
+
|
|
3
|
+
## 1.3.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- a135b75: fix workflows
|
|
8
|
+
- a135b75: upgrade dependencies
|
|
9
|
+
|
|
10
|
+
## 1.2.1
|
|
11
|
+
|
|
12
|
+
### Patch Changes
|
|
13
|
+
|
|
14
|
+
- a62cfc0: fix typings
|
|
15
|
+
|
|
16
|
+
## 1.2.0
|
|
17
|
+
|
|
18
|
+
### Minor Changes
|
|
19
|
+
|
|
20
|
+
- 57a8404: fix decorator metadata not being emitted
|
|
21
|
+
|
|
22
|
+
## 1.1.2
|
|
23
|
+
|
|
24
|
+
### Patch Changes
|
|
25
|
+
|
|
26
|
+
- 42e8273: update readme
|
|
27
|
+
|
|
28
|
+
## 1.1.1
|
|
29
|
+
|
|
30
|
+
### Patch Changes
|
|
31
|
+
|
|
32
|
+
- d41793f: update readme
|
|
33
|
+
|
|
34
|
+
## 1.1.0
|
|
35
|
+
|
|
36
|
+
### Minor Changes
|
|
37
|
+
|
|
38
|
+
- 5d1400f: add dataloader decorator
|
|
39
|
+
|
|
40
|
+
## 1.0.0
|
|
41
|
+
|
|
42
|
+
### Major Changes
|
|
43
|
+
|
|
44
|
+
- 6cfb2ae: implemented dataloader library for nestjs
|
|
45
|
+
|
|
46
|
+
## 0.0.2
|
|
47
|
+
|
|
48
|
+
### Patch Changes
|
|
49
|
+
|
|
50
|
+
- 20b4a79: update readme
|
|
51
|
+
|
|
52
|
+
## 0.0.1
|
|
53
|
+
|
|
54
|
+
### Patch Changes
|
|
55
|
+
|
|
56
|
+
- db3832c: initial commit
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Gabriel Roberge
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
# NestJS Dataloader
|
|
2
|
+
|
|
3
|
+
NestJS dataloader simplifies adding [graphql/dataloader](https://github.com/graphql/dataloader) to your NestJS project. DataLoader aims to solve the common N+1 loading problem.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Install with pnpm
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pnpm add @gabroberge/nestjs-dataloader
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Install with yarn
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
yarn add @gabroberge/nestjs-dataloader
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Install with npm
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npm install --save @gabroberge/nestjs-dataloader
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Usage
|
|
26
|
+
|
|
27
|
+
### NestDataLoader Creation
|
|
28
|
+
|
|
29
|
+
We start by extends the `NestDataLoader` abstract class. This tells `DataLoader` how to load our objects.
|
|
30
|
+
|
|
31
|
+
```typescript
|
|
32
|
+
import DataLoader from 'dataloader';
|
|
33
|
+
import { Injectable } from '@nestjs/common';
|
|
34
|
+
import { NestDataLoader } from '@gabroberge/nestjs-dataloader';
|
|
35
|
+
...
|
|
36
|
+
|
|
37
|
+
@Injectable()
|
|
38
|
+
export class AccountLoader extends NestDataLoader<string, Account> {
|
|
39
|
+
constructor(private readonly accountService: AccountService) { }
|
|
40
|
+
|
|
41
|
+
generateDataLoader(): DataLoader<string, Account> {
|
|
42
|
+
return new DataLoader<string, Account>(keys => this.accountService.findByIds(keys));
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
The first generic of the interface is the type of ID the datastore uses. The second generic is the type of object that will be returned. In the above instance, we want `DataLoader` to return instances of the `Account` class.
|
|
48
|
+
|
|
49
|
+
### Providing the NestDataLoader
|
|
50
|
+
|
|
51
|
+
For each NestDataLoader we create, we need to provide it to our module.
|
|
52
|
+
|
|
53
|
+
```typescript
|
|
54
|
+
import { Module } from '@nestjs/common';
|
|
55
|
+
import { APP_INTERCEPTOR } from '@nestjs/core';
|
|
56
|
+
import { NestDataLoaderInterceptor } from '@gabroberge/nestjs-dataloader'
|
|
57
|
+
...
|
|
58
|
+
|
|
59
|
+
@Module({
|
|
60
|
+
providers: [
|
|
61
|
+
AccountResolver,
|
|
62
|
+
AccountLoader,
|
|
63
|
+
{
|
|
64
|
+
provide: APP_INTERCEPTOR,
|
|
65
|
+
useClass: NestDataLoaderInterceptor,
|
|
66
|
+
},
|
|
67
|
+
],
|
|
68
|
+
|
|
69
|
+
})
|
|
70
|
+
export class ResolversModule { }
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### Using the NestDataLoader
|
|
74
|
+
|
|
75
|
+
Now that we have a dataloader and our module is aware of it, we need to pass it as a parameter to an endpoint in our graphQL resolver.
|
|
76
|
+
|
|
77
|
+
```typescript
|
|
78
|
+
import DataLoader from 'dataloader';
|
|
79
|
+
import { Loader } from '@gabroberge/nestjs-dataloader';
|
|
80
|
+
...
|
|
81
|
+
|
|
82
|
+
@Resolver(Account)
|
|
83
|
+
export class AccountResolver {
|
|
84
|
+
|
|
85
|
+
@Query(() => [Account])
|
|
86
|
+
public getAccounts(
|
|
87
|
+
@Args({ name: 'ids', type: () => [String] }) ids: string[],
|
|
88
|
+
@Loader(AccountLoader) accountLoader: DataLoader<Account['id'], Account>): Promise<Account[]> {
|
|
89
|
+
return accountLoader.loadMany(ids);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
The important thing to note is that the parameter of the `@Loader` decorator is the entity/class of the `NestDataLoader` we want to be injected to the method. The DataLoader library will handle bulk retrieval and caching of our requests. Note that the caching is stored on a per-request basis.
|
|
95
|
+
|
|
96
|
+
## Contributing
|
|
97
|
+
|
|
98
|
+
Pull requests are always welcome. For major changes, please open an issue first to discuss what you would like to change.
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import * as _nestjs_common from '@nestjs/common';
|
|
2
|
+
import { Type, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
|
|
3
|
+
import { ContextId, ModuleRef } from '@nestjs/core';
|
|
4
|
+
import DataLoader from 'dataloader';
|
|
5
|
+
import { Observable } from 'rxjs';
|
|
6
|
+
|
|
7
|
+
declare const NEST_LOADER_CONTEXT_KEY = "NEST_LOADER_CONTEXT_KEY";
|
|
8
|
+
|
|
9
|
+
declare abstract class NestDataLoader<Key extends string | number, Item> {
|
|
10
|
+
abstract generateDataLoader(): DataLoader<Key, Item | Item[]>;
|
|
11
|
+
mapFromArrayToArray<KeyStrategy extends (item: Item) => Key>(array: Item[], keyStrategy: KeyStrategy): Map<Key, Item[]>;
|
|
12
|
+
mapFromArrayToObject<KeyStrategy extends (item: Item) => Key>(array: Item[], keyStrategy: KeyStrategy): Map<Key, Item>;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
type LoaderContext = {
|
|
16
|
+
contextId: ContextId;
|
|
17
|
+
getLoader(data: LoaderData | string): Promise<DataLoader<any, any>>;
|
|
18
|
+
};
|
|
19
|
+
type LoaderData = Type<NestDataLoader<any, any>>;
|
|
20
|
+
type InjectionContext = {
|
|
21
|
+
[key: string]: DataLoader<any, any> | LoaderContext | undefined;
|
|
22
|
+
[NEST_LOADER_CONTEXT_KEY]?: LoaderContext;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
declare const Loader: (...dataOrPipes: (LoaderData | _nestjs_common.PipeTransform<any, any> | _nestjs_common.Type<_nestjs_common.PipeTransform<any, any>>)[]) => ParameterDecorator;
|
|
26
|
+
|
|
27
|
+
declare class NestDataLoaderInterceptor implements NestInterceptor {
|
|
28
|
+
private readonly moduleRef;
|
|
29
|
+
constructor(moduleRef: ModuleRef);
|
|
30
|
+
intercept(context: ExecutionContext, next: CallHandler): Observable<unknown>;
|
|
31
|
+
private getLoader;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export { type InjectionContext, Loader, type LoaderContext, type LoaderData, NestDataLoader, NestDataLoaderInterceptor };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import * as _nestjs_common from '@nestjs/common';
|
|
2
|
+
import { Type, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
|
|
3
|
+
import { ContextId, ModuleRef } from '@nestjs/core';
|
|
4
|
+
import DataLoader from 'dataloader';
|
|
5
|
+
import { Observable } from 'rxjs';
|
|
6
|
+
|
|
7
|
+
declare const NEST_LOADER_CONTEXT_KEY = "NEST_LOADER_CONTEXT_KEY";
|
|
8
|
+
|
|
9
|
+
declare abstract class NestDataLoader<Key extends string | number, Item> {
|
|
10
|
+
abstract generateDataLoader(): DataLoader<Key, Item | Item[]>;
|
|
11
|
+
mapFromArrayToArray<KeyStrategy extends (item: Item) => Key>(array: Item[], keyStrategy: KeyStrategy): Map<Key, Item[]>;
|
|
12
|
+
mapFromArrayToObject<KeyStrategy extends (item: Item) => Key>(array: Item[], keyStrategy: KeyStrategy): Map<Key, Item>;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
type LoaderContext = {
|
|
16
|
+
contextId: ContextId;
|
|
17
|
+
getLoader(data: LoaderData | string): Promise<DataLoader<any, any>>;
|
|
18
|
+
};
|
|
19
|
+
type LoaderData = Type<NestDataLoader<any, any>>;
|
|
20
|
+
type InjectionContext = {
|
|
21
|
+
[key: string]: DataLoader<any, any> | LoaderContext | undefined;
|
|
22
|
+
[NEST_LOADER_CONTEXT_KEY]?: LoaderContext;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
declare const Loader: (...dataOrPipes: (LoaderData | _nestjs_common.PipeTransform<any, any> | _nestjs_common.Type<_nestjs_common.PipeTransform<any, any>>)[]) => ParameterDecorator;
|
|
26
|
+
|
|
27
|
+
declare class NestDataLoaderInterceptor implements NestInterceptor {
|
|
28
|
+
private readonly moduleRef;
|
|
29
|
+
constructor(moduleRef: ModuleRef);
|
|
30
|
+
intercept(context: ExecutionContext, next: CallHandler): Observable<unknown>;
|
|
31
|
+
private getLoader;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export { type InjectionContext, Loader, type LoaderContext, type LoaderData, NestDataLoader, NestDataLoaderInterceptor };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
7
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
21
|
+
var __publicField = (obj, key, value) => {
|
|
22
|
+
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
23
|
+
return value;
|
|
24
|
+
};
|
|
25
|
+
var __async = (__this, __arguments, generator) => {
|
|
26
|
+
return new Promise((resolve, reject) => {
|
|
27
|
+
var fulfilled = (value) => {
|
|
28
|
+
try {
|
|
29
|
+
step(generator.next(value));
|
|
30
|
+
} catch (e) {
|
|
31
|
+
reject(e);
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
var rejected = (value) => {
|
|
35
|
+
try {
|
|
36
|
+
step(generator.throw(value));
|
|
37
|
+
} catch (e) {
|
|
38
|
+
reject(e);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
42
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
43
|
+
});
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
// src/index.ts
|
|
47
|
+
var src_exports = {};
|
|
48
|
+
__export(src_exports, {
|
|
49
|
+
Loader: () => Loader,
|
|
50
|
+
NestDataLoader: () => NestDataLoader,
|
|
51
|
+
NestDataLoaderInterceptor: () => NestDataLoaderInterceptor
|
|
52
|
+
});
|
|
53
|
+
module.exports = __toCommonJS(src_exports);
|
|
54
|
+
|
|
55
|
+
// src/loader.decorator.ts
|
|
56
|
+
var import_common2 = require("@nestjs/common");
|
|
57
|
+
var import_core2 = require("@nestjs/core");
|
|
58
|
+
var import_graphql2 = require("@nestjs/graphql");
|
|
59
|
+
|
|
60
|
+
// src/constants.ts
|
|
61
|
+
var NEST_LOADER_CONTEXT_KEY = "NEST_LOADER_CONTEXT_KEY";
|
|
62
|
+
|
|
63
|
+
// src/nest-loader.interceptor.ts
|
|
64
|
+
var import_common = require("@nestjs/common");
|
|
65
|
+
var import_core = require("@nestjs/core");
|
|
66
|
+
var import_graphql = require("@nestjs/graphql");
|
|
67
|
+
function _ts_decorate(decorators, target, key, desc) {
|
|
68
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
69
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
|
|
70
|
+
r = Reflect.decorate(decorators, target, key, desc);
|
|
71
|
+
else
|
|
72
|
+
for (var i = decorators.length - 1; i >= 0; i--)
|
|
73
|
+
if (d = decorators[i])
|
|
74
|
+
r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
75
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
76
|
+
}
|
|
77
|
+
__name(_ts_decorate, "_ts_decorate");
|
|
78
|
+
function _ts_metadata(k, v) {
|
|
79
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function")
|
|
80
|
+
return Reflect.metadata(k, v);
|
|
81
|
+
}
|
|
82
|
+
__name(_ts_metadata, "_ts_metadata");
|
|
83
|
+
var _NestDataLoaderInterceptor = class _NestDataLoaderInterceptor {
|
|
84
|
+
constructor(moduleRef) {
|
|
85
|
+
__publicField(this, "moduleRef");
|
|
86
|
+
this.moduleRef = moduleRef;
|
|
87
|
+
}
|
|
88
|
+
intercept(context, next) {
|
|
89
|
+
const gqlExecutionContext = import_graphql.GqlExecutionContext.create(context);
|
|
90
|
+
const injectionContext = gqlExecutionContext.getContext();
|
|
91
|
+
if (injectionContext[NEST_LOADER_CONTEXT_KEY] === void 0) {
|
|
92
|
+
injectionContext[NEST_LOADER_CONTEXT_KEY] = {
|
|
93
|
+
contextId: import_core.ContextIdFactory.create(),
|
|
94
|
+
getLoader: this.getLoader.bind(this, injectionContext)
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
return next.handle();
|
|
98
|
+
}
|
|
99
|
+
getLoader(injectionContext, type) {
|
|
100
|
+
return __async(this, null, function* () {
|
|
101
|
+
var _a;
|
|
102
|
+
if (injectionContext[type] === void 0) {
|
|
103
|
+
const nestLoader = yield this.moduleRef.resolve(type, (_a = injectionContext[NEST_LOADER_CONTEXT_KEY]) == null ? void 0 : _a.contextId, {
|
|
104
|
+
strict: false
|
|
105
|
+
});
|
|
106
|
+
injectionContext[type] = nestLoader.generateDataLoader();
|
|
107
|
+
}
|
|
108
|
+
return injectionContext[type];
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
__name(_NestDataLoaderInterceptor, "NestDataLoaderInterceptor");
|
|
113
|
+
var NestDataLoaderInterceptor = _NestDataLoaderInterceptor;
|
|
114
|
+
NestDataLoaderInterceptor = _ts_decorate([
|
|
115
|
+
(0, import_common.Injectable)(),
|
|
116
|
+
_ts_metadata("design:type", Function),
|
|
117
|
+
_ts_metadata("design:paramtypes", [
|
|
118
|
+
typeof import_core.ModuleRef === "undefined" ? Object : import_core.ModuleRef
|
|
119
|
+
])
|
|
120
|
+
], NestDataLoaderInterceptor);
|
|
121
|
+
|
|
122
|
+
// src/loader.decorator.ts
|
|
123
|
+
var Loader = (0, import_common2.createParamDecorator)((data, context) => __async(void 0, null, function* () {
|
|
124
|
+
const gqlExecutionContext = import_graphql2.GqlExecutionContext.create(context);
|
|
125
|
+
const injectionContext = gqlExecutionContext.getContext();
|
|
126
|
+
const loaderContext = injectionContext[NEST_LOADER_CONTEXT_KEY];
|
|
127
|
+
if (loaderContext !== void 0)
|
|
128
|
+
return loaderContext.getLoader(data);
|
|
129
|
+
throw new import_common2.InternalServerErrorException(`You should provide interceptor ${NestDataLoaderInterceptor.name} globally with ${import_core2.APP_INTERCEPTOR}`);
|
|
130
|
+
}));
|
|
131
|
+
|
|
132
|
+
// src/nest-dataloader.ts
|
|
133
|
+
var _NestDataLoader = class _NestDataLoader {
|
|
134
|
+
mapFromArrayToArray(array, keyStrategy) {
|
|
135
|
+
return array.reduce((map, item) => {
|
|
136
|
+
const key = keyStrategy(item);
|
|
137
|
+
const existingGroup = map.get(key);
|
|
138
|
+
if (existingGroup) {
|
|
139
|
+
existingGroup.push(item);
|
|
140
|
+
} else {
|
|
141
|
+
map.set(key, [
|
|
142
|
+
item
|
|
143
|
+
]);
|
|
144
|
+
}
|
|
145
|
+
return map;
|
|
146
|
+
}, /* @__PURE__ */ new Map());
|
|
147
|
+
}
|
|
148
|
+
mapFromArrayToObject(array, keyStrategy) {
|
|
149
|
+
return array.reduce((map, item) => {
|
|
150
|
+
map.set(keyStrategy(item), item);
|
|
151
|
+
return map;
|
|
152
|
+
}, /* @__PURE__ */ new Map());
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
__name(_NestDataLoader, "NestDataLoader");
|
|
156
|
+
var NestDataLoader = _NestDataLoader;
|
|
157
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
158
|
+
0 && (module.exports = {
|
|
159
|
+
Loader,
|
|
160
|
+
NestDataLoader,
|
|
161
|
+
NestDataLoaderInterceptor
|
|
162
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
4
|
+
var __publicField = (obj, key, value) => {
|
|
5
|
+
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
6
|
+
return value;
|
|
7
|
+
};
|
|
8
|
+
var __async = (__this, __arguments, generator) => {
|
|
9
|
+
return new Promise((resolve, reject) => {
|
|
10
|
+
var fulfilled = (value) => {
|
|
11
|
+
try {
|
|
12
|
+
step(generator.next(value));
|
|
13
|
+
} catch (e) {
|
|
14
|
+
reject(e);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
var rejected = (value) => {
|
|
18
|
+
try {
|
|
19
|
+
step(generator.throw(value));
|
|
20
|
+
} catch (e) {
|
|
21
|
+
reject(e);
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
25
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
26
|
+
});
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
// src/loader.decorator.ts
|
|
30
|
+
import { createParamDecorator, InternalServerErrorException } from "@nestjs/common";
|
|
31
|
+
import { APP_INTERCEPTOR } from "@nestjs/core";
|
|
32
|
+
import { GqlExecutionContext as GqlExecutionContext2 } from "@nestjs/graphql";
|
|
33
|
+
|
|
34
|
+
// src/constants.ts
|
|
35
|
+
var NEST_LOADER_CONTEXT_KEY = "NEST_LOADER_CONTEXT_KEY";
|
|
36
|
+
|
|
37
|
+
// src/nest-loader.interceptor.ts
|
|
38
|
+
import { Injectable } from "@nestjs/common";
|
|
39
|
+
import { ContextIdFactory, ModuleRef } from "@nestjs/core";
|
|
40
|
+
import { GqlExecutionContext } from "@nestjs/graphql";
|
|
41
|
+
function _ts_decorate(decorators, target, key, desc) {
|
|
42
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
43
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
|
|
44
|
+
r = Reflect.decorate(decorators, target, key, desc);
|
|
45
|
+
else
|
|
46
|
+
for (var i = decorators.length - 1; i >= 0; i--)
|
|
47
|
+
if (d = decorators[i])
|
|
48
|
+
r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
49
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
50
|
+
}
|
|
51
|
+
__name(_ts_decorate, "_ts_decorate");
|
|
52
|
+
function _ts_metadata(k, v) {
|
|
53
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function")
|
|
54
|
+
return Reflect.metadata(k, v);
|
|
55
|
+
}
|
|
56
|
+
__name(_ts_metadata, "_ts_metadata");
|
|
57
|
+
var _NestDataLoaderInterceptor = class _NestDataLoaderInterceptor {
|
|
58
|
+
constructor(moduleRef) {
|
|
59
|
+
__publicField(this, "moduleRef");
|
|
60
|
+
this.moduleRef = moduleRef;
|
|
61
|
+
}
|
|
62
|
+
intercept(context, next) {
|
|
63
|
+
const gqlExecutionContext = GqlExecutionContext.create(context);
|
|
64
|
+
const injectionContext = gqlExecutionContext.getContext();
|
|
65
|
+
if (injectionContext[NEST_LOADER_CONTEXT_KEY] === void 0) {
|
|
66
|
+
injectionContext[NEST_LOADER_CONTEXT_KEY] = {
|
|
67
|
+
contextId: ContextIdFactory.create(),
|
|
68
|
+
getLoader: this.getLoader.bind(this, injectionContext)
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
return next.handle();
|
|
72
|
+
}
|
|
73
|
+
getLoader(injectionContext, type) {
|
|
74
|
+
return __async(this, null, function* () {
|
|
75
|
+
var _a;
|
|
76
|
+
if (injectionContext[type] === void 0) {
|
|
77
|
+
const nestLoader = yield this.moduleRef.resolve(type, (_a = injectionContext[NEST_LOADER_CONTEXT_KEY]) == null ? void 0 : _a.contextId, {
|
|
78
|
+
strict: false
|
|
79
|
+
});
|
|
80
|
+
injectionContext[type] = nestLoader.generateDataLoader();
|
|
81
|
+
}
|
|
82
|
+
return injectionContext[type];
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
__name(_NestDataLoaderInterceptor, "NestDataLoaderInterceptor");
|
|
87
|
+
var NestDataLoaderInterceptor = _NestDataLoaderInterceptor;
|
|
88
|
+
NestDataLoaderInterceptor = _ts_decorate([
|
|
89
|
+
Injectable(),
|
|
90
|
+
_ts_metadata("design:type", Function),
|
|
91
|
+
_ts_metadata("design:paramtypes", [
|
|
92
|
+
typeof ModuleRef === "undefined" ? Object : ModuleRef
|
|
93
|
+
])
|
|
94
|
+
], NestDataLoaderInterceptor);
|
|
95
|
+
|
|
96
|
+
// src/loader.decorator.ts
|
|
97
|
+
var Loader = createParamDecorator((data, context) => __async(void 0, null, function* () {
|
|
98
|
+
const gqlExecutionContext = GqlExecutionContext2.create(context);
|
|
99
|
+
const injectionContext = gqlExecutionContext.getContext();
|
|
100
|
+
const loaderContext = injectionContext[NEST_LOADER_CONTEXT_KEY];
|
|
101
|
+
if (loaderContext !== void 0)
|
|
102
|
+
return loaderContext.getLoader(data);
|
|
103
|
+
throw new InternalServerErrorException(`You should provide interceptor ${NestDataLoaderInterceptor.name} globally with ${APP_INTERCEPTOR}`);
|
|
104
|
+
}));
|
|
105
|
+
|
|
106
|
+
// src/nest-dataloader.ts
|
|
107
|
+
var _NestDataLoader = class _NestDataLoader {
|
|
108
|
+
mapFromArrayToArray(array, keyStrategy) {
|
|
109
|
+
return array.reduce((map, item) => {
|
|
110
|
+
const key = keyStrategy(item);
|
|
111
|
+
const existingGroup = map.get(key);
|
|
112
|
+
if (existingGroup) {
|
|
113
|
+
existingGroup.push(item);
|
|
114
|
+
} else {
|
|
115
|
+
map.set(key, [
|
|
116
|
+
item
|
|
117
|
+
]);
|
|
118
|
+
}
|
|
119
|
+
return map;
|
|
120
|
+
}, /* @__PURE__ */ new Map());
|
|
121
|
+
}
|
|
122
|
+
mapFromArrayToObject(array, keyStrategy) {
|
|
123
|
+
return array.reduce((map, item) => {
|
|
124
|
+
map.set(keyStrategy(item), item);
|
|
125
|
+
return map;
|
|
126
|
+
}, /* @__PURE__ */ new Map());
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
__name(_NestDataLoader, "NestDataLoader");
|
|
130
|
+
var NestDataLoader = _NestDataLoader;
|
|
131
|
+
export {
|
|
132
|
+
Loader,
|
|
133
|
+
NestDataLoader,
|
|
134
|
+
NestDataLoaderInterceptor
|
|
135
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@gabroberge/nestjs-dataloader",
|
|
3
|
+
"version": "1.3.0",
|
|
4
|
+
"description": "A NestJS decorator for dataloader",
|
|
5
|
+
"private": false,
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"module": "./dist/index.mjs",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"lint-staged": {
|
|
10
|
+
"*.{js,ts}": [
|
|
11
|
+
"pnpm format"
|
|
12
|
+
]
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"nestjs",
|
|
16
|
+
"dataloader",
|
|
17
|
+
"graphql",
|
|
18
|
+
"typescript"
|
|
19
|
+
],
|
|
20
|
+
"author": "Gabriel Roberge",
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"repository": {
|
|
23
|
+
"type": "git",
|
|
24
|
+
"url": "https://github.com/gabroberge/nestjs-dataloader.git"
|
|
25
|
+
},
|
|
26
|
+
"homepage": "https://github.com/gabroberge/nestjs-dataloader#readme",
|
|
27
|
+
"bugs": "https://github.com/gabroberge/nestjs-dataloader/issues",
|
|
28
|
+
"peerDependencies": {
|
|
29
|
+
"@nestjs/common": "^10.0.1",
|
|
30
|
+
"@nestjs/core": "^10.0.1",
|
|
31
|
+
"@nestjs/graphql": "^12.0.1",
|
|
32
|
+
"dataloader": "^2.2.2",
|
|
33
|
+
"rxjs": "^7.8.1"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@changesets/cli": "^2.26.2",
|
|
37
|
+
"@nestjs/common": "^10.2.10",
|
|
38
|
+
"@nestjs/core": "^10.2.10",
|
|
39
|
+
"@nestjs/graphql": "^12.0.11",
|
|
40
|
+
"@nestjs/testing": "^10.2.10",
|
|
41
|
+
"@types/node": "^20.10.0",
|
|
42
|
+
"dataloader": "^2.2.2",
|
|
43
|
+
"husky": "^8.0.3",
|
|
44
|
+
"prettier": "^3.1.0",
|
|
45
|
+
"rxjs": "^7.8.1",
|
|
46
|
+
"tsup": "^8.0.1",
|
|
47
|
+
"typescript": "^5.3.2",
|
|
48
|
+
"unplugin-swc": "^1.4.3",
|
|
49
|
+
"vitest": "^0.34.6"
|
|
50
|
+
},
|
|
51
|
+
"scripts": {
|
|
52
|
+
"dev": "vitest",
|
|
53
|
+
"test": "vitest run",
|
|
54
|
+
"build": "tsup src/index.ts --format cjs,esm --dts",
|
|
55
|
+
"format": "prettier --write \"src/**/*.ts\" \"tests/**/*.spec.ts\"",
|
|
56
|
+
"lint": "tsc",
|
|
57
|
+
"ci": "pnpm lint && pnpm test && pnpm build",
|
|
58
|
+
"release": "pnpm run ci && changeset publish"
|
|
59
|
+
}
|
|
60
|
+
}
|