2
2
import { noSqlClient } from "../dal/client/nosql" ;
3
3
4
4
// TODO: Import from @topcoder -framework/lib-common
5
- import {
6
- LookupCriteria ,
7
- ScanCriteria ,
8
- ScanResult ,
9
- } from "../models/common/common" ;
5
+ import { LookupCriteria , ScanCriteria , ScanResult } from "../models/common/common" ;
10
6
11
7
// TODO: Import from @topcoder -framework/lib-common
12
8
import { Value } from "../models/google/protobuf/struct" ;
@@ -25,7 +21,7 @@ import {
25
21
UpdateType ,
26
22
Value as PartiQLValue ,
27
23
} from "../dal/models/nosql/parti_ql" ;
28
- import { StatusBuilder } from "@grpc/grpc-js" ;
24
+ import { Metadata , StatusBuilder } from "@grpc/grpc-js" ;
29
25
import { Status } from "@grpc/grpc-js/build/src/constants" ;
30
26
31
27
export type ValueType =
@@ -44,24 +40,20 @@ export type DynamoTableIndex = {
44
40
} ;
45
41
} ;
46
42
47
- abstract class CoreOperations <
48
- T extends { [ key : string ] : any } ,
49
- I extends { [ key : string ] : any }
50
- > {
43
+ abstract class CoreOperations < T extends { [ key : string ] : any } , I extends { [ key : string ] : any } > {
51
44
public constructor (
52
45
private entityName : string ,
53
46
private entityAttributes : Attribute [ ] ,
54
47
private entityIndexList : DynamoTableIndex
55
48
) { }
56
49
57
- private attributesKeyTypeMap : { [ key : string ] : DataType } =
58
- this . entityAttributes . reduce (
59
- ( map , attribute ) => ( {
60
- ...map ,
61
- [ attribute . name ] : attribute . type ,
62
- } ) ,
63
- { }
64
- ) ;
50
+ private attributesKeyTypeMap : { [ key : string ] : DataType } = this . entityAttributes . reduce (
51
+ ( map , attribute ) => ( {
52
+ ...map ,
53
+ [ attribute . name ] : attribute . type ,
54
+ } ) ,
55
+ { }
56
+ ) ;
65
57
66
58
public async lookup ( lookupCriteria : LookupCriteria ) : Promise < T > {
67
59
const selectQuery : SelectQuery = {
@@ -150,7 +142,7 @@ abstract class CoreOperations<
150
142
} ;
151
143
}
152
144
153
- protected async create ( entity : I & T ) : Promise < T > {
145
+ protected async create ( entity : I & T , metadata ?: Metadata ) : Promise < T > {
154
146
const queryRequest : QueryRequest = {
155
147
kind : {
156
148
$case : "query" ,
@@ -177,7 +169,8 @@ abstract class CoreOperations<
177
169
178
170
public async update (
179
171
scanCriteria : ScanCriteria [ ] ,
180
- entity : unknown
172
+ entity : unknown ,
173
+ metadata ?: Metadata
181
174
) : Promise < { items : T [ ] } > {
182
175
if ( typeof entity != "object" || entity == null ) {
183
176
throw new Error ( "Expected key-value pairs to update" ) ;
@@ -193,12 +186,14 @@ abstract class CoreOperations<
193
186
update : {
194
187
table : this . entityName ,
195
188
// TODO: Write a convenience method in @topcoder -framework/lib-common to support additional update operations like LIST_APPEND, SET_ADD, SET_REMOVE, etc
196
- updates : Object . entries ( entity ) . map ( ( [ key , value ] ) => ( {
197
- action : UpdateAction . UPDATE_ACTION_SET ,
198
- type : UpdateType . UPDATE_TYPE_VALUE ,
199
- attribute : key ,
200
- value : this . toValue ( key , value ) ,
201
- } ) ) ,
189
+ updates : Object . entries ( entity )
190
+ . filter ( ( [ key , value ] ) => value !== undefined )
191
+ . map ( ( [ key , value ] ) => ( {
192
+ action : UpdateAction . UPDATE_ACTION_SET ,
193
+ type : UpdateType . UPDATE_TYPE_VALUE ,
194
+ attribute : key ,
195
+ value : this . toValue ( key , value ) ,
196
+ } ) ) ,
202
197
filters,
203
198
returnValue : ReturnValue . RETURN_VALUE_ALL_NEW ,
204
199
} ,
@@ -330,9 +325,7 @@ abstract class CoreOperations<
330
325
break ;
331
326
332
327
default :
333
- throw new Error (
334
- "Lookups are only supported for string, number & boolean value"
335
- ) ;
328
+ throw new Error ( "Lookups are only supported for string, number & boolean value" ) ;
336
329
}
337
330
338
331
return value ;
0 commit comments