From 6a181fd580bc9c6a0c43edc5f0321c4a820fe7a1 Mon Sep 17 00:00:00 2001 From: Matt Gacy Date: Fri, 10 May 2024 10:04:24 -0700 Subject: [PATCH] Add member to return instance adding timestamp --- Sources/Persistence/Persistence.swift | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/Sources/Persistence/Persistence.swift b/Sources/Persistence/Persistence.swift index e642774..4818928 100644 --- a/Sources/Persistence/Persistence.swift +++ b/Sources/Persistence/Persistence.swift @@ -46,6 +46,24 @@ public struct Persistence { } } +public extension Persistence { + /// Returns an instance that adds a `CreatedAt` timestamp from the given `TimeStampProvider` to + /// all persisted entities. + /// + /// - Parameters: + /// - timestampProvider: A timestamp provider. + /// - put: A closure to persist item attributes. + /// - Returns: A `Persistence` instance. + static func addingTimestamp( + from timestampProvider: TimestampProvider, + put: @escaping @Sendable ([String: AttributeValue]) async throws -> Void + ) -> Self { + .init( + put: put, + attributeModifier: { $0.merging(["CreatedAt": .s(timestampProvider.timestamp())]) { _, new in new } }) + } +} + /// A type that creates ``Persistence`` instances. public struct PersistenceFactory { /// The region where the table is located.