@@ -27,15 +27,22 @@ class DynamoDBProvider extends BaseProvider {
27
27
if ( config . valueAttr ) this . valueAttr = config . valueAttr ;
28
28
}
29
29
30
+ public async get ( name : string , options ?: DynamoDBGetOptionsInterface ) : Promise < undefined | string | Record < string , unknown > > {
31
+ return super . get ( name , options ) ;
32
+ }
33
+
34
+ public async getMultiple ( path : string , options ?: DynamoDBGetMultipleOptionsInterface ) : Promise < undefined | Record < string , unknown > > {
35
+ return super . getMultiple ( path , options ) ;
36
+ }
37
+
30
38
protected async _get ( name : string , options ?: DynamoDBGetOptionsInterface ) : Promise < string | undefined > {
31
39
const sdkOptions : GetItemCommandInput = {
32
40
TableName : this . tableName ,
33
41
Key : marshall ( { [ this . keyAttr ] : name } ) ,
42
+ ProjectionExpression : this . valueAttr ,
34
43
} ;
35
- if ( options && options . hasOwnProperty ( 'sdkOptions' ) ) {
36
- // Explicit arguments passed to the constructor will take precedence over ones passed to the method
37
- delete options . sdkOptions ?. Key ;
38
- // TODO: check if TableName is overridable
44
+ if ( options && options . sdkOptions ) {
45
+ this . removeNonOverridableOptions ( options . sdkOptions as GetItemCommandInput ) ;
39
46
Object . assign ( sdkOptions , options . sdkOptions ) ;
40
47
}
41
48
const result = await this . client . send ( new GetItemCommand ( sdkOptions ) ) ;
@@ -48,14 +55,13 @@ class DynamoDBProvider extends BaseProvider {
48
55
TableName : this . tableName ,
49
56
KeyConditionExpression : `${ this . keyAttr } = :key` ,
50
57
ExpressionAttributeValues : marshall ( { ':key' : path } ) ,
58
+ ProjectionExpression : `${ this . sortAttr } , ${ this . valueAttr } ` ,
51
59
} ;
52
60
const paginationOptions : PaginationConfiguration = {
53
61
client : this . client ,
54
62
} ;
55
- if ( options && options . hasOwnProperty ( 'sdkOptions' ) ) {
56
- // Explicit arguments passed to the constructor will take precedence over ones passed to the method
57
- delete options . sdkOptions ?. KeyConditionExpression ;
58
- delete options . sdkOptions ?. ExpressionAttributeValues ;
63
+ if ( options && options . sdkOptions ) {
64
+ this . removeNonOverridableOptions ( options . sdkOptions as QueryCommandInput ) ;
59
65
if ( options . sdkOptions ?. hasOwnProperty ( 'Limit' ) ) {
60
66
paginationOptions . pageSize = options . sdkOptions . Limit ;
61
67
}
@@ -66,12 +72,43 @@ class DynamoDBProvider extends BaseProvider {
66
72
for await ( const page of paginateQuery ( paginationOptions , sdkOptions ) ) {
67
73
for ( const item of page . Items || [ ] ) {
68
74
const unmarshalledItem = unmarshall ( item ) ;
69
- parameters [ unmarshalledItem [ this . keyAttr ] ] = unmarshalledItem [ this . valueAttr ] ;
75
+ parameters [ unmarshalledItem [ this . sortAttr ] ] = unmarshalledItem [ this . valueAttr ] ;
70
76
}
71
77
}
72
78
73
79
return parameters ;
74
80
}
81
+
82
+ /**
83
+ * This method is used as a type guard to narrow down the type of the options object.
84
+ */
85
+ protected isGetItemCommandInput ( options : GetItemCommandInput | QueryCommandInput ) : options is GetItemCommandInput {
86
+ return ( options as GetItemCommandInput ) . Key !== undefined ;
87
+ }
88
+
89
+ /**
90
+ * Explicit arguments passed to the constructor will take precedence over ones passed to the method.
91
+ * For users who consume the library with TypeScript, this will be enforced by the type system. However,
92
+ * for JavaScript users, we need to manually delete the properties that are not allowed to be overridden.
93
+ */
94
+ protected removeNonOverridableOptions ( options : GetItemCommandInput | QueryCommandInput ) : void {
95
+ if ( options . hasOwnProperty ( 'TableName' ) ) {
96
+ delete options . TableName ;
97
+ }
98
+ if ( options . hasOwnProperty ( 'ProjectionExpression' ) ) {
99
+ delete options . ProjectionExpression ;
100
+ }
101
+ if ( options . hasOwnProperty ( 'Key' ) && this . isGetItemCommandInput ( options ) ) {
102
+ delete options . Key ;
103
+ } else if ( options . hasOwnProperty ( 'KeyConditionExpression' ) && ! this . isGetItemCommandInput ( options ) ) {
104
+ if ( options . hasOwnProperty ( 'KeyConditionExpression' ) ) {
105
+ delete options . KeyConditionExpression ;
106
+ }
107
+ if ( options . hasOwnProperty ( 'ExpressionAttributeValues' ) ) {
108
+ delete options . ExpressionAttributeValues ;
109
+ }
110
+ }
111
+ }
75
112
}
76
113
77
114
export {
0 commit comments