-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.js
44 lines (37 loc) · 963 Bytes
/
helpers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { util } from '@aws-appsync/utils';
export function updateItem(item) {
return {
updatedAt: util.time.nowISO8601(),
};
}
export function createItem(item) {
return {
...item,
createdAt: util.time.nowISO8601(),
updatedAt: util.time.nowISO8601(),
};
}
export function validateUser(user) {
if (user.age < 18) {
util.appendError('User must be at least 18 years old');
}
}
export function generateUpdateExpressions(item) {
const updateItem = {
...item,
updatedAt: util.time.nowISO8601(),
}
const updateExpression = [];
const expressionNames = {};
const expressionValues = {};
for (const [key, value] of Object.entries(updateItem)) {
updateExpression.push(`#${key} = :${key}`);
expressionNames[`#${key}`] = key;
expressionValues[`:${key}`] = util.dynamodb.toDynamoDB(value);
}
return {
expression: `set ${updateExpression.join(', ')}`,
expressionNames,
expressionValues,
}
}