Skip to content

Commit 6cbf22b

Browse files
Store createdAt and updatedAt as TimeIntervalSince1970 in DynamoDB
1 parent 7d39361 commit 6cbf22b

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

Examples/LambdaFunctions/Sources/ProductAPI/Product+DynamoDB.swift

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
//===----------------------------------------------------------------------===//
1414

1515
import AWSDynamoDB
16+
import Foundation
1617

1718
extension Product {
1819

@@ -22,12 +23,12 @@ extension Product {
2223
Field.name: DynamoDB.AttributeValue(s: name),
2324
Field.description: DynamoDB.AttributeValue(s: description),
2425
]
25-
if let createdAt = createdAt {
26-
dictionary[Field.createdAt] = DynamoDB.AttributeValue(s: createdAt)
26+
if let createdAt = createdAt?.timeIntervalSince1970String {
27+
dictionary[Field.createdAt] = DynamoDB.AttributeValue(n: createdAt)
2728
}
2829

29-
if let updatedAt = updatedAt {
30-
dictionary[Field.updatedAt] = DynamoDB.AttributeValue(s: updatedAt)
30+
if let updatedAt = updatedAt?.timeIntervalSince1970String {
31+
dictionary[Field.updatedAt] = DynamoDB.AttributeValue(n: updatedAt)
3132
}
3233
return dictionary
3334
}
@@ -42,7 +43,15 @@ extension Product {
4243
self.name = name
4344
self.sku = sku
4445
self.description = description
45-
self.createdAt = dictionary[Field.createdAt]?.s
46-
self.updatedAt = dictionary[Field.updatedAt]?.s
46+
if let value = dictionary[Field.createdAt]?.n,
47+
let timeInterval = TimeInterval(value) {
48+
let date = Date(timeIntervalSince1970: timeInterval)
49+
self.createdAt = date.iso8601
50+
}
51+
if let value = dictionary[Field.updatedAt]?.n,
52+
let timeInterval = TimeInterval(value) {
53+
let date = Date(timeIntervalSince1970: timeInterval)
54+
self.updatedAt = date.iso8601
55+
}
4756
}
4857
}

Examples/LambdaFunctions/Sources/ProductAPI/ProductService.swift

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,20 @@ extension Date {
3939
}
4040
}
4141

42+
extension String {
43+
var iso8601: Date? {
44+
let formatter = DateFormatter.iso8061
45+
return formatter.date(from: self)
46+
}
47+
48+
var timeIntervalSince1970String: String? {
49+
guard let timeInterval = self.iso8601?.timeIntervalSince1970 else {
50+
return nil
51+
}
52+
return "\(timeInterval)"
53+
}
54+
}
55+
4256
public class ProductService {
4357

4458
let db: DynamoDB
@@ -78,18 +92,21 @@ public class ProductService {
7892
public func updateItem(product: Product) -> EventLoopFuture<Product> {
7993
var product = product
8094
let date = Date()
95+
let updatedAt = "\(date.timeIntervalSince1970)"
8196
product.updatedAt = date.iso8601
8297

8398
let input = DynamoDB.UpdateItemInput(
99+
conditionExpression: "attribute_exists(#createdAt)",
84100
expressionAttributeNames: [
85101
"#name": Product.Field.name,
86102
"#description": Product.Field.description,
87103
"#updatedAt": Product.Field.updatedAt,
104+
"#createdAt": Product.Field.createdAt
88105
],
89106
expressionAttributeValues: [
90107
":name": DynamoDB.AttributeValue(s: product.name),
91108
":description": DynamoDB.AttributeValue(s: product.description),
92-
":updatedAt": DynamoDB.AttributeValue(s: product.updatedAt),
109+
":updatedAt": DynamoDB.AttributeValue(n: updatedAt)
93110
],
94111
key: [Product.Field.sku: DynamoDB.AttributeValue(s: product.sku)],
95112
returnValues: DynamoDB.ReturnValue.allNew,

0 commit comments

Comments
 (0)