Skip to content

Commit bd7dcf8

Browse files
Add return=minimal option, Fix content-type bug (#19)
1 parent 20986b6 commit bd7dcf8

File tree

3 files changed

+23
-13
lines changed

3 files changed

+23
-13
lines changed

README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,11 @@ semaphore.wait()
7979

8080
## Contributing
8181

82-
- Fork the repo on GitHub
83-
- Clone the project to your own machine
84-
- Commit changes to your own branch
85-
- Push your work back up to your fork
86-
- Submit a Pull request so that we can review your changes and merge
82+
- Fork the repo on GitHub
83+
- Clone the project to your own machine
84+
- Commit changes to your own branch
85+
- Push your work back up to your fork
86+
- Submit a Pull request so that we can review your changes and merge
8787

8888
## License
8989

Sources/PostgREST/PostgrestQueryBuilder.swift

+8-8
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ public class PostgrestQueryBuilder: PostgrestBuilder {
1818
)
1919
}
2020

21-
public func insert(values: Any, upsert: Bool = false, onConflict: String? = nil) -> PostgrestBuilder {
21+
public func insert(values: Any, upsert: Bool = false, onConflict: String? = nil, returning: PostgrestReturningOptions = .representation) -> PostgrestBuilder {
2222
method = "POST"
23-
headers["Prefer"] = upsert ? "return=representation,resolution=merge-duplicates" : "return=representation"
23+
headers["Prefer"] = upsert ? "return=\(returning.rawValue),resolution=merge-duplicates" : "return=\(returning.rawValue)"
2424
if let onConflict = onConflict {
2525
appendSearchParams(name: "on_conflict", value: onConflict)
2626
}
@@ -29,9 +29,9 @@ public class PostgrestQueryBuilder: PostgrestBuilder {
2929
return self
3030
}
3131

32-
public func upsert(values: Any, onConflict: String? = nil) -> PostgrestBuilder {
32+
public func upsert(values: Any, onConflict: String? = nil, returning: PostgrestReturningOptions = .representation) -> PostgrestBuilder {
3333
method = "POST"
34-
headers["Prefer"] = "return=representation,resolution=merge-duplicates"
34+
headers["Prefer"] = "return=\(returning.rawValue),resolution=merge-duplicates"
3535
if let onConflict = onConflict {
3636
appendSearchParams(name: "on_conflict", value: onConflict)
3737
}
@@ -40,19 +40,19 @@ public class PostgrestQueryBuilder: PostgrestBuilder {
4040
return self
4141
}
4242

43-
public func update(values: Any) -> PostgrestFilterBuilder {
43+
public func update(values: Any, returning: PostgrestReturningOptions = .representation) -> PostgrestFilterBuilder {
4444
method = "PATCH"
45-
headers["Prefer"] = "return=representation"
45+
headers["Prefer"] = "return=\(returning.rawValue)"
4646
body = values
4747
return PostgrestFilterBuilder(
4848
url: url, queryParams: queryParams, headers: headers, schema: schema, method: method,
4949
body: body
5050
)
5151
}
5252

53-
public func delete() -> PostgrestFilterBuilder {
53+
public func delete(returning: PostgrestReturningOptions = .representation) -> PostgrestFilterBuilder {
5454
method = "DELETE"
55-
headers["Prefer"] = "return=representation"
55+
headers["Prefer"] = "return=\(returning.rawValue)"
5656
return PostgrestFilterBuilder(
5757
url: url, queryParams: queryParams, headers: headers, schema: schema, method: method,
5858
body: body
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/// Enum of options representing the ways PostgREST can return values from the server.
2+
/// Options are:
3+
/// - minimal => Returns nothing from the server
4+
/// - representation => Returns a copy of the updated data.
5+
///
6+
/// https://postgrest.org/en/v9.0/api.html?highlight=PREFER#insertions-updates
7+
public enum PostgrestReturningOptions: String {
8+
case minimal = "minimal"
9+
case representation = "representation"
10+
}

0 commit comments

Comments
 (0)