Skip to content

Commit

Permalink
feat(postgrest): use Date when filtering columns (#514)
Browse files Browse the repository at this point in the history
* Add conformance to URLQueryRepresentable for Date

* add test case

---------

Co-authored-by: Guilherme Souza <[email protected]>
  • Loading branch information
LucasAbijmil and grdsdev authored Aug 28, 2024
1 parent f6cd365 commit 1b0155c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Sources/PostgREST/URLQueryRepresentable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ extension UUID: URLQueryRepresentable {
public var queryValue: String { uuidString }
}

extension Date: URLQueryRepresentable {
public var queryValue: String {
let formatter = ISO8601DateFormatter()
formatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
return formatter.string(from: self)
}
}

extension Array: URLQueryRepresentable where Element: URLQueryRepresentable {
public var queryValue: String {
"{\(map(\.queryValue).joined(separator: ","))}"
Expand Down
5 changes: 5 additions & 0 deletions Tests/PostgRESTTests/BuildURLRequestTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,11 @@ final class BuildURLRequestTests: XCTestCase {
.select()
.eq("to", value: "+16505555555")
},
TestCase(name: "filter using Date") { client in
client.from("users")
.select()
.gt("created_at", value: Date(timeIntervalSince1970: 0))
},
]

for testCase in testCases {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
curl \
--header "Accept: application/json" \
--header "Content-Type: application/json" \
--header "X-Client-Info: postgrest-swift/x.y.z" \
"https://example.supabase.co/users?created_at=gt.1970-01-01T00:00:00.000Z&select=*"

0 comments on commit 1b0155c

Please sign in to comment.