Skip to content

Commit e63075f

Browse files
committed
improve type safety
1 parent 2103e1c commit e63075f

3 files changed

Lines changed: 62 additions & 10 deletions

File tree

src/transactions/index.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,7 @@ export class Transaction {
110110

111111
const itemKey = this.getItemKey(item, args?.TableName);
112112

113-
const actions: UpdateAction[] = [
114-
{ _type: "set", values: { updatedAt: this.updatedAt } },
115-
];
113+
const actions: UpdateAction[] = [];
116114

117115
const existingOperation = this.operations[itemKey];
118116
if (existingOperation && existingOperation._type === "update") {
@@ -195,11 +193,17 @@ export class Transaction {
195193
ExpressionAttributeNames
196194
);
197195

196+
let hasSetUpdatedAt = false;
197+
198198
actions.forEach((action) => {
199199
switch (action._type) {
200200
case "set":
201201
attr.appendBoth(action.values);
202202

203+
if ("updatedAt" in action.values) {
204+
hasSetUpdatedAt = true;
205+
}
206+
203207
expressions.set.push(
204208
Object.keys(action.values)
205209
.map((key) => `${attr.getName(key)} = ${attr.getValue(key)}`)
@@ -209,13 +213,21 @@ export class Transaction {
209213
case "remove":
210214
attr.appendNames(action.attributes);
211215

216+
if (action.attributes.includes("updatedAt")) {
217+
hasSetUpdatedAt = true;
218+
}
219+
212220
expressions.remove.push(
213221
action.attributes.map((key) => attr.getName(key)).join(", ")
214222
);
215223
break;
216224
case "add":
217225
attr.appendBoth(action.values);
218226

227+
if ("updatedAt" in action.values) {
228+
hasSetUpdatedAt = true;
229+
}
230+
219231
expressions.add.push(
220232
Object.keys(action.values)
221233
.map((key) => `${attr.getName(key)} ${attr.getValue(key)}`)
@@ -225,6 +237,10 @@ export class Transaction {
225237
case "delete":
226238
attr.appendBoth(action.values);
227239

240+
if ("updatedAt" in action.values) {
241+
hasSetUpdatedAt = true;
242+
}
243+
228244
expressions.delete.push(
229245
Object.keys(action.values)
230246
.map((key) => `${attr.getName(key)} ${attr.getValue(key)}`)
@@ -234,6 +250,14 @@ export class Transaction {
234250
}
235251
});
236252

253+
if (!hasSetUpdatedAt) {
254+
attr.appendBoth({ updatedAt: this.updatedAt });
255+
256+
expressions.set.push(
257+
`${attr.getName("updatedAt")} = ${attr.getValue("updatedAt")}`
258+
);
259+
}
260+
237261
const joinedExpressions = Object.entries(expressions)
238262
.filter(([, value]) => value?.length)
239263
.reduce(

src/transactions/operations.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,18 @@ export class UpdateOperations<U extends DynamoDBItem> {
107107
this.operation.actions.push({ _type: "add", values: values as any });
108108
return this;
109109
}
110-
removeAttributes(...attributes: string[]) {
110+
removeAttributes(
111+
...attributes: Array<
112+
| Exclude<Extract<keyof U, string>, keyof DynamoDBItemKey>
113+
| `${string}.${string}`
114+
| `${string}.${string}.${string}`
115+
>
116+
): UpdateOperations<U> {
111117
if (attributes.length === 0) return this;
112-
this.operation.actions.push({ _type: "remove", attributes });
118+
this.operation.actions.push({
119+
_type: "remove",
120+
attributes: attributes.map(String),
121+
});
113122
return this;
114123
}
115124

test/types.ts

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,30 @@ async function transactionPlayground() {
152152
SK: string;
153153
stars: number;
154154
otherProp: string;
155+
nested: {
156+
params: number;
157+
};
155158
};
156159

157-
const res = await new Transaction()
158-
.update<TxnItem>({ PK: "User/1", SK: "Book/1" })
159-
.set({ otherProp: "test" })
160-
.adjustNumber({ stars: 1 })
161-
.execute();
160+
const item: TxnItem = {
161+
PK: "User/1",
162+
SK: "Book/1",
163+
stars: 0,
164+
otherProp: "test",
165+
nested: {
166+
params: 1,
167+
},
168+
};
169+
170+
await putItem(item);
171+
172+
const txn = new Transaction();
173+
txn.update<TxnItem>(item).set({ otherProp: "test" });
174+
txn.update<TxnItem>(item).removeAttributes("nested");
175+
await txn.execute();
176+
177+
const updatedItem = await getItem<TxnItem>(item);
178+
console.log(updatedItem);
162179
}
180+
181+
transactionPlayground();

0 commit comments

Comments
 (0)