@@ -2,61 +2,16 @@ import {
2
2
Injectable ,
3
3
type OnModuleInit ,
4
4
Scope ,
5
- SetMetadata ,
6
5
type Type ,
7
6
} from '@nestjs/common' ;
8
7
import { ModulesContainer } from '@nestjs/core' ;
9
8
import { type DataLoaderStrategy } from '@seedcompany/data-loader' ;
10
- import type { ValueOf } from 'type-fest' ;
11
- import { many , type Many } from '~/common' ;
9
+ import { many } from '~/common' ;
12
10
import { type ResourceMap } from '~/core' ;
13
- import { ObjectViewAwareLoader } from '../data-loader' ;
14
-
15
- type SomeResource = ValueOf < ResourceMap > ;
16
-
17
- const LOADER_OF_RESOURCE = Symbol ( 'LOADER_OF_RESOURCE' ) ;
18
-
19
- interface MetadataShape extends LoaderOptions {
20
- resource ?: ( ) => Many < SomeResource > ;
21
- }
22
-
23
- interface LoaderOptions {
24
- /**
25
- * Whether the loader is aware of ObjectViews.
26
- *
27
- * This means the loader key needs to be in the shape of
28
- * {@link import('../data-loader/object-view-aware-loader.strategy').Key Key}
29
- *
30
- * This defaults to true if the class extends ObjectViewAwareLoader.
31
- */
32
- objectViewAware ?: boolean ;
33
- }
34
-
35
- type DataLoaderCtor = Type < DataLoaderStrategy < any , any > > ;
36
-
37
- /**
38
- * Register this class as a DataLoader for the given resource(s)
39
- *
40
- * @param resource Connect this loader to this resource, so it can be accessed dynamically.
41
- * @param options
42
- */
43
- export const LoaderFactory =
44
- (
45
- resource ?: ( ) => Many < SomeResource > ,
46
- options ?: LoaderOptions ,
47
- ) : ( < LoaderCtor extends DataLoaderCtor > ( target : LoaderCtor ) => void ) =>
48
- ( target ) => {
49
- Injectable ( { scope : Scope . REQUEST } ) ( target ) ;
50
-
51
- const metadata : MetadataShape = {
52
- resource,
53
- ...options ,
54
- objectViewAware :
55
- options ?. objectViewAware ??
56
- Object . getPrototypeOf ( target ) === ObjectViewAwareLoader ,
57
- } ;
58
- SetMetadata ( LOADER_OF_RESOURCE , metadata ) ( target ) ;
59
- } ;
11
+ import {
12
+ LoaderFactoryMetadata ,
13
+ type LoaderOptions ,
14
+ } from '../data-loader/loader-factory.decorator' ;
60
15
61
16
@Injectable ( )
62
17
export class ResourceLoaderRegistry implements OnModuleInit {
@@ -75,10 +30,7 @@ export class ResourceLoaderRegistry implements OnModuleInit {
75
30
if ( ! provider . metatype ) {
76
31
return [ ] ;
77
32
}
78
- const metadata = Reflect . getMetadata (
79
- LOADER_OF_RESOURCE ,
80
- provider . metatype ,
81
- ) as MetadataShape | undefined ;
33
+ const metadata = LoaderFactoryMetadata . get ( provider . metatype as Type ) ;
82
34
return metadata ? { ...metadata , provider } : [ ] ;
83
35
} ) ;
84
36
for ( const { resource, provider, ...options } of loaderFactories ) {
0 commit comments