11import * as DynamoDB from 'aws-sdk/clients/dynamodb'
2- import { promiseTap } from '../../../helper/promise-tap.function'
32import { createLogger , Logger } from '../../../logger/logger'
43import { createKeyAttributes } from '../../../mapper/mapper'
54import { ModelConstructor } from '../../../model/model-constructor'
@@ -10,11 +9,22 @@ import { addUpdate } from '../../expression/request-expression-builder'
109import { RequestUpdateFunction } from '../../expression/type/update-expression-definition-chain'
1110import { UpdateExpressionDefinitionFunction } from '../../expression/type/update-expression-definition-function'
1211import { WriteRequest } from '../write.request'
12+ import { UpdateResponse } from './update.response'
1313
14- type UpdateRequestReturnT < T > = Omit < UpdateRequest < T > , 'exec' > & { exec ( ) : Promise < T > }
15- type UpdateRequestReturnPartialT < T > = Omit < UpdateRequest < T > , 'exec' > & { exec ( ) : Promise < Partial < T > > }
14+ type UpdateRequestReturnT < T > = Omit < Omit < UpdateRequest < T > , 'exec' > , 'execFullResponse' > & {
15+ exec ( ) : Promise < T >
16+ execFullResponse ( ) : Promise < UpdateResponse < T > >
17+ }
18+ type UpdateRequestReturnPartialT < T > = Omit < Omit < UpdateRequest < T > , 'exec' > , 'execFullResponse' > & {
19+ exec ( ) : Promise < Partial < T > >
20+ execFullResponse ( ) : Promise < UpdateResponse < T > >
21+ }
22+
23+ /**
24+ * Request class for the UpdateItem operation.
25+ */
26+ export class UpdateRequest < T > extends WriteRequest < T , DynamoDB . UpdateItemInput , DynamoDB . UpdateItemOutput , UpdateRequest < T > > {
1627
17- export class UpdateRequest < T > extends WriteRequest < T , DynamoDB . UpdateItemInput , UpdateRequest < T > > {
1828 protected readonly logger : Logger
1929
2030 constructor ( dynamoDBWrapper : DynamoDbWrapper , modelClazz : ModelConstructor < T > , partitionKey : any , sortKey ?: any ) {
@@ -23,10 +33,18 @@ export class UpdateRequest<T> extends WriteRequest<T, DynamoDB.UpdateItemInput,
2333 this . params . Key = createKeyAttributes ( this . metadata , partitionKey , sortKey )
2434 }
2535
36+ /**
37+ * create and add a single update operation
38+ * @example req.updateAttribute('path.to.attr').set('newVal')
39+ */
2640 updateAttribute < K extends keyof T > ( attributePath : K ) : RequestUpdateFunction < UpdateRequest < T > , T , K > {
2741 return addUpdate ( attributePath , this , this . metadata )
2842 }
2943
44+ /**
45+ * add multiple update operations comma separated
46+ * @example req.operations(update('path.to.attr).set('newVal'), ... )
47+ */
3048 operations ( ...updateDefFns : UpdateExpressionDefinitionFunction [ ] ) : UpdateRequest < T > {
3149 prepareAndAddUpdateExpressions ( this . metadata , this . params , updateDefFns )
3250 return this
@@ -40,20 +58,7 @@ export class UpdateRequest<T> extends WriteRequest<T, DynamoDB.UpdateItemInput,
4058 return this
4159 }
4260
43- /*
44- * kind a hacky - this is just for typing reasons so Promise<void> is the default return type when not defining a
45- * returnValues other than NONE
46- *
47- * const valueVoid = new DeleteRequest(...).exec()
48- * const valueMyModel = new DeleteRequest(...).returnValues('ALL_OLD').exec()
49- */
50- exec ( ) : Promise < void > {
51- return < Promise < void > > super . exec ( )
52- }
53-
54- execFullResponse ( ) : Promise < DynamoDB . UpdateItemOutput > {
55- this . logger . debug ( 'request' , this . params )
61+ protected doRequest ( params : DynamoDB . UpdateItemInput ) : Promise < DynamoDB . UpdateItemOutput > {
5662 return this . dynamoDBWrapper . updateItem ( this . params )
57- . then ( promiseTap ( response => this . logger . debug ( 'response' , response ) ) )
5863 }
5964}
0 commit comments