From c359db6bca60c31bf388b13841a6bc17c4c347ef Mon Sep 17 00:00:00 2001 From: Marcio Cruz de Almeida Date: Sun, 7 Jul 2024 14:25:01 -0300 Subject: [PATCH] adding update call to dynamodb library --- dynamodb/dynamodb-client.w | 6 ++++++ dynamodb/dynamodb-types.w | 12 +++++++++++ dynamodb/dynamodb.sim.w | 4 ++++ dynamodb/dynamodb.tf-aws.w | 4 ++++ dynamodb/dynamodb.w | 4 ++++ dynamodb/tests/update.test.w | 42 ++++++++++++++++++++++++++++++++++++ 6 files changed, 72 insertions(+) create mode 100644 dynamodb/tests/update.test.w diff --git a/dynamodb/dynamodb-client.w b/dynamodb/dynamodb-client.w index 50a0bd3f..bbc2b998 100644 --- a/dynamodb/dynamodb-client.w +++ b/dynamodb/dynamodb-client.w @@ -61,6 +61,12 @@ pub inflight class Client impl dynamodb_types.IClient { return unsafeCast(this.client.put(input)); } + pub inflight update(options: dynamodb_types.UpdateOptions): dynamodb_types.UpdateOutput { + let input: MutJson = options; + input.set("TableName", this.tableName); + return unsafeCast(this.client.update(input)); + } + pub inflight transactWrite(options: dynamodb_types.TransactWriteOptions): dynamodb_types.TransactWriteOutput { let transactItems = MutArray []; for item in options.TransactItems { diff --git a/dynamodb/dynamodb-types.w b/dynamodb/dynamodb-types.w index a3d2428a..efb417c2 100644 --- a/dynamodb/dynamodb-types.w +++ b/dynamodb/dynamodb-types.w @@ -46,9 +46,20 @@ pub struct PutOptions { ReturnValues: str?; } +pub struct UpdateOptions { + Key: Json; + ReturnValues: str?; + ReturnConsumedCapacity: str?; + UpdateExpression: str; + ConditionExpression: str?; + ExpressionAttributeNames: Map?; + ExpressionAttributeValues: Map?; +} + pub struct PutOutput { Attributes: Json?; } +pub struct UpdateOutput extends PutOptions {} pub struct QueryOptions { ConsistentRead: bool?; @@ -201,6 +212,7 @@ pub inflight interface IClient { inflight delete(options: DeleteOptions): DeleteOutput; inflight get(options: GetOptions): GetOutput; inflight put(options: PutOptions): PutOutput; + inflight update(options: UpdateOptions): UpdateOutput; inflight query(options: QueryOptions): QueryOutput; inflight scan(options: ScanOptions?): ScanOutput; inflight transactWrite(options: TransactWriteOptions): TransactWriteOutput; diff --git a/dynamodb/dynamodb.sim.w b/dynamodb/dynamodb.sim.w index 89b2b4da..260582bc 100644 --- a/dynamodb/dynamodb.sim.w +++ b/dynamodb/dynamodb.sim.w @@ -275,6 +275,10 @@ pub class Table_sim impl dynamodb_types.ITable { return this.client.put(options); } + pub inflight update(options: dynamodb_types.UpdateOptions): dynamodb_types.UpdateOutput { + return this.client.update(options); + } + pub inflight transactWrite(options: dynamodb_types.TransactWriteOptions): dynamodb_types.TransactWriteOutput { return this.client.transactWrite(options); } diff --git a/dynamodb/dynamodb.tf-aws.w b/dynamodb/dynamodb.tf-aws.w index 3b7794a4..b12d48f7 100644 --- a/dynamodb/dynamodb.tf-aws.w +++ b/dynamodb/dynamodb.tf-aws.w @@ -184,6 +184,10 @@ pub class Table_tfaws impl dynamodb_types.ITable { return this.client.put(options); } + pub inflight update(options: dynamodb_types.UpdateOptions): dynamodb_types.UpdateOutput { + return this.client.update(options); + } + pub inflight transactWrite(options: dynamodb_types.TransactWriteOptions): dynamodb_types.TransactWriteOutput { return this.client.transactWrite(options); } diff --git a/dynamodb/dynamodb.w b/dynamodb/dynamodb.w index deba8fbd..62adefd5 100644 --- a/dynamodb/dynamodb.w +++ b/dynamodb/dynamodb.w @@ -61,6 +61,10 @@ pub class Table impl dynamodb_types.ITable { return this.implementation.put(options); } + pub inflight update(options: dynamodb_types.UpdateOptions): dynamodb_types.UpdateOutput { + return this.implementation.update(options); + } + pub inflight transactWrite(options: dynamodb_types.TransactWriteOptions): dynamodb_types.TransactWriteOutput { return this.implementation.transactWrite(options); } diff --git a/dynamodb/tests/update.test.w b/dynamodb/tests/update.test.w new file mode 100644 index 00000000..cb8eefc8 --- /dev/null +++ b/dynamodb/tests/update.test.w @@ -0,0 +1,42 @@ +bring cloud; +bring "../dynamodb.w" as dynamodb; + +let table = new dynamodb.Table( + attributes: [ + { + name: "id", + type: "S", + }, + ], + hashKey: "id", +) as "table1"; + +test "update" { + table.put( + Item: { + id: "1", + val1: "anything", + val2: "something" + }, + ); + + let var response = table.query( + KeyConditionExpression: "id = :id", + ExpressionAttributeValues: {":id": "1"}, + ); + assert(response.Items[0]["val1"].asStr() == "anything"); + assert(response.Items[0]["val2"].asStr() == "something"); + + table.update( + Key: { id: "1" }, + UpdateExpression: "SET val2 = :v", + ExpressionAttributeValues: {":v": "everything"} + ); + + response = table.query( + KeyConditionExpression: "id = :id", + ExpressionAttributeValues: {":id": "1"}, + ); + assert(response.Items[0]["val1"].asStr() == "anything"); + assert(response.Items[0]["val2"].asStr() == "everything"); +} \ No newline at end of file