1
1
import {
2
- deepClone ,
3
- Depth ,
4
- FetchCallback ,
5
- InvalidTypeError ,
6
- ISchema ,
7
- isNull ,
8
- isObject ,
9
- IStore ,
10
- IStoreTargetItem ,
11
- KeyMap ,
12
- NormalizedData ,
13
- NotFoundError ,
14
- TypeMismatchError ,
15
- ValidKey
2
+ deepClone , Depth , FetchCallback , InvalidTypeError , ISchema , isNull , isObject , IStore , IStoreTargetItem , KeyMap ,
3
+ NdbDocument , NormalizedData , NotFoundError , TypeMismatchError , ValidKey
16
4
} from '@normalized-db/core' ;
17
5
import { IDenormalizer } from '../denormalizer-interface' ;
18
6
@@ -42,17 +30,19 @@ export class BasicDenormalizer implements IDenormalizer {
42
30
}
43
31
}
44
32
45
- public async applyAll < T > ( type : string , data : T [ ] , depth ?: number | Depth ) : Promise < T [ ] > {
33
+ public async applyAll < T extends NdbDocument > ( type : string , data : T [ ] , depth ?: number | Depth ) : Promise < T [ ] > {
46
34
this . validateType ( type ) ;
47
35
return await this . denormalizeArray ( type , data , depth ) ;
48
36
}
49
37
50
- public async applyAllKeys < Key extends ValidKey , T > ( type : string , keys : Key [ ] , depth ?: number | Depth ) : Promise < T [ ] > {
38
+ public async applyAllKeys < Key extends ValidKey , T extends NdbDocument > ( type : string ,
39
+ keys : Key [ ] ,
40
+ depth ?: number | Depth ) : Promise < T [ ] > {
51
41
this . validateType ( type ) ;
52
42
return await Promise . all ( keys . map ( async key => await this . applyKey < Key , T > ( type , key , depth ) ) ) ;
53
43
}
54
44
55
- public async apply < T > ( type : string , data : T , depth ?: number | Depth ) : Promise < T > {
45
+ public async apply < T extends NdbDocument > ( type : string , data : T , depth ?: number | Depth ) : Promise < T > {
56
46
this . validateType ( type ) ;
57
47
58
48
if ( isObject ( data ) ) {
@@ -62,7 +52,9 @@ export class BasicDenormalizer implements IDenormalizer {
62
52
}
63
53
}
64
54
65
- public async applyKey < Key extends ValidKey , T > ( type : string , key : Key , depth ?: number | Depth ) : Promise < T > {
55
+ public async applyKey < Key extends ValidKey , T extends NdbDocument > ( type : string ,
56
+ key : Key ,
57
+ depth ?: number | Depth ) : Promise < T > {
66
58
this . validateType ( type ) ;
67
59
68
60
if ( ! this . _keys [ type ] . has ( key ) ) {
@@ -91,12 +83,12 @@ export class BasicDenormalizer implements IDenormalizer {
91
83
return await this . apply ( type , deepClone ( data ) , depth ) ;
92
84
}
93
85
94
- protected async denormalizeArray ( type : string , data : any [ ] , depth : number | Depth ) : Promise < any > {
86
+ protected async denormalizeArray ( type : string , data : NdbDocument [ ] , depth : number | Depth ) : Promise < any > {
95
87
// TODO: check for arrays, empty objects
96
88
return await Promise . all ( data . map ( item => this . denormalizeObject ( type , item , depth ) ) ) ;
97
89
}
98
90
99
- protected async denormalizeObject ( type : string , data : any , depth : number | Depth ) : Promise < any > {
91
+ protected async denormalizeObject ( type : string , data : NdbDocument , depth : number | Depth ) : Promise < any > {
100
92
const config = this . _schema . getConfig ( type ) ;
101
93
if ( ! isNull ( config . targets ) ) {
102
94
await this . denormalizeTargets ( type , data , config , depth ) ;
@@ -105,7 +97,10 @@ export class BasicDenormalizer implements IDenormalizer {
105
97
return data ;
106
98
}
107
99
108
- protected async denormalizeTargets ( type : string , data : any , config : IStore , depth : number | Depth ) : Promise < void > {
100
+ protected async denormalizeTargets ( type : string ,
101
+ data : NdbDocument ,
102
+ config : IStore ,
103
+ depth : number | Depth ) : Promise < void > {
109
104
const promises = Object . keys ( config . targets )
110
105
. filter ( field => field in data && ! isNull ( data [ field ] ) )
111
106
. map ( async field => {
@@ -167,7 +162,7 @@ export class BasicDenormalizer implements IDenormalizer {
167
162
return isArray ? result : result [ 0 ] ;
168
163
}
169
164
170
- protected applyTarget ( type : string , data : any , depth ?: number | Depth ) : Promise < any > {
165
+ protected applyTarget ( type : string , data : NdbDocument , depth ?: number | Depth ) : Promise < NdbDocument > {
171
166
return this . apply ( type , data , depth ) ;
172
167
}
173
168
0 commit comments