Skip to content

Commit

Permalink
Add member to return instance adding timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
mgacy committed May 10, 2024
1 parent 945437d commit 6a181fd
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Sources/Persistence/Persistence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 6a181fd

Please sign in to comment.