Skip to content

Commit

Permalink
adding update call to dynamodb library
Browse files Browse the repository at this point in the history
  • Loading branch information
marciocadev committed Jul 7, 2024
1 parent d8e60e6 commit c359db6
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 0 deletions.
6 changes: 6 additions & 0 deletions dynamodb/dynamodb-client.w
Original file line number Diff line number Diff line change
Expand Up @@ -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<Json> [];
for item in options.TransactItems {
Expand Down
12 changes: 12 additions & 0 deletions dynamodb/dynamodb-types.w
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,20 @@ pub struct PutOptions {
ReturnValues: str?;
}

pub struct UpdateOptions {
Key: Json;
ReturnValues: str?;
ReturnConsumedCapacity: str?;
UpdateExpression: str;
ConditionExpression: str?;
ExpressionAttributeNames: Map<str>?;
ExpressionAttributeValues: Map<Json>?;
}

pub struct PutOutput {
Attributes: Json?;
}
pub struct UpdateOutput extends PutOptions {}

pub struct QueryOptions {
ConsistentRead: bool?;
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions dynamodb/dynamodb.sim.w
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
4 changes: 4 additions & 0 deletions dynamodb/dynamodb.tf-aws.w
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
4 changes: 4 additions & 0 deletions dynamodb/dynamodb.w
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
42 changes: 42 additions & 0 deletions dynamodb/tests/update.test.w
Original file line number Diff line number Diff line change
@@ -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");
}

0 comments on commit c359db6

Please sign in to comment.