Skip to content

Commit 7fa9362

Browse files
authored
Merge pull request #1034 from manriif/postgrest-overloads
Add overloads for Postgrest insert, upsert and update requests
2 parents a899cb0 + 0b99c76 commit 7fa9362

File tree

1 file changed

+71
-12
lines changed

1 file changed

+71
-12
lines changed

Postgrest/src/commonMain/kotlin/io/github/jan/supabase/postgrest/query/PostgrestQueryBuilder.kt

Lines changed: 71 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import io.github.jan.supabase.postgrest.request.SelectRequest
1717
import io.github.jan.supabase.postgrest.request.UpdateRequest
1818
import io.github.jan.supabase.postgrest.result.PostgrestResult
1919
import io.ktor.client.plugins.HttpRequestTimeoutException
20+
import kotlinx.serialization.json.JsonArray
21+
import kotlinx.serialization.json.JsonElement
2022
import kotlinx.serialization.json.jsonArray
2123
import kotlinx.serialization.json.jsonObject
2224

@@ -65,18 +67,17 @@ class PostgrestQueryBuilder(
6567
*
6668
* By default, upserted rows are not returned. To return it, call `[PostgrestRequestBuilder.select]`.
6769
*
68-
* @param values The values to insert, will automatically get serialized into json.
70+
* @param body The request body as an array of json object.
6971
* @param request Additional configurations for the request including filters
7072
* @throws PostgrestRestException if receiving an error response
7173
* @throws HttpRequestTimeoutException if the request timed out
7274
* @throws HttpRequestException on network related issues
7375
*/
74-
suspend inline fun <reified T : Any> upsert(
75-
values: List<T>,
76+
suspend inline fun upsert(
77+
body: JsonArray,
7678
request: UpsertRequestBuilder.() -> Unit = {}
7779
): PostgrestResult {
7880
val requestBuilder = UpsertRequestBuilder(postgrest.config.propertyConversionMethod).apply(request)
79-
val body = postgrest.serializer.encodeToJsonElement(values).jsonArray
8081
val columns = body.map { it.jsonObject.keys }.flatten().distinct()
8182
if(columns.isNotEmpty()) requestBuilder.params["columns"] = listOf(columns.joinToString(","))
8283
requestBuilder.onConflict?.let {
@@ -96,6 +97,29 @@ class PostgrestQueryBuilder(
9697
return RestRequestExecutor.execute(postgrest = postgrest, path = table, request = insertRequest)
9798
}
9899

100+
/**
101+
* Perform an UPSERT on the table or view. Depending on the column(s) passed
102+
* to [UpsertRequestBuilder.onConflict], [upsert] allows you to perform the equivalent of
103+
* `[insert] if a row with the corresponding onConflict columns doesn't
104+
* exist, or if it does exist, perform an alternative action depending on
105+
* [UpsertRequestBuilder.ignoreDuplicates].
106+
*
107+
* By default, upserted rows are not returned. To return it, call `[PostgrestRequestBuilder.select]`.
108+
*
109+
* @param values The values to insert, will automatically get serialized into json.
110+
* @param request Additional configurations for the request including filters
111+
* @throws PostgrestRestException if receiving an error response
112+
* @throws HttpRequestTimeoutException if the request timed out
113+
* @throws HttpRequestException on network related issues
114+
*/
115+
suspend inline fun <reified T : Any> upsert(
116+
values: List<T>,
117+
request: UpsertRequestBuilder.() -> Unit = {}
118+
): PostgrestResult = upsert(
119+
body = postgrest.serializer.encodeToJsonElement(values).jsonArray,
120+
request = request
121+
)
122+
99123
/**
100124
* Perform an UPSERT on the table or view. Depending on the column(s) passed
101125
* to [UpsertRequestBuilder.onConflict], [upsert] allows you to perform the equivalent of
@@ -119,18 +143,17 @@ class PostgrestQueryBuilder(
119143
/**
120144
* Executes an insert operation on the [table]
121145
*
122-
* @param values The values to insert, will automatically get serialized into json.
146+
* @param body The request body as an array of json object.
123147
* @param request Additional filtering to apply to the query
124148
* @throws PostgrestRestException if receiving an error response
125149
* @throws HttpRequestTimeoutException if the request timed out
126150
* @throws HttpRequestException on network related issues
127151
*/
128-
suspend inline fun <reified T : Any> insert(
129-
values: List<T>,
152+
suspend inline fun insert(
153+
body: JsonArray,
130154
request: InsertRequestBuilder.() -> Unit = {}
131155
): PostgrestResult {
132156
val requestBuilder = InsertRequestBuilder(postgrest.config.propertyConversionMethod).apply(request)
133-
val body = postgrest.serializer.encodeToJsonElement(values).jsonArray
134157
val columns = body.map { it.jsonObject.keys }.flatten().distinct()
135158
if(columns.isNotEmpty()) requestBuilder.params["columns"] = listOf(columns.joinToString(","))
136159
val insertRequest = InsertRequest(
@@ -145,6 +168,23 @@ class PostgrestQueryBuilder(
145168
return RestRequestExecutor.execute(postgrest = postgrest, path = table, request = insertRequest)
146169
}
147170

171+
/**
172+
* Executes an insert operation on the [table]
173+
*
174+
* @param values The values to insert, will automatically get serialized into json.
175+
* @param request Additional filtering to apply to the query
176+
* @throws PostgrestRestException if receiving an error response
177+
* @throws HttpRequestTimeoutException if the request timed out
178+
* @throws HttpRequestException on network related issues
179+
*/
180+
suspend inline fun <reified T : Any> insert(
181+
values: List<T>,
182+
request: InsertRequestBuilder.() -> Unit = {}
183+
): PostgrestResult = insert(
184+
body = postgrest.serializer.encodeToJsonElement(values).jsonArray,
185+
request = request
186+
)
187+
148188
/**
149189
* Executes an insert operation on the [table]
150190
*
@@ -191,28 +231,47 @@ class PostgrestQueryBuilder(
191231
*
192232
* By default, updated rows are not returned. To return it, call `[PostgrestRequestBuilder.select]`.
193233
*
194-
* @param value The value to update, will automatically get serialized into json.
234+
* @param body The request body representing the value to update.
195235
* @param request Additional filtering to apply to the query
196236
* @throws PostgrestRestException if receiving an error response
197237
* @throws HttpRequestTimeoutException if the request timed out
198238
* @throws HttpRequestException on network related issues
199239
*/
200-
suspend inline fun <reified T : Any> update(
201-
value: T,
240+
suspend inline fun update(
241+
body: JsonElement,
202242
request: PostgrestRequestBuilder.() -> Unit = {}
203243
): PostgrestResult {
204244
val requestBuilder = PostgrestRequestBuilder(postgrest.config.propertyConversionMethod).apply(request)
205245
val updateRequest = UpdateRequest(
246+
body = body,
206247
returning = requestBuilder.returning,
207248
count = requestBuilder.count,
208249
urlParams = requestBuilder.params.mapToFirstValue(),
209-
body = postgrest.serializer.encodeToJsonElement(value),
210250
schema = schema,
211251
headers = requestBuilder.headers.build()
212252
)
213253
return RestRequestExecutor.execute(postgrest = postgrest, path = table, request = updateRequest)
214254
}
215255

256+
/**
257+
* Executes an update operation on the [table].
258+
*
259+
* By default, updated rows are not returned. To return it, call `[PostgrestRequestBuilder.select]`.
260+
*
261+
* @param value The value to update, will automatically get serialized into json.
262+
* @param request Additional filtering to apply to the query
263+
* @throws PostgrestRestException if receiving an error response
264+
* @throws HttpRequestTimeoutException if the request timed out
265+
* @throws HttpRequestException on network related issues
266+
*/
267+
suspend inline fun <reified T : Any> update(
268+
value: T,
269+
request: PostgrestRequestBuilder.() -> Unit = {}
270+
): PostgrestResult = update(
271+
body = postgrest.serializer.encodeToJsonElement(value),
272+
request = request
273+
)
274+
216275
/**
217276
* Executes a delete operation on the [table].
218277
*

0 commit comments

Comments
 (0)