diff --git a/docs/examples/java/databases/create-documents.md b/docs/examples/java/databases/upsert-document.md similarity index 73% rename from docs/examples/java/databases/create-documents.md rename to docs/examples/java/databases/upsert-document.md index dbdc64f..868576b 100644 --- a/docs/examples/java/databases/create-documents.md +++ b/docs/examples/java/databases/upsert-document.md @@ -4,14 +4,16 @@ import io.appwrite.services.Databases; Client client = new Client(context) .setEndpoint("https://.cloud.appwrite.io/v1") // Your API Endpoint - .setKey(""); // + .setProject(""); // Your project ID Databases databases = new Databases(client); -databases.createDocuments( +databases.upsertDocument( "", // databaseId "", // collectionId - listOf(), // documents + "", // documentId + mapOf( "a" to "b" ), // data + listOf("read("any")"), // permissions (optional) new CoroutineCallback<>((result, error) -> { if (error != null) { error.printStackTrace(); diff --git a/docs/examples/kotlin/databases/create-documents.md b/docs/examples/kotlin/databases/upsert-document.md similarity index 60% rename from docs/examples/kotlin/databases/create-documents.md rename to docs/examples/kotlin/databases/upsert-document.md index 33635b4..a31dfc8 100644 --- a/docs/examples/kotlin/databases/create-documents.md +++ b/docs/examples/kotlin/databases/upsert-document.md @@ -4,12 +4,14 @@ import io.appwrite.services.Databases val client = Client(context) .setEndpoint("https://.cloud.appwrite.io/v1") // Your API Endpoint - .setKey("") // + .setProject("") // Your project ID val databases = Databases(client) -val result = databases.createDocuments( +val result = databases.upsertDocument( databaseId = "", collectionId = "", - documents = listOf(), + documentId = "", + data = mapOf( "a" to "b" ), + permissions = listOf("read("any")"), // (optional) ) \ No newline at end of file diff --git a/library/src/main/java/io/appwrite/Client.kt b/library/src/main/java/io/appwrite/Client.kt index 6ea42ae..f223cef 100644 --- a/library/src/main/java/io/appwrite/Client.kt +++ b/library/src/main/java/io/appwrite/Client.kt @@ -153,6 +153,21 @@ class Client @JvmOverloads constructor( return this } + /** + * Set DevKey + * + * Your secret dev API key + * + * @param {string} devkey + * + * @return this + */ + fun setDevKey(value: String): Client { + config["devKey"] = value + addHeader("x-appwrite-dev-key", value) + return this + } + /** * Set self Signed * diff --git a/library/src/main/java/io/appwrite/services/Databases.kt b/library/src/main/java/io/appwrite/services/Databases.kt index 73384a1..2601af2 100644 --- a/library/src/main/java/io/appwrite/services/Databases.kt +++ b/library/src/main/java/io/appwrite/services/Databases.kt @@ -146,64 +146,6 @@ class Databases(client: Client) : Service(client) { nestedType = classOf(), ) - /** - * 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. - * - * @param databaseId Database ID. - * @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. - * @param documents Array of documents data as JSON objects. - * @return [io.appwrite.models.DocumentList] - */ - suspend fun createDocuments( - databaseId: String, - collectionId: String, - documents: List, - nestedType: Class, - ): io.appwrite.models.DocumentList { - val apiPath = "/databases/{databaseId}/collections/{collectionId}/documents" - .replace("{databaseId}", databaseId) - .replace("{collectionId}", collectionId) - - val apiParams = mutableMapOf( - "documents" to documents, - ) - val apiHeaders = mutableMapOf( - "content-type" to "application/json", - ) - val converter: (Any) -> io.appwrite.models.DocumentList = { - @Suppress("UNCHECKED_CAST") - io.appwrite.models.DocumentList.from(map = it as Map, nestedType) - } - return client.call( - "POST", - apiPath, - apiHeaders, - apiParams, - responseType = classOf(), - converter, - ) - } - - /** - * 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. - * - * @param databaseId Database ID. - * @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. - * @param documents Array of documents data as JSON objects. - * @return [io.appwrite.models.DocumentList] - */ - @Throws(AppwriteException::class) - suspend fun createDocuments( - databaseId: String, - collectionId: String, - documents: List, - ): io.appwrite.models.DocumentList> = createDocuments( - databaseId, - collectionId, - documents, - nestedType = classOf(), - ) - /** * Get a document by its unique ID. This endpoint response returns a JSON object with the document data. * @@ -269,6 +211,78 @@ class Databases(client: Client) : Service(client) { nestedType = classOf(), ) + /** + * 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. + * + * @param databaseId Database ID. + * @param collectionId Collection ID. + * @param documentId Document ID. + * @param data Document data as JSON object. Include all required attributes of the document to be created or updated. + * @param permissions An array of permissions strings. By default, the current permissions are inherited. [Learn more about permissions](https://appwrite.io/docs/permissions). + * @return [io.appwrite.models.Document] + */ + @JvmOverloads + suspend fun upsertDocument( + databaseId: String, + collectionId: String, + documentId: String, + data: Any, + permissions: List? = null, + nestedType: Class, + ): io.appwrite.models.Document { + val apiPath = "/databases/{databaseId}/collections/{collectionId}/documents/{documentId}" + .replace("{databaseId}", databaseId) + .replace("{collectionId}", collectionId) + .replace("{documentId}", documentId) + + val apiParams = mutableMapOf( + "data" to data, + "permissions" to permissions, + ) + val apiHeaders = mutableMapOf( + "content-type" to "application/json", + ) + val converter: (Any) -> io.appwrite.models.Document = { + @Suppress("UNCHECKED_CAST") + io.appwrite.models.Document.from(map = it as Map, nestedType) + } + return client.call( + "PUT", + apiPath, + apiHeaders, + apiParams, + responseType = classOf(), + converter, + ) + } + + /** + * 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. + * + * @param databaseId Database ID. + * @param collectionId Collection ID. + * @param documentId Document ID. + * @param data Document data as JSON object. Include all required attributes of the document to be created or updated. + * @param permissions An array of permissions strings. By default, the current permissions are inherited. [Learn more about permissions](https://appwrite.io/docs/permissions). + * @return [io.appwrite.models.Document] + */ + @JvmOverloads + @Throws(AppwriteException::class) + suspend fun upsertDocument( + databaseId: String, + collectionId: String, + documentId: String, + data: Any, + permissions: List? = null, + ): io.appwrite.models.Document> = upsertDocument( + databaseId, + collectionId, + documentId, + data, + permissions, + nestedType = classOf(), + ) + /** * Update a document by its unique ID. Using the patch method you can pass only specific fields that will get updated. *