@@ -17,6 +17,7 @@ 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
20
21
import kotlinx.serialization.json.jsonArray
21
22
import kotlinx.serialization.json.jsonObject
22
23
@@ -65,18 +66,17 @@ class PostgrestQueryBuilder(
65
66
*
66
67
* By default, upserted rows are not returned. To return it, call `[PostgrestRequestBuilder.select]`.
67
68
*
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 .
69
70
* @param request Additional configurations for the request including filters
70
71
* @throws PostgrestRestException if receiving an error response
71
72
* @throws HttpRequestTimeoutException if the request timed out
72
73
* @throws HttpRequestException on network related issues
73
74
*/
74
- suspend inline fun < reified T : Any > upsert (
75
- values : List < T > ,
75
+ suspend inline fun upsert (
76
+ body : JsonArray ,
76
77
request : UpsertRequestBuilder .() -> Unit = {}
77
78
): PostgrestResult {
78
79
val requestBuilder = UpsertRequestBuilder (postgrest.config.propertyConversionMethod).apply (request)
79
- val body = postgrest.serializer.encodeToJsonElement(values).jsonArray
80
80
val columns = body.map { it.jsonObject.keys }.flatten().distinct()
81
81
if (columns.isNotEmpty()) requestBuilder.params[" columns" ] = listOf (columns.joinToString(" ," ))
82
82
requestBuilder.onConflict?.let {
@@ -96,6 +96,29 @@ class PostgrestQueryBuilder(
96
96
return RestRequestExecutor .execute(postgrest = postgrest, path = table, request = insertRequest)
97
97
}
98
98
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
+
99
122
/* *
100
123
* Perform an UPSERT on the table or view. Depending on the column(s) passed
101
124
* to [UpsertRequestBuilder.onConflict], [upsert] allows you to perform the equivalent of
@@ -119,18 +142,17 @@ class PostgrestQueryBuilder(
119
142
/* *
120
143
* Executes an insert operation on the [table]
121
144
*
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 .
123
146
* @param request Additional filtering to apply to the query
124
147
* @throws PostgrestRestException if receiving an error response
125
148
* @throws HttpRequestTimeoutException if the request timed out
126
149
* @throws HttpRequestException on network related issues
127
150
*/
128
- suspend inline fun < reified T : Any > insert (
129
- values : List < T > ,
151
+ suspend inline fun insert (
152
+ body : JsonArray ,
130
153
request : InsertRequestBuilder .() -> Unit = {}
131
154
): PostgrestResult {
132
155
val requestBuilder = InsertRequestBuilder (postgrest.config.propertyConversionMethod).apply (request)
133
- val body = postgrest.serializer.encodeToJsonElement(values).jsonArray
134
156
val columns = body.map { it.jsonObject.keys }.flatten().distinct()
135
157
if (columns.isNotEmpty()) requestBuilder.params[" columns" ] = listOf (columns.joinToString(" ," ))
136
158
val insertRequest = InsertRequest (
@@ -145,6 +167,23 @@ class PostgrestQueryBuilder(
145
167
return RestRequestExecutor .execute(postgrest = postgrest, path = table, request = insertRequest)
146
168
}
147
169
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
+
148
187
/* *
149
188
* Executes an insert operation on the [table]
150
189
*
0 commit comments