Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Add the package to your `Package.swift` dependencies:

```swift
dependencies: [
.package(url: "[email protected]:appwrite/sdk-for-apple.git", from: "10.0.0"),
.package(url: "[email protected]:appwrite/sdk-for-apple.git", from: "10.1.0"),
],
```

Expand Down
17 changes: 16 additions & 1 deletion Sources/Appwrite/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ open class Client {
"x-sdk-name": "Apple",
"x-sdk-platform": "client",
"x-sdk-language": "apple",
"x-sdk-version": "10.0.0",
"x-sdk-version": "10.1.0",
"x-appwrite-response-format": "1.7.0"
]

Expand Down Expand Up @@ -147,6 +147,21 @@ open class Client {
return self
}

///
/// Set DevKey
///
/// Your secret dev API key
///
/// @param String value
///
/// @return Client
///
open func setDevKey(_ value: String) -> Client {
config["devkey"] = value
_ = addHeader(key: "X-Appwrite-Dev-Key", value: value)
return self
}


///
/// Set self signed
Expand Down
80 changes: 80 additions & 0 deletions Sources/Appwrite/Services/Databases.swift
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,86 @@ open class Databases: Service {
)
}

///
/// 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 String databaseId
/// @param String collectionId
/// @param String documentId
/// @param Any data
/// @param [String] permissions
/// @throws Exception
/// @return array
///
open func upsertDocument<T>(
databaseId: String,
collectionId: String,
documentId: String,
data: Any,
permissions: [String]? = nil,
nestedType: T.Type
) async throws -> AppwriteModels.Document<T> {
let apiPath: String = "/databases/{databaseId}/collections/{collectionId}/documents/{documentId}"
.replacingOccurrences(of: "{databaseId}", with: databaseId)
.replacingOccurrences(of: "{collectionId}", with: collectionId)
.replacingOccurrences(of: "{documentId}", with: documentId)

let apiParams: [String: Any?] = [
"data": data,
"permissions": permissions
]

let apiHeaders: [String: String] = [
"content-type": "application/json"
]

let converter: (Any) -> AppwriteModels.Document<T> = { response in
return AppwriteModels.Document.from(map: response as! [String: Any])
}

return try await client.call(
method: "PUT",
path: apiPath,
headers: apiHeaders,
params: apiParams,
converter: 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 String databaseId
/// @param String collectionId
/// @param String documentId
/// @param Any data
/// @param [String] permissions
/// @throws Exception
/// @return array
///
open func upsertDocument(
databaseId: String,
collectionId: String,
documentId: String,
data: Any,
permissions: [String]? = nil
) async throws -> AppwriteModels.Document<[String: AnyCodable]> {
return try await upsertDocument(
databaseId: databaseId,
collectionId: collectionId,
documentId: documentId,
data: data,
permissions: permissions,
nestedType: [String: AnyCodable].self
)
}

///
/// Update a document by its unique ID. Using the patch method you can pass
/// only specific fields that will get updated.
Expand Down
16 changes: 16 additions & 0 deletions docs/examples/databases/upsert-document.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Appwrite

let client = Client()
.setEndpoint("https://<REGION>.cloud.appwrite.io/v1") // Your API Endpoint
.setProject("<YOUR_PROJECT_ID>") // Your project ID

let databases = Databases(client)

let document = try await databases.upsertDocument(
databaseId: "<DATABASE_ID>",
collectionId: "<COLLECTION_ID>",
documentId: "<DOCUMENT_ID>",
data: [:],
permissions: ["read("any")"] // optional
)