Skip to content

Commit 881da2e

Browse files
committed
chore: add setDevkey and upsertDocument methods
1 parent 4ec6a52 commit 881da2e

File tree

4 files changed

+74
-41
lines changed

4 files changed

+74
-41
lines changed

docs/examples/java/databases/create-documents.md renamed to docs/examples/java/databases/upsert-document.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@ import io.appwrite.services.Databases;
44

55
Client client = new Client(context)
66
.setEndpoint("https://<REGION>.cloud.appwrite.io/v1") // Your API Endpoint
7-
.setKey(""); //
7+
.setProject("<YOUR_PROJECT_ID>"); // Your project ID
88

99
Databases databases = new Databases(client);
1010

11-
databases.createDocuments(
11+
databases.upsertDocument(
1212
"<DATABASE_ID>", // databaseId
1313
"<COLLECTION_ID>", // collectionId
14-
listOf(), // documents
14+
"<DOCUMENT_ID>", // documentId
15+
mapOf( "a" to "b" ), // data
16+
listOf("read("any")"), // permissions (optional)
1517
new CoroutineCallback<>((result, error) -> {
1618
if (error != null) {
1719
error.printStackTrace();

docs/examples/kotlin/databases/create-documents.md renamed to docs/examples/kotlin/databases/upsert-document.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ import io.appwrite.services.Databases
44

55
val client = Client(context)
66
.setEndpoint("https://<REGION>.cloud.appwrite.io/v1") // Your API Endpoint
7-
.setKey("") //
7+
.setProject("<YOUR_PROJECT_ID>") // Your project ID
88

99
val databases = Databases(client)
1010

11-
val result = databases.createDocuments(
11+
val result = databases.upsertDocument(
1212
databaseId = "<DATABASE_ID>",
1313
collectionId = "<COLLECTION_ID>",
14-
documents = listOf(),
14+
documentId = "<DOCUMENT_ID>",
15+
data = mapOf( "a" to "b" ),
16+
permissions = listOf("read("any")"), // (optional)
1517
)

library/src/main/java/io/appwrite/Client.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,21 @@ class Client @JvmOverloads constructor(
153153
return this
154154
}
155155

156+
/**
157+
* Set DevKey
158+
*
159+
* Your secret dev API key
160+
*
161+
* @param {string} devkey
162+
*
163+
* @return this
164+
*/
165+
fun setDevKey(value: String): Client {
166+
config["devKey"] = value
167+
addHeader("x-appwrite-dev-key", value)
168+
return this
169+
}
170+
156171
/**
157172
* Set self Signed
158173
*

library/src/main/java/io/appwrite/services/Databases.kt

Lines changed: 49 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -147,35 +147,38 @@ class Databases(client: Client) : Service(client) {
147147
)
148148

149149
/**
150-
* Create new Documents. Before using this route, you should create a new collection resource using either a [server integration](https://appwrite.io/docs/server/databases#databasesCreateCollection) API or directly from your database console.
150+
* Get a document by its unique ID. This endpoint response returns a JSON object with the document data.
151151
*
152152
* @param databaseId Database ID.
153-
* @param collectionId Collection ID. You can create a new collection using the Database service [server integration](https://appwrite.io/docs/server/databases#databasesCreateCollection). Make sure to define attributes before creating documents.
154-
* @param documents Array of documents data as JSON objects.
155-
* @return [io.appwrite.models.DocumentList<T>]
153+
* @param collectionId Collection ID. You can create a new collection using the Database service [server integration](https://appwrite.io/docs/server/databases#databasesCreateCollection).
154+
* @param documentId Document ID.
155+
* @param queries Array of query strings generated using the Query class provided by the SDK. [Learn more about queries](https://appwrite.io/docs/queries). Maximum of 100 queries are allowed, each 4096 characters long.
156+
* @return [io.appwrite.models.Document<T>]
156157
*/
157-
suspend fun <T> createDocuments(
158+
@JvmOverloads
159+
suspend fun <T> getDocument(
158160
databaseId: String,
159161
collectionId: String,
160-
documents: List<Any>,
162+
documentId: String,
163+
queries: List<String>? = null,
161164
nestedType: Class<T>,
162-
): io.appwrite.models.DocumentList<T> {
163-
val apiPath = "/databases/{databaseId}/collections/{collectionId}/documents"
165+
): io.appwrite.models.Document<T> {
166+
val apiPath = "/databases/{databaseId}/collections/{collectionId}/documents/{documentId}"
164167
.replace("{databaseId}", databaseId)
165168
.replace("{collectionId}", collectionId)
169+
.replace("{documentId}", documentId)
166170

167171
val apiParams = mutableMapOf<String, Any?>(
168-
"documents" to documents,
172+
"queries" to queries,
169173
)
170174
val apiHeaders = mutableMapOf<String, String>(
171-
"content-type" to "application/json",
172175
)
173-
val converter: (Any) -> io.appwrite.models.DocumentList<T> = {
176+
val converter: (Any) -> io.appwrite.models.Document<T> = {
174177
@Suppress("UNCHECKED_CAST")
175-
io.appwrite.models.DocumentList.from(map = it as Map<String, Any>, nestedType)
178+
io.appwrite.models.Document.from(map = it as Map<String, Any>, nestedType)
176179
}
177180
return client.call(
178-
"POST",
181+
"GET",
179182
apiPath,
180183
apiHeaders,
181184
apiParams,
@@ -185,40 +188,46 @@ class Databases(client: Client) : Service(client) {
185188
}
186189

187190
/**
188-
* Create new Documents. Before using this route, you should create a new collection resource using either a [server integration](https://appwrite.io/docs/server/databases#databasesCreateCollection) API or directly from your database console.
191+
* Get a document by its unique ID. This endpoint response returns a JSON object with the document data.
189192
*
190193
* @param databaseId Database ID.
191-
* @param collectionId Collection ID. You can create a new collection using the Database service [server integration](https://appwrite.io/docs/server/databases#databasesCreateCollection). Make sure to define attributes before creating documents.
192-
* @param documents Array of documents data as JSON objects.
193-
* @return [io.appwrite.models.DocumentList<T>]
194+
* @param collectionId Collection ID. You can create a new collection using the Database service [server integration](https://appwrite.io/docs/server/databases#databasesCreateCollection).
195+
* @param documentId Document ID.
196+
* @param queries Array of query strings generated using the Query class provided by the SDK. [Learn more about queries](https://appwrite.io/docs/queries). Maximum of 100 queries are allowed, each 4096 characters long.
197+
* @return [io.appwrite.models.Document<T>]
194198
*/
199+
@JvmOverloads
195200
@Throws(AppwriteException::class)
196-
suspend fun createDocuments(
201+
suspend fun getDocument(
197202
databaseId: String,
198203
collectionId: String,
199-
documents: List<Any>,
200-
): io.appwrite.models.DocumentList<Map<String, Any>> = createDocuments(
204+
documentId: String,
205+
queries: List<String>? = null,
206+
): io.appwrite.models.Document<Map<String, Any>> = getDocument(
201207
databaseId,
202208
collectionId,
203-
documents,
209+
documentId,
210+
queries,
204211
nestedType = classOf(),
205212
)
206213

207214
/**
208-
* Get a document by its unique ID. This endpoint response returns a JSON object with the document data.
215+
* Create or update a Document. Before using this route, you should create a new collection resource using either a [server integration](https://appwrite.io/docs/server/databases#databasesCreateCollection) API or directly from your database console.
209216
*
210217
* @param databaseId Database ID.
211-
* @param collectionId Collection ID. You can create a new collection using the Database service [server integration](https://appwrite.io/docs/server/databases#databasesCreateCollection).
218+
* @param collectionId Collection ID.
212219
* @param documentId Document ID.
213-
* @param queries Array of query strings generated using the Query class provided by the SDK. [Learn more about queries](https://appwrite.io/docs/queries). Maximum of 100 queries are allowed, each 4096 characters long.
220+
* @param data Document data as JSON object. Include all required attributes of the document to be created or updated.
221+
* @param permissions An array of permissions strings. By default, the current permissions are inherited. [Learn more about permissions](https://appwrite.io/docs/permissions).
214222
* @return [io.appwrite.models.Document<T>]
215223
*/
216224
@JvmOverloads
217-
suspend fun <T> getDocument(
225+
suspend fun <T> upsertDocument(
218226
databaseId: String,
219227
collectionId: String,
220228
documentId: String,
221-
queries: List<String>? = null,
229+
data: Any,
230+
permissions: List<String>? = null,
222231
nestedType: Class<T>,
223232
): io.appwrite.models.Document<T> {
224233
val apiPath = "/databases/{databaseId}/collections/{collectionId}/documents/{documentId}"
@@ -227,16 +236,18 @@ class Databases(client: Client) : Service(client) {
227236
.replace("{documentId}", documentId)
228237

229238
val apiParams = mutableMapOf<String, Any?>(
230-
"queries" to queries,
239+
"data" to data,
240+
"permissions" to permissions,
231241
)
232242
val apiHeaders = mutableMapOf<String, String>(
243+
"content-type" to "application/json",
233244
)
234245
val converter: (Any) -> io.appwrite.models.Document<T> = {
235246
@Suppress("UNCHECKED_CAST")
236247
io.appwrite.models.Document.from(map = it as Map<String, Any>, nestedType)
237248
}
238249
return client.call(
239-
"GET",
250+
"PUT",
240251
apiPath,
241252
apiHeaders,
242253
apiParams,
@@ -246,26 +257,29 @@ class Databases(client: Client) : Service(client) {
246257
}
247258

248259
/**
249-
* Get a document by its unique ID. This endpoint response returns a JSON object with the document data.
260+
* Create or update a Document. Before using this route, you should create a new collection resource using either a [server integration](https://appwrite.io/docs/server/databases#databasesCreateCollection) API or directly from your database console.
250261
*
251262
* @param databaseId Database ID.
252-
* @param collectionId Collection ID. You can create a new collection using the Database service [server integration](https://appwrite.io/docs/server/databases#databasesCreateCollection).
263+
* @param collectionId Collection ID.
253264
* @param documentId Document ID.
254-
* @param queries Array of query strings generated using the Query class provided by the SDK. [Learn more about queries](https://appwrite.io/docs/queries). Maximum of 100 queries are allowed, each 4096 characters long.
265+
* @param data Document data as JSON object. Include all required attributes of the document to be created or updated.
266+
* @param permissions An array of permissions strings. By default, the current permissions are inherited. [Learn more about permissions](https://appwrite.io/docs/permissions).
255267
* @return [io.appwrite.models.Document<T>]
256268
*/
257269
@JvmOverloads
258270
@Throws(AppwriteException::class)
259-
suspend fun getDocument(
271+
suspend fun upsertDocument(
260272
databaseId: String,
261273
collectionId: String,
262274
documentId: String,
263-
queries: List<String>? = null,
264-
): io.appwrite.models.Document<Map<String, Any>> = getDocument(
275+
data: Any,
276+
permissions: List<String>? = null,
277+
): io.appwrite.models.Document<Map<String, Any>> = upsertDocument(
265278
databaseId,
266279
collectionId,
267280
documentId,
268-
queries,
281+
data,
282+
permissions,
269283
nestedType = classOf(),
270284
)
271285

0 commit comments

Comments
 (0)