Skip to content

Commit

Permalink
Don't update expires if it is nil in FluentPersistDriver.set() (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-fowler authored Oct 14, 2024
1 parent 4e80bf8 commit 24ebbb5
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions Sources/HummingbirdFluent/Persist+fluent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ public final class FluentPersistDriver: PersistDriver {
public func set(key: String, value: some Codable, expires: Duration?) async throws {
let db = self.fluent.db(self.databaseID)
let data = try JSONEncoder().encode(value)
let date = expires.map { Date.now + Double($0.components.seconds) } ?? Date.distantFuture
let model = PersistModel(id: key, data: data, expires: date)
let date = expires.map { Date.now + Double($0.components.seconds) }
let model = PersistModel(id: key, data: data, expires: date ?? Date.distantFuture)
do {
try await model.save(on: db)
} catch let error as DatabaseError where error.isConstraintFailure {
Expand All @@ -68,10 +68,12 @@ public final class FluentPersistDriver: PersistDriver {
.first()
if let model {
model.data = data
model.expires = date
if let date {
model.expires = date
}
try await model.update(on: db)
} else {
let model = PersistModel(id: key, data: data, expires: date)
let model = PersistModel(id: key, data: data, expires: date ?? Date.distantFuture)
try await model.save(on: db)
}
} catch {
Expand Down

0 comments on commit 24ebbb5

Please sign in to comment.