@@ -17,6 +17,8 @@ import io.github.jan.supabase.postgrest.request.SelectRequest
17
17
import io.github.jan.supabase.postgrest.request.UpdateRequest
18
18
import io.github.jan.supabase.postgrest.result.PostgrestResult
19
19
import io.ktor.client.plugins.HttpRequestTimeoutException
20
+ import kotlinx.serialization.json.JsonArray
21
+ import kotlinx.serialization.json.JsonElement
20
22
import kotlinx.serialization.json.jsonArray
21
23
import kotlinx.serialization.json.jsonObject
22
24
@@ -65,18 +67,17 @@ class PostgrestQueryBuilder(
65
67
*
66
68
* By default, upserted rows are not returned. To return it, call `[PostgrestRequestBuilder.select]`.
67
69
*
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 .
69
71
* @param request Additional configurations for the request including filters
70
72
* @throws PostgrestRestException if receiving an error response
71
73
* @throws HttpRequestTimeoutException if the request timed out
72
74
* @throws HttpRequestException on network related issues
73
75
*/
74
- suspend inline fun < reified T : Any > upsert (
75
- values : List < T > ,
76
+ suspend inline fun upsert (
77
+ body : JsonArray ,
76
78
request : UpsertRequestBuilder .() -> Unit = {}
77
79
): PostgrestResult {
78
80
val requestBuilder = UpsertRequestBuilder (postgrest.config.propertyConversionMethod).apply (request)
79
- val body = postgrest.serializer.encodeToJsonElement(values).jsonArray
80
81
val columns = body.map { it.jsonObject.keys }.flatten().distinct()
81
82
if (columns.isNotEmpty()) requestBuilder.params[" columns" ] = listOf (columns.joinToString(" ," ))
82
83
requestBuilder.onConflict?.let {
@@ -96,6 +97,29 @@ class PostgrestQueryBuilder(
96
97
return RestRequestExecutor .execute(postgrest = postgrest, path = table, request = insertRequest)
97
98
}
98
99
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
+
99
123
/* *
100
124
* Perform an UPSERT on the table or view. Depending on the column(s) passed
101
125
* to [UpsertRequestBuilder.onConflict], [upsert] allows you to perform the equivalent of
@@ -119,18 +143,17 @@ class PostgrestQueryBuilder(
119
143
/* *
120
144
* Executes an insert operation on the [table]
121
145
*
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 .
123
147
* @param request Additional filtering to apply to the query
124
148
* @throws PostgrestRestException if receiving an error response
125
149
* @throws HttpRequestTimeoutException if the request timed out
126
150
* @throws HttpRequestException on network related issues
127
151
*/
128
- suspend inline fun < reified T : Any > insert (
129
- values : List < T > ,
152
+ suspend inline fun insert (
153
+ body : JsonArray ,
130
154
request : InsertRequestBuilder .() -> Unit = {}
131
155
): PostgrestResult {
132
156
val requestBuilder = InsertRequestBuilder (postgrest.config.propertyConversionMethod).apply (request)
133
- val body = postgrest.serializer.encodeToJsonElement(values).jsonArray
134
157
val columns = body.map { it.jsonObject.keys }.flatten().distinct()
135
158
if (columns.isNotEmpty()) requestBuilder.params[" columns" ] = listOf (columns.joinToString(" ," ))
136
159
val insertRequest = InsertRequest (
@@ -145,6 +168,23 @@ class PostgrestQueryBuilder(
145
168
return RestRequestExecutor .execute(postgrest = postgrest, path = table, request = insertRequest)
146
169
}
147
170
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
+
148
188
/* *
149
189
* Executes an insert operation on the [table]
150
190
*
@@ -191,28 +231,47 @@ class PostgrestQueryBuilder(
191
231
*
192
232
* By default, updated rows are not returned. To return it, call `[PostgrestRequestBuilder.select]`.
193
233
*
194
- * @param value The value to update, will automatically get serialized into json .
234
+ * @param body The request body representing the value to update .
195
235
* @param request Additional filtering to apply to the query
196
236
* @throws PostgrestRestException if receiving an error response
197
237
* @throws HttpRequestTimeoutException if the request timed out
198
238
* @throws HttpRequestException on network related issues
199
239
*/
200
- suspend inline fun < reified T : Any > update (
201
- value : T ,
240
+ suspend inline fun update (
241
+ body : JsonElement ,
202
242
request : PostgrestRequestBuilder .() -> Unit = {}
203
243
): PostgrestResult {
204
244
val requestBuilder = PostgrestRequestBuilder (postgrest.config.propertyConversionMethod).apply (request)
205
245
val updateRequest = UpdateRequest (
246
+ body = body,
206
247
returning = requestBuilder.returning,
207
248
count = requestBuilder.count,
208
249
urlParams = requestBuilder.params.mapToFirstValue(),
209
- body = postgrest.serializer.encodeToJsonElement(value),
210
250
schema = schema,
211
251
headers = requestBuilder.headers.build()
212
252
)
213
253
return RestRequestExecutor .execute(postgrest = postgrest, path = table, request = updateRequest)
214
254
}
215
255
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
+
216
275
/* *
217
276
* Executes a delete operation on the [table].
218
277
*
0 commit comments