diff --git a/README.md b/README.md index 8d0dc9b..f53384f 100644 --- a/README.md +++ b/README.md @@ -79,11 +79,11 @@ semaphore.wait() ## Contributing -- Fork the repo on GitHub -- Clone the project to your own machine -- Commit changes to your own branch -- Push your work back up to your fork -- Submit a Pull request so that we can review your changes and merge +- Fork the repo on GitHub +- Clone the project to your own machine +- Commit changes to your own branch +- Push your work back up to your fork +- Submit a Pull request so that we can review your changes and merge ## License diff --git a/Sources/PostgREST/PostgrestQueryBuilder.swift b/Sources/PostgREST/PostgrestQueryBuilder.swift index e39dfcb..8f18034 100644 --- a/Sources/PostgREST/PostgrestQueryBuilder.swift +++ b/Sources/PostgREST/PostgrestQueryBuilder.swift @@ -18,9 +18,9 @@ public class PostgrestQueryBuilder: PostgrestBuilder { ) } - public func insert(values: Any, upsert: Bool = false, onConflict: String? = nil) -> PostgrestBuilder { + public func insert(values: Any, upsert: Bool = false, onConflict: String? = nil, returning: PostgrestReturningOptions = .representation) -> PostgrestBuilder { method = "POST" - headers["Prefer"] = upsert ? "return=representation,resolution=merge-duplicates" : "return=representation" + headers["Prefer"] = upsert ? "return=\(returning.rawValue),resolution=merge-duplicates" : "return=\(returning.rawValue)" if let onConflict = onConflict { appendSearchParams(name: "on_conflict", value: onConflict) } @@ -29,9 +29,9 @@ public class PostgrestQueryBuilder: PostgrestBuilder { return self } - public func upsert(values: Any, onConflict: String? = nil) -> PostgrestBuilder { + public func upsert(values: Any, onConflict: String? = nil, returning: PostgrestReturningOptions = .representation) -> PostgrestBuilder { method = "POST" - headers["Prefer"] = "return=representation,resolution=merge-duplicates" + headers["Prefer"] = "return=\(returning.rawValue),resolution=merge-duplicates" if let onConflict = onConflict { appendSearchParams(name: "on_conflict", value: onConflict) } @@ -40,9 +40,9 @@ public class PostgrestQueryBuilder: PostgrestBuilder { return self } - public func update(values: Any) -> PostgrestFilterBuilder { + public func update(values: Any, returning: PostgrestReturningOptions = .representation) -> PostgrestFilterBuilder { method = "PATCH" - headers["Prefer"] = "return=representation" + headers["Prefer"] = "return=\(returning.rawValue)" body = values return PostgrestFilterBuilder( url: url, queryParams: queryParams, headers: headers, schema: schema, method: method, @@ -50,9 +50,9 @@ public class PostgrestQueryBuilder: PostgrestBuilder { ) } - public func delete() -> PostgrestFilterBuilder { + public func delete(returning: PostgrestReturningOptions = .representation) -> PostgrestFilterBuilder { method = "DELETE" - headers["Prefer"] = "return=representation" + headers["Prefer"] = "return=\(returning.rawValue)" return PostgrestFilterBuilder( url: url, queryParams: queryParams, headers: headers, schema: schema, method: method, body: body diff --git a/Sources/PostgREST/PostgrestReturningOptions.swift b/Sources/PostgREST/PostgrestReturningOptions.swift new file mode 100644 index 0000000..03a54bd --- /dev/null +++ b/Sources/PostgREST/PostgrestReturningOptions.swift @@ -0,0 +1,10 @@ +/// Enum of options representing the ways PostgREST can return values from the server. +/// Options are: +/// - minimal => Returns nothing from the server +/// - representation => Returns a copy of the updated data. +/// +/// https://postgrest.org/en/v9.0/api.html?highlight=PREFER#insertions-updates +public enum PostgrestReturningOptions: String { + case minimal = "minimal" + case representation = "representation" +}