Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't update expires if it is nil in FluentPersistDriver.set() #28

Merged
merged 1 commit into from
Oct 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading