Skip to content

Commit 84a3893

Browse files
committed
Add overloads for postgrest insert and upsert requests accepting a JsonArray as request body
1 parent 43bbd28 commit 84a3893

File tree

1 file changed

+47
-8
lines changed

1 file changed

+47
-8
lines changed

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

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ 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
2021
import kotlinx.serialization.json.jsonArray
2122
import kotlinx.serialization.json.jsonObject
2223

@@ -65,18 +66,17 @@ class PostgrestQueryBuilder(
6566
*
6667
* By default, upserted rows are not returned. To return it, call `[PostgrestRequestBuilder.select]`.
6768
*
68-
* @param values The values to insert, will automatically get serialized into json.
69+
* @param body The request body as an array of json object.
6970
* @param request Additional configurations for the request including filters
7071
* @throws PostgrestRestException if receiving an error response
7172
* @throws HttpRequestTimeoutException if the request timed out
7273
* @throws HttpRequestException on network related issues
7374
*/
74-
suspend inline fun <reified T : Any> upsert(
75-
values: List<T>,
75+
suspend inline fun upsert(
76+
body: JsonArray,
7677
request: UpsertRequestBuilder.() -> Unit = {}
7778
): PostgrestResult {
7879
val requestBuilder = UpsertRequestBuilder(postgrest.config.propertyConversionMethod).apply(request)
79-
val body = postgrest.serializer.encodeToJsonElement(values).jsonArray
8080
val columns = body.map { it.jsonObject.keys }.flatten().distinct()
8181
if(columns.isNotEmpty()) requestBuilder.params["columns"] = listOf(columns.joinToString(","))
8282
requestBuilder.onConflict?.let {
@@ -96,6 +96,29 @@ class PostgrestQueryBuilder(
9696
return RestRequestExecutor.execute(postgrest = postgrest, path = table, request = insertRequest)
9797
}
9898

99+
/**
100+
* Perform an UPSERT on the table or view. Depending on the column(s) passed
101+
* to [UpsertRequestBuilder.onConflict], [upsert] allows you to perform the equivalent of
102+
* `[insert] if a row with the corresponding onConflict columns doesn't
103+
* exist, or if it does exist, perform an alternative action depending on
104+
* [UpsertRequestBuilder.ignoreDuplicates].
105+
*
106+
* By default, upserted rows are not returned. To return it, call `[PostgrestRequestBuilder.select]`.
107+
*
108+
* @param values The values to insert, will automatically get serialized into json.
109+
* @param request Additional configurations for the request including filters
110+
* @throws PostgrestRestException if receiving an error response
111+
* @throws HttpRequestTimeoutException if the request timed out
112+
* @throws HttpRequestException on network related issues
113+
*/
114+
suspend inline fun <reified T : Any> upsert(
115+
values: List<T>,
116+
request: UpsertRequestBuilder.() -> Unit = {}
117+
): PostgrestResult = upsert(
118+
body = postgrest.serializer.encodeToJsonElement(values).jsonArray,
119+
request = request
120+
)
121+
99122
/**
100123
* Perform an UPSERT on the table or view. Depending on the column(s) passed
101124
* to [UpsertRequestBuilder.onConflict], [upsert] allows you to perform the equivalent of
@@ -119,18 +142,17 @@ class PostgrestQueryBuilder(
119142
/**
120143
* Executes an insert operation on the [table]
121144
*
122-
* @param values The values to insert, will automatically get serialized into json.
145+
* @param body The request body as an array of json object.
123146
* @param request Additional filtering to apply to the query
124147
* @throws PostgrestRestException if receiving an error response
125148
* @throws HttpRequestTimeoutException if the request timed out
126149
* @throws HttpRequestException on network related issues
127150
*/
128-
suspend inline fun <reified T : Any> insert(
129-
values: List<T>,
151+
suspend inline fun insert(
152+
body: JsonArray,
130153
request: InsertRequestBuilder.() -> Unit = {}
131154
): PostgrestResult {
132155
val requestBuilder = InsertRequestBuilder(postgrest.config.propertyConversionMethod).apply(request)
133-
val body = postgrest.serializer.encodeToJsonElement(values).jsonArray
134156
val columns = body.map { it.jsonObject.keys }.flatten().distinct()
135157
if(columns.isNotEmpty()) requestBuilder.params["columns"] = listOf(columns.joinToString(","))
136158
val insertRequest = InsertRequest(
@@ -145,6 +167,23 @@ class PostgrestQueryBuilder(
145167
return RestRequestExecutor.execute(postgrest = postgrest, path = table, request = insertRequest)
146168
}
147169

170+
/**
171+
* Executes an insert operation on the [table]
172+
*
173+
* @param values The values to insert, will automatically get serialized into json.
174+
* @param request Additional filtering to apply to the query
175+
* @throws PostgrestRestException if receiving an error response
176+
* @throws HttpRequestTimeoutException if the request timed out
177+
* @throws HttpRequestException on network related issues
178+
*/
179+
suspend inline fun <reified T : Any> insert(
180+
values: List<T>,
181+
request: InsertRequestBuilder.() -> Unit = {}
182+
): PostgrestResult = insert(
183+
body = postgrest.serializer.encodeToJsonElement(values).jsonArray,
184+
request = request
185+
)
186+
148187
/**
149188
* Executes an insert operation on the [table]
150189
*

0 commit comments

Comments
 (0)