From d61ce34e246c1ec115f67794a61790b2faa98f15 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Thu, 9 Oct 2025 20:20:50 +1300 Subject: [PATCH] Add transactions --- CHANGELOG.md | 4 + README.md | 2 +- Sources/Appwrite/Client.swift | 2 +- Sources/Appwrite/Services/Databases.swift | 279 +++++++++++++++++- Sources/Appwrite/Services/TablesDb.swift | 279 +++++++++++++++++- Sources/AppwriteModels/Transaction.swift | 94 ++++++ Sources/AppwriteModels/TransactionList.swift | 54 ++++ docs/examples/databases/create-document.md | 3 +- docs/examples/databases/create-operations.md | 23 ++ docs/examples/databases/create-transaction.md | 12 + .../databases/decrement-document-attribute.md | 3 +- docs/examples/databases/delete-document.md | 3 +- docs/examples/databases/delete-transaction.md | 12 + docs/examples/databases/get-document.md | 3 +- docs/examples/databases/get-transaction.md | 12 + .../databases/increment-document-attribute.md | 3 +- docs/examples/databases/list-documents.md | 3 +- docs/examples/databases/list-transactions.md | 12 + docs/examples/databases/update-document.md | 3 +- docs/examples/databases/update-transaction.md | 14 + docs/examples/databases/upsert-document.md | 3 +- docs/examples/tablesdb/create-operations.md | 23 ++ docs/examples/tablesdb/create-row.md | 3 +- docs/examples/tablesdb/create-transaction.md | 12 + .../examples/tablesdb/decrement-row-column.md | 3 +- docs/examples/tablesdb/delete-row.md | 3 +- docs/examples/tablesdb/delete-transaction.md | 12 + docs/examples/tablesdb/get-row.md | 3 +- docs/examples/tablesdb/get-transaction.md | 12 + .../examples/tablesdb/increment-row-column.md | 3 +- docs/examples/tablesdb/list-rows.md | 3 +- docs/examples/tablesdb/list-transactions.md | 12 + docs/examples/tablesdb/update-row.md | 3 +- docs/examples/tablesdb/update-transaction.md | 14 + docs/examples/tablesdb/upsert-row.md | 3 +- 35 files changed, 882 insertions(+), 50 deletions(-) create mode 100644 Sources/AppwriteModels/Transaction.swift create mode 100644 Sources/AppwriteModels/TransactionList.swift create mode 100644 docs/examples/databases/create-operations.md create mode 100644 docs/examples/databases/create-transaction.md create mode 100644 docs/examples/databases/delete-transaction.md create mode 100644 docs/examples/databases/get-transaction.md create mode 100644 docs/examples/databases/list-transactions.md create mode 100644 docs/examples/databases/update-transaction.md create mode 100644 docs/examples/tablesdb/create-operations.md create mode 100644 docs/examples/tablesdb/create-transaction.md create mode 100644 docs/examples/tablesdb/delete-transaction.md create mode 100644 docs/examples/tablesdb/get-transaction.md create mode 100644 docs/examples/tablesdb/list-transactions.md create mode 100644 docs/examples/tablesdb/update-transaction.md diff --git a/CHANGELOG.md b/CHANGELOG.md index dce2df1..fed0502 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Change Log +## 13.2.0 + +* Add transaction support for Databases and TablesDB + ## 13.1.0 * Deprecate `createVerification` method in `Account` service diff --git a/README.md b/README.md index 56b12e2..a64598b 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ Add the package to your `Package.swift` dependencies: ```swift dependencies: [ - .package(url: "git@github.com:appwrite/sdk-for-apple.git", from: "13.1.0"), + .package(url: "git@github.com:appwrite/sdk-for-apple.git", from: "13.2.0"), ], ``` diff --git a/Sources/Appwrite/Client.swift b/Sources/Appwrite/Client.swift index 0bb8ef7..fd7c90a 100644 --- a/Sources/Appwrite/Client.swift +++ b/Sources/Appwrite/Client.swift @@ -23,7 +23,7 @@ open class Client { "x-sdk-name": "Apple", "x-sdk-platform": "client", "x-sdk-language": "apple", - "x-sdk-version": "13.1.0", + "x-sdk-version": "13.2.0", "x-appwrite-response-format": "1.8.0" ] diff --git a/Sources/Appwrite/Services/Databases.swift b/Sources/Appwrite/Services/Databases.swift index e8c8f1e..f8bfc37 100644 --- a/Sources/Appwrite/Services/Databases.swift +++ b/Sources/Appwrite/Services/Databases.swift @@ -8,6 +8,207 @@ import AppwriteModels /// The Databases service allows you to create structured collections of documents, query and filter lists of documents open class Databases: Service { + /// + /// List transactions across all databases. + /// + /// - Parameters: + /// - queries: [String] (optional) + /// - Throws: Exception if the request fails + /// - Returns: AppwriteModels.TransactionList + /// + open func listTransactions( + queries: [String]? = nil + ) async throws -> AppwriteModels.TransactionList { + let apiPath: String = "/databases/transactions" + + let apiParams: [String: Any?] = [ + "queries": queries + ] + + let apiHeaders: [String: String] = [:] + + let converter: (Any) -> AppwriteModels.TransactionList = { response in + return AppwriteModels.TransactionList.from(map: response as! [String: Any]) + } + + return try await client.call( + method: "GET", + path: apiPath, + headers: apiHeaders, + params: apiParams, + converter: converter + ) + } + + /// + /// Create a new transaction. + /// + /// - Parameters: + /// - ttl: Int (optional) + /// - Throws: Exception if the request fails + /// - Returns: AppwriteModels.Transaction + /// + open func createTransaction( + ttl: Int? = nil + ) async throws -> AppwriteModels.Transaction { + let apiPath: String = "/databases/transactions" + + let apiParams: [String: Any?] = [ + "ttl": ttl + ] + + let apiHeaders: [String: String] = [ + "content-type": "application/json" + ] + + let converter: (Any) -> AppwriteModels.Transaction = { response in + return AppwriteModels.Transaction.from(map: response as! [String: Any]) + } + + return try await client.call( + method: "POST", + path: apiPath, + headers: apiHeaders, + params: apiParams, + converter: converter + ) + } + + /// + /// Get a transaction by its unique ID. + /// + /// - Parameters: + /// - transactionId: String + /// - Throws: Exception if the request fails + /// - Returns: AppwriteModels.Transaction + /// + open func getTransaction( + transactionId: String + ) async throws -> AppwriteModels.Transaction { + let apiPath: String = "/databases/transactions/{transactionId}" + .replacingOccurrences(of: "{transactionId}", with: transactionId) + + let apiParams: [String: Any] = [:] + + let apiHeaders: [String: String] = [:] + + let converter: (Any) -> AppwriteModels.Transaction = { response in + return AppwriteModels.Transaction.from(map: response as! [String: Any]) + } + + return try await client.call( + method: "GET", + path: apiPath, + headers: apiHeaders, + params: apiParams, + converter: converter + ) + } + + /// + /// Update a transaction, to either commit or roll back its operations. + /// + /// - Parameters: + /// - transactionId: String + /// - commit: Bool (optional) + /// - rollback: Bool (optional) + /// - Throws: Exception if the request fails + /// - Returns: AppwriteModels.Transaction + /// + open func updateTransaction( + transactionId: String, + commit: Bool? = nil, + rollback: Bool? = nil + ) async throws -> AppwriteModels.Transaction { + let apiPath: String = "/databases/transactions/{transactionId}" + .replacingOccurrences(of: "{transactionId}", with: transactionId) + + let apiParams: [String: Any?] = [ + "commit": commit, + "rollback": rollback + ] + + let apiHeaders: [String: String] = [ + "content-type": "application/json" + ] + + let converter: (Any) -> AppwriteModels.Transaction = { response in + return AppwriteModels.Transaction.from(map: response as! [String: Any]) + } + + return try await client.call( + method: "PATCH", + path: apiPath, + headers: apiHeaders, + params: apiParams, + converter: converter + ) + } + + /// + /// Delete a transaction by its unique ID. + /// + /// - Parameters: + /// - transactionId: String + /// - Throws: Exception if the request fails + /// - Returns: Any + /// + open func deleteTransaction( + transactionId: String + ) async throws -> Any { + let apiPath: String = "/databases/transactions/{transactionId}" + .replacingOccurrences(of: "{transactionId}", with: transactionId) + + let apiParams: [String: Any] = [:] + + let apiHeaders: [String: String] = [ + "content-type": "application/json" + ] + + return try await client.call( + method: "DELETE", + path: apiPath, + headers: apiHeaders, + params: apiParams ) + } + + /// + /// Create multiple operations in a single transaction. + /// + /// - Parameters: + /// - transactionId: String + /// - operations: [Any] (optional) + /// - Throws: Exception if the request fails + /// - Returns: AppwriteModels.Transaction + /// + open func createOperations( + transactionId: String, + operations: [Any]? = nil + ) async throws -> AppwriteModels.Transaction { + let apiPath: String = "/databases/transactions/{transactionId}/operations" + .replacingOccurrences(of: "{transactionId}", with: transactionId) + + let apiParams: [String: Any?] = [ + "operations": operations + ] + + let apiHeaders: [String: String] = [ + "content-type": "application/json" + ] + + let converter: (Any) -> AppwriteModels.Transaction = { response in + return AppwriteModels.Transaction.from(map: response as! [String: Any]) + } + + return try await client.call( + method: "POST", + path: apiPath, + headers: apiHeaders, + params: apiParams, + converter: converter + ) + } + /// /// Get a list of all the user's documents in a given collection. You can use /// the query params to filter your results. @@ -16,6 +217,7 @@ open class Databases: Service { /// - databaseId: String /// - collectionId: String /// - queries: [String] (optional) + /// - transactionId: String (optional) /// - Throws: Exception if the request fails /// - Returns: AppwriteModels.DocumentList /// @@ -24,6 +226,7 @@ open class Databases: Service { databaseId: String, collectionId: String, queries: [String]? = nil, + transactionId: String? = nil, nestedType: T.Type ) async throws -> AppwriteModels.DocumentList { let apiPath: String = "/databases/{databaseId}/collections/{collectionId}/documents" @@ -31,7 +234,8 @@ open class Databases: Service { .replacingOccurrences(of: "{collectionId}", with: collectionId) let apiParams: [String: Any?] = [ - "queries": queries + "queries": queries, + "transactionId": transactionId ] let apiHeaders: [String: String] = [:] @@ -57,6 +261,7 @@ open class Databases: Service { /// - databaseId: String /// - collectionId: String /// - queries: [String] (optional) + /// - transactionId: String (optional) /// - Throws: Exception if the request fails /// - Returns: AppwriteModels.DocumentList /// @@ -64,12 +269,14 @@ open class Databases: Service { open func listDocuments( databaseId: String, collectionId: String, - queries: [String]? = nil + queries: [String]? = nil, + transactionId: String? = nil ) async throws -> AppwriteModels.DocumentList<[String: AnyCodable]> { return try await listDocuments( databaseId: databaseId, collectionId: collectionId, queries: queries, + transactionId: transactionId, nestedType: [String: AnyCodable].self ) } @@ -86,6 +293,7 @@ open class Databases: Service { /// - documentId: String /// - data: Any /// - permissions: [String] (optional) + /// - transactionId: String (optional) /// - Throws: Exception if the request fails /// - Returns: AppwriteModels.Document /// @@ -96,6 +304,7 @@ open class Databases: Service { documentId: String, data: Any, permissions: [String]? = nil, + transactionId: String? = nil, nestedType: T.Type ) async throws -> AppwriteModels.Document { let apiPath: String = "/databases/{databaseId}/collections/{collectionId}/documents" @@ -105,7 +314,8 @@ open class Databases: Service { let apiParams: [String: Any?] = [ "documentId": documentId, "data": data, - "permissions": permissions + "permissions": permissions, + "transactionId": transactionId ] let apiHeaders: [String: String] = [ @@ -137,6 +347,7 @@ open class Databases: Service { /// - documentId: String /// - data: Any /// - permissions: [String] (optional) + /// - transactionId: String (optional) /// - Throws: Exception if the request fails /// - Returns: AppwriteModels.Document /// @@ -146,7 +357,8 @@ open class Databases: Service { collectionId: String, documentId: String, data: Any, - permissions: [String]? = nil + permissions: [String]? = nil, + transactionId: String? = nil ) async throws -> AppwriteModels.Document<[String: AnyCodable]> { return try await createDocument( databaseId: databaseId, @@ -154,6 +366,7 @@ open class Databases: Service { documentId: documentId, data: data, permissions: permissions, + transactionId: transactionId, nestedType: [String: AnyCodable].self ) } @@ -167,6 +380,7 @@ open class Databases: Service { /// - collectionId: String /// - documentId: String /// - queries: [String] (optional) + /// - transactionId: String (optional) /// - Throws: Exception if the request fails /// - Returns: AppwriteModels.Document /// @@ -176,6 +390,7 @@ open class Databases: Service { collectionId: String, documentId: String, queries: [String]? = nil, + transactionId: String? = nil, nestedType: T.Type ) async throws -> AppwriteModels.Document { let apiPath: String = "/databases/{databaseId}/collections/{collectionId}/documents/{documentId}" @@ -184,7 +399,8 @@ open class Databases: Service { .replacingOccurrences(of: "{documentId}", with: documentId) let apiParams: [String: Any?] = [ - "queries": queries + "queries": queries, + "transactionId": transactionId ] let apiHeaders: [String: String] = [:] @@ -211,6 +427,7 @@ open class Databases: Service { /// - collectionId: String /// - documentId: String /// - queries: [String] (optional) + /// - transactionId: String (optional) /// - Throws: Exception if the request fails /// - Returns: AppwriteModels.Document /// @@ -219,13 +436,15 @@ open class Databases: Service { databaseId: String, collectionId: String, documentId: String, - queries: [String]? = nil + queries: [String]? = nil, + transactionId: String? = nil ) async throws -> AppwriteModels.Document<[String: AnyCodable]> { return try await getDocument( databaseId: databaseId, collectionId: collectionId, documentId: documentId, queries: queries, + transactionId: transactionId, nestedType: [String: AnyCodable].self ) } @@ -242,6 +461,7 @@ open class Databases: Service { /// - documentId: String /// - data: Any /// - permissions: [String] (optional) + /// - transactionId: String (optional) /// - Throws: Exception if the request fails /// - Returns: AppwriteModels.Document /// @@ -252,6 +472,7 @@ open class Databases: Service { documentId: String, data: Any, permissions: [String]? = nil, + transactionId: String? = nil, nestedType: T.Type ) async throws -> AppwriteModels.Document { let apiPath: String = "/databases/{databaseId}/collections/{collectionId}/documents/{documentId}" @@ -261,7 +482,8 @@ open class Databases: Service { let apiParams: [String: Any?] = [ "data": data, - "permissions": permissions + "permissions": permissions, + "transactionId": transactionId ] let apiHeaders: [String: String] = [ @@ -293,6 +515,7 @@ open class Databases: Service { /// - documentId: String /// - data: Any /// - permissions: [String] (optional) + /// - transactionId: String (optional) /// - Throws: Exception if the request fails /// - Returns: AppwriteModels.Document /// @@ -302,7 +525,8 @@ open class Databases: Service { collectionId: String, documentId: String, data: Any, - permissions: [String]? = nil + permissions: [String]? = nil, + transactionId: String? = nil ) async throws -> AppwriteModels.Document<[String: AnyCodable]> { return try await upsertDocument( databaseId: databaseId, @@ -310,6 +534,7 @@ open class Databases: Service { documentId: documentId, data: data, permissions: permissions, + transactionId: transactionId, nestedType: [String: AnyCodable].self ) } @@ -324,6 +549,7 @@ open class Databases: Service { /// - documentId: String /// - data: Any (optional) /// - permissions: [String] (optional) + /// - transactionId: String (optional) /// - Throws: Exception if the request fails /// - Returns: AppwriteModels.Document /// @@ -334,6 +560,7 @@ open class Databases: Service { documentId: String, data: Any? = nil, permissions: [String]? = nil, + transactionId: String? = nil, nestedType: T.Type ) async throws -> AppwriteModels.Document { let apiPath: String = "/databases/{databaseId}/collections/{collectionId}/documents/{documentId}" @@ -343,7 +570,8 @@ open class Databases: Service { let apiParams: [String: Any?] = [ "data": data, - "permissions": permissions + "permissions": permissions, + "transactionId": transactionId ] let apiHeaders: [String: String] = [ @@ -373,6 +601,7 @@ open class Databases: Service { /// - documentId: String /// - data: Any (optional) /// - permissions: [String] (optional) + /// - transactionId: String (optional) /// - Throws: Exception if the request fails /// - Returns: AppwriteModels.Document /// @@ -382,7 +611,8 @@ open class Databases: Service { collectionId: String, documentId: String, data: Any? = nil, - permissions: [String]? = nil + permissions: [String]? = nil, + transactionId: String? = nil ) async throws -> AppwriteModels.Document<[String: AnyCodable]> { return try await updateDocument( databaseId: databaseId, @@ -390,6 +620,7 @@ open class Databases: Service { documentId: documentId, data: data, permissions: permissions, + transactionId: transactionId, nestedType: [String: AnyCodable].self ) } @@ -401,6 +632,7 @@ open class Databases: Service { /// - databaseId: String /// - collectionId: String /// - documentId: String + /// - transactionId: String (optional) /// - Throws: Exception if the request fails /// - Returns: Any /// @@ -408,14 +640,17 @@ open class Databases: Service { open func deleteDocument( databaseId: String, collectionId: String, - documentId: String + documentId: String, + transactionId: String? = nil ) async throws -> Any { 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] = [:] + let apiParams: [String: Any?] = [ + "transactionId": transactionId + ] let apiHeaders: [String: String] = [ "content-type": "application/json" @@ -438,6 +673,7 @@ open class Databases: Service { /// - attribute: String /// - value: Double (optional) /// - min: Double (optional) + /// - transactionId: String (optional) /// - Throws: Exception if the request fails /// - Returns: AppwriteModels.Document /// @@ -449,6 +685,7 @@ open class Databases: Service { attribute: String, value: Double? = nil, min: Double? = nil, + transactionId: String? = nil, nestedType: T.Type ) async throws -> AppwriteModels.Document { let apiPath: String = "/databases/{databaseId}/collections/{collectionId}/documents/{documentId}/{attribute}/decrement" @@ -459,7 +696,8 @@ open class Databases: Service { let apiParams: [String: Any?] = [ "value": value, - "min": min + "min": min, + "transactionId": transactionId ] let apiHeaders: [String: String] = [ @@ -489,6 +727,7 @@ open class Databases: Service { /// - attribute: String /// - value: Double (optional) /// - min: Double (optional) + /// - transactionId: String (optional) /// - Throws: Exception if the request fails /// - Returns: AppwriteModels.Document /// @@ -499,7 +738,8 @@ open class Databases: Service { documentId: String, attribute: String, value: Double? = nil, - min: Double? = nil + min: Double? = nil, + transactionId: String? = nil ) async throws -> AppwriteModels.Document<[String: AnyCodable]> { return try await decrementDocumentAttribute( databaseId: databaseId, @@ -508,6 +748,7 @@ open class Databases: Service { attribute: attribute, value: value, min: min, + transactionId: transactionId, nestedType: [String: AnyCodable].self ) } @@ -522,6 +763,7 @@ open class Databases: Service { /// - attribute: String /// - value: Double (optional) /// - max: Double (optional) + /// - transactionId: String (optional) /// - Throws: Exception if the request fails /// - Returns: AppwriteModels.Document /// @@ -533,6 +775,7 @@ open class Databases: Service { attribute: String, value: Double? = nil, max: Double? = nil, + transactionId: String? = nil, nestedType: T.Type ) async throws -> AppwriteModels.Document { let apiPath: String = "/databases/{databaseId}/collections/{collectionId}/documents/{documentId}/{attribute}/increment" @@ -543,7 +786,8 @@ open class Databases: Service { let apiParams: [String: Any?] = [ "value": value, - "max": max + "max": max, + "transactionId": transactionId ] let apiHeaders: [String: String] = [ @@ -573,6 +817,7 @@ open class Databases: Service { /// - attribute: String /// - value: Double (optional) /// - max: Double (optional) + /// - transactionId: String (optional) /// - Throws: Exception if the request fails /// - Returns: AppwriteModels.Document /// @@ -583,7 +828,8 @@ open class Databases: Service { documentId: String, attribute: String, value: Double? = nil, - max: Double? = nil + max: Double? = nil, + transactionId: String? = nil ) async throws -> AppwriteModels.Document<[String: AnyCodable]> { return try await incrementDocumentAttribute( databaseId: databaseId, @@ -592,6 +838,7 @@ open class Databases: Service { attribute: attribute, value: value, max: max, + transactionId: transactionId, nestedType: [String: AnyCodable].self ) } diff --git a/Sources/Appwrite/Services/TablesDb.swift b/Sources/Appwrite/Services/TablesDb.swift index 3445d0e..0727dde 100644 --- a/Sources/Appwrite/Services/TablesDb.swift +++ b/Sources/Appwrite/Services/TablesDb.swift @@ -8,6 +8,207 @@ import AppwriteModels /// open class TablesDB: Service { + /// + /// List transactions across all databases. + /// + /// - Parameters: + /// - queries: [String] (optional) + /// - Throws: Exception if the request fails + /// - Returns: AppwriteModels.TransactionList + /// + open func listTransactions( + queries: [String]? = nil + ) async throws -> AppwriteModels.TransactionList { + let apiPath: String = "/tablesdb/transactions" + + let apiParams: [String: Any?] = [ + "queries": queries + ] + + let apiHeaders: [String: String] = [:] + + let converter: (Any) -> AppwriteModels.TransactionList = { response in + return AppwriteModels.TransactionList.from(map: response as! [String: Any]) + } + + return try await client.call( + method: "GET", + path: apiPath, + headers: apiHeaders, + params: apiParams, + converter: converter + ) + } + + /// + /// Create a new transaction. + /// + /// - Parameters: + /// - ttl: Int (optional) + /// - Throws: Exception if the request fails + /// - Returns: AppwriteModels.Transaction + /// + open func createTransaction( + ttl: Int? = nil + ) async throws -> AppwriteModels.Transaction { + let apiPath: String = "/tablesdb/transactions" + + let apiParams: [String: Any?] = [ + "ttl": ttl + ] + + let apiHeaders: [String: String] = [ + "content-type": "application/json" + ] + + let converter: (Any) -> AppwriteModels.Transaction = { response in + return AppwriteModels.Transaction.from(map: response as! [String: Any]) + } + + return try await client.call( + method: "POST", + path: apiPath, + headers: apiHeaders, + params: apiParams, + converter: converter + ) + } + + /// + /// Get a transaction by its unique ID. + /// + /// - Parameters: + /// - transactionId: String + /// - Throws: Exception if the request fails + /// - Returns: AppwriteModels.Transaction + /// + open func getTransaction( + transactionId: String + ) async throws -> AppwriteModels.Transaction { + let apiPath: String = "/tablesdb/transactions/{transactionId}" + .replacingOccurrences(of: "{transactionId}", with: transactionId) + + let apiParams: [String: Any] = [:] + + let apiHeaders: [String: String] = [:] + + let converter: (Any) -> AppwriteModels.Transaction = { response in + return AppwriteModels.Transaction.from(map: response as! [String: Any]) + } + + return try await client.call( + method: "GET", + path: apiPath, + headers: apiHeaders, + params: apiParams, + converter: converter + ) + } + + /// + /// Update a transaction, to either commit or roll back its operations. + /// + /// - Parameters: + /// - transactionId: String + /// - commit: Bool (optional) + /// - rollback: Bool (optional) + /// - Throws: Exception if the request fails + /// - Returns: AppwriteModels.Transaction + /// + open func updateTransaction( + transactionId: String, + commit: Bool? = nil, + rollback: Bool? = nil + ) async throws -> AppwriteModels.Transaction { + let apiPath: String = "/tablesdb/transactions/{transactionId}" + .replacingOccurrences(of: "{transactionId}", with: transactionId) + + let apiParams: [String: Any?] = [ + "commit": commit, + "rollback": rollback + ] + + let apiHeaders: [String: String] = [ + "content-type": "application/json" + ] + + let converter: (Any) -> AppwriteModels.Transaction = { response in + return AppwriteModels.Transaction.from(map: response as! [String: Any]) + } + + return try await client.call( + method: "PATCH", + path: apiPath, + headers: apiHeaders, + params: apiParams, + converter: converter + ) + } + + /// + /// Delete a transaction by its unique ID. + /// + /// - Parameters: + /// - transactionId: String + /// - Throws: Exception if the request fails + /// - Returns: Any + /// + open func deleteTransaction( + transactionId: String + ) async throws -> Any { + let apiPath: String = "/tablesdb/transactions/{transactionId}" + .replacingOccurrences(of: "{transactionId}", with: transactionId) + + let apiParams: [String: Any] = [:] + + let apiHeaders: [String: String] = [ + "content-type": "application/json" + ] + + return try await client.call( + method: "DELETE", + path: apiPath, + headers: apiHeaders, + params: apiParams ) + } + + /// + /// Create multiple operations in a single transaction. + /// + /// - Parameters: + /// - transactionId: String + /// - operations: [Any] (optional) + /// - Throws: Exception if the request fails + /// - Returns: AppwriteModels.Transaction + /// + open func createOperations( + transactionId: String, + operations: [Any]? = nil + ) async throws -> AppwriteModels.Transaction { + let apiPath: String = "/tablesdb/transactions/{transactionId}/operations" + .replacingOccurrences(of: "{transactionId}", with: transactionId) + + let apiParams: [String: Any?] = [ + "operations": operations + ] + + let apiHeaders: [String: String] = [ + "content-type": "application/json" + ] + + let converter: (Any) -> AppwriteModels.Transaction = { response in + return AppwriteModels.Transaction.from(map: response as! [String: Any]) + } + + return try await client.call( + method: "POST", + path: apiPath, + headers: apiHeaders, + params: apiParams, + converter: converter + ) + } + /// /// Get a list of all the user's rows in a given table. You can use the query /// params to filter your results. @@ -16,6 +217,7 @@ open class TablesDB: Service { /// - databaseId: String /// - tableId: String /// - queries: [String] (optional) + /// - transactionId: String (optional) /// - Throws: Exception if the request fails /// - Returns: AppwriteModels.RowList /// @@ -23,6 +225,7 @@ open class TablesDB: Service { databaseId: String, tableId: String, queries: [String]? = nil, + transactionId: String? = nil, nestedType: T.Type ) async throws -> AppwriteModels.RowList { let apiPath: String = "/tablesdb/{databaseId}/tables/{tableId}/rows" @@ -30,7 +233,8 @@ open class TablesDB: Service { .replacingOccurrences(of: "{tableId}", with: tableId) let apiParams: [String: Any?] = [ - "queries": queries + "queries": queries, + "transactionId": transactionId ] let apiHeaders: [String: String] = [:] @@ -56,18 +260,21 @@ open class TablesDB: Service { /// - databaseId: String /// - tableId: String /// - queries: [String] (optional) + /// - transactionId: String (optional) /// - Throws: Exception if the request fails /// - Returns: AppwriteModels.RowList /// open func listRows( databaseId: String, tableId: String, - queries: [String]? = nil + queries: [String]? = nil, + transactionId: String? = nil ) async throws -> AppwriteModels.RowList<[String: AnyCodable]> { return try await listRows( databaseId: databaseId, tableId: tableId, queries: queries, + transactionId: transactionId, nestedType: [String: AnyCodable].self ) } @@ -84,6 +291,7 @@ open class TablesDB: Service { /// - rowId: String /// - data: Any /// - permissions: [String] (optional) + /// - transactionId: String (optional) /// - Throws: Exception if the request fails /// - Returns: AppwriteModels.Row /// @@ -93,6 +301,7 @@ open class TablesDB: Service { rowId: String, data: Any, permissions: [String]? = nil, + transactionId: String? = nil, nestedType: T.Type ) async throws -> AppwriteModels.Row { let apiPath: String = "/tablesdb/{databaseId}/tables/{tableId}/rows" @@ -102,7 +311,8 @@ open class TablesDB: Service { let apiParams: [String: Any?] = [ "rowId": rowId, "data": data, - "permissions": permissions + "permissions": permissions, + "transactionId": transactionId ] let apiHeaders: [String: String] = [ @@ -134,6 +344,7 @@ open class TablesDB: Service { /// - rowId: String /// - data: Any /// - permissions: [String] (optional) + /// - transactionId: String (optional) /// - Throws: Exception if the request fails /// - Returns: AppwriteModels.Row /// @@ -142,7 +353,8 @@ open class TablesDB: Service { tableId: String, rowId: String, data: Any, - permissions: [String]? = nil + permissions: [String]? = nil, + transactionId: String? = nil ) async throws -> AppwriteModels.Row<[String: AnyCodable]> { return try await createRow( databaseId: databaseId, @@ -150,6 +362,7 @@ open class TablesDB: Service { rowId: rowId, data: data, permissions: permissions, + transactionId: transactionId, nestedType: [String: AnyCodable].self ) } @@ -163,6 +376,7 @@ open class TablesDB: Service { /// - tableId: String /// - rowId: String /// - queries: [String] (optional) + /// - transactionId: String (optional) /// - Throws: Exception if the request fails /// - Returns: AppwriteModels.Row /// @@ -171,6 +385,7 @@ open class TablesDB: Service { tableId: String, rowId: String, queries: [String]? = nil, + transactionId: String? = nil, nestedType: T.Type ) async throws -> AppwriteModels.Row { let apiPath: String = "/tablesdb/{databaseId}/tables/{tableId}/rows/{rowId}" @@ -179,7 +394,8 @@ open class TablesDB: Service { .replacingOccurrences(of: "{rowId}", with: rowId) let apiParams: [String: Any?] = [ - "queries": queries + "queries": queries, + "transactionId": transactionId ] let apiHeaders: [String: String] = [:] @@ -206,6 +422,7 @@ open class TablesDB: Service { /// - tableId: String /// - rowId: String /// - queries: [String] (optional) + /// - transactionId: String (optional) /// - Throws: Exception if the request fails /// - Returns: AppwriteModels.Row /// @@ -213,13 +430,15 @@ open class TablesDB: Service { databaseId: String, tableId: String, rowId: String, - queries: [String]? = nil + queries: [String]? = nil, + transactionId: String? = nil ) async throws -> AppwriteModels.Row<[String: AnyCodable]> { return try await getRow( databaseId: databaseId, tableId: tableId, rowId: rowId, queries: queries, + transactionId: transactionId, nestedType: [String: AnyCodable].self ) } @@ -236,6 +455,7 @@ open class TablesDB: Service { /// - rowId: String /// - data: Any (optional) /// - permissions: [String] (optional) + /// - transactionId: String (optional) /// - Throws: Exception if the request fails /// - Returns: AppwriteModels.Row /// @@ -245,6 +465,7 @@ open class TablesDB: Service { rowId: String, data: Any? = nil, permissions: [String]? = nil, + transactionId: String? = nil, nestedType: T.Type ) async throws -> AppwriteModels.Row { let apiPath: String = "/tablesdb/{databaseId}/tables/{tableId}/rows/{rowId}" @@ -254,7 +475,8 @@ open class TablesDB: Service { let apiParams: [String: Any?] = [ "data": data, - "permissions": permissions + "permissions": permissions, + "transactionId": transactionId ] let apiHeaders: [String: String] = [ @@ -286,6 +508,7 @@ open class TablesDB: Service { /// - rowId: String /// - data: Any (optional) /// - permissions: [String] (optional) + /// - transactionId: String (optional) /// - Throws: Exception if the request fails /// - Returns: AppwriteModels.Row /// @@ -294,7 +517,8 @@ open class TablesDB: Service { tableId: String, rowId: String, data: Any? = nil, - permissions: [String]? = nil + permissions: [String]? = nil, + transactionId: String? = nil ) async throws -> AppwriteModels.Row<[String: AnyCodable]> { return try await upsertRow( databaseId: databaseId, @@ -302,6 +526,7 @@ open class TablesDB: Service { rowId: rowId, data: data, permissions: permissions, + transactionId: transactionId, nestedType: [String: AnyCodable].self ) } @@ -316,6 +541,7 @@ open class TablesDB: Service { /// - rowId: String /// - data: Any (optional) /// - permissions: [String] (optional) + /// - transactionId: String (optional) /// - Throws: Exception if the request fails /// - Returns: AppwriteModels.Row /// @@ -325,6 +551,7 @@ open class TablesDB: Service { rowId: String, data: Any? = nil, permissions: [String]? = nil, + transactionId: String? = nil, nestedType: T.Type ) async throws -> AppwriteModels.Row { let apiPath: String = "/tablesdb/{databaseId}/tables/{tableId}/rows/{rowId}" @@ -334,7 +561,8 @@ open class TablesDB: Service { let apiParams: [String: Any?] = [ "data": data, - "permissions": permissions + "permissions": permissions, + "transactionId": transactionId ] let apiHeaders: [String: String] = [ @@ -364,6 +592,7 @@ open class TablesDB: Service { /// - rowId: String /// - data: Any (optional) /// - permissions: [String] (optional) + /// - transactionId: String (optional) /// - Throws: Exception if the request fails /// - Returns: AppwriteModels.Row /// @@ -372,7 +601,8 @@ open class TablesDB: Service { tableId: String, rowId: String, data: Any? = nil, - permissions: [String]? = nil + permissions: [String]? = nil, + transactionId: String? = nil ) async throws -> AppwriteModels.Row<[String: AnyCodable]> { return try await updateRow( databaseId: databaseId, @@ -380,6 +610,7 @@ open class TablesDB: Service { rowId: rowId, data: data, permissions: permissions, + transactionId: transactionId, nestedType: [String: AnyCodable].self ) } @@ -391,20 +622,24 @@ open class TablesDB: Service { /// - databaseId: String /// - tableId: String /// - rowId: String + /// - transactionId: String (optional) /// - Throws: Exception if the request fails /// - Returns: Any /// open func deleteRow( databaseId: String, tableId: String, - rowId: String + rowId: String, + transactionId: String? = nil ) async throws -> Any { let apiPath: String = "/tablesdb/{databaseId}/tables/{tableId}/rows/{rowId}" .replacingOccurrences(of: "{databaseId}", with: databaseId) .replacingOccurrences(of: "{tableId}", with: tableId) .replacingOccurrences(of: "{rowId}", with: rowId) - let apiParams: [String: Any] = [:] + let apiParams: [String: Any?] = [ + "transactionId": transactionId + ] let apiHeaders: [String: String] = [ "content-type": "application/json" @@ -427,6 +662,7 @@ open class TablesDB: Service { /// - column: String /// - value: Double (optional) /// - min: Double (optional) + /// - transactionId: String (optional) /// - Throws: Exception if the request fails /// - Returns: AppwriteModels.Row /// @@ -437,6 +673,7 @@ open class TablesDB: Service { column: String, value: Double? = nil, min: Double? = nil, + transactionId: String? = nil, nestedType: T.Type ) async throws -> AppwriteModels.Row { let apiPath: String = "/tablesdb/{databaseId}/tables/{tableId}/rows/{rowId}/{column}/decrement" @@ -447,7 +684,8 @@ open class TablesDB: Service { let apiParams: [String: Any?] = [ "value": value, - "min": min + "min": min, + "transactionId": transactionId ] let apiHeaders: [String: String] = [ @@ -477,6 +715,7 @@ open class TablesDB: Service { /// - column: String /// - value: Double (optional) /// - min: Double (optional) + /// - transactionId: String (optional) /// - Throws: Exception if the request fails /// - Returns: AppwriteModels.Row /// @@ -486,7 +725,8 @@ open class TablesDB: Service { rowId: String, column: String, value: Double? = nil, - min: Double? = nil + min: Double? = nil, + transactionId: String? = nil ) async throws -> AppwriteModels.Row<[String: AnyCodable]> { return try await decrementRowColumn( databaseId: databaseId, @@ -495,6 +735,7 @@ open class TablesDB: Service { column: column, value: value, min: min, + transactionId: transactionId, nestedType: [String: AnyCodable].self ) } @@ -509,6 +750,7 @@ open class TablesDB: Service { /// - column: String /// - value: Double (optional) /// - max: Double (optional) + /// - transactionId: String (optional) /// - Throws: Exception if the request fails /// - Returns: AppwriteModels.Row /// @@ -519,6 +761,7 @@ open class TablesDB: Service { column: String, value: Double? = nil, max: Double? = nil, + transactionId: String? = nil, nestedType: T.Type ) async throws -> AppwriteModels.Row { let apiPath: String = "/tablesdb/{databaseId}/tables/{tableId}/rows/{rowId}/{column}/increment" @@ -529,7 +772,8 @@ open class TablesDB: Service { let apiParams: [String: Any?] = [ "value": value, - "max": max + "max": max, + "transactionId": transactionId ] let apiHeaders: [String: String] = [ @@ -559,6 +803,7 @@ open class TablesDB: Service { /// - column: String /// - value: Double (optional) /// - max: Double (optional) + /// - transactionId: String (optional) /// - Throws: Exception if the request fails /// - Returns: AppwriteModels.Row /// @@ -568,7 +813,8 @@ open class TablesDB: Service { rowId: String, column: String, value: Double? = nil, - max: Double? = nil + max: Double? = nil, + transactionId: String? = nil ) async throws -> AppwriteModels.Row<[String: AnyCodable]> { return try await incrementRowColumn( databaseId: databaseId, @@ -577,6 +823,7 @@ open class TablesDB: Service { column: column, value: value, max: max, + transactionId: transactionId, nestedType: [String: AnyCodable].self ) } diff --git a/Sources/AppwriteModels/Transaction.swift b/Sources/AppwriteModels/Transaction.swift new file mode 100644 index 0000000..584c4c3 --- /dev/null +++ b/Sources/AppwriteModels/Transaction.swift @@ -0,0 +1,94 @@ +import Foundation +import JSONCodable + +/// Transaction +open class Transaction: Codable { + + enum CodingKeys: String, CodingKey { + case id = "$id" + case createdAt = "$createdAt" + case updatedAt = "$updatedAt" + case status = "status" + case operations = "operations" + case expiresAt = "expiresAt" + } + + /// Transaction ID. + public let id: String + + /// Transaction creation time in ISO 8601 format. + public let createdAt: String + + /// Transaction update date in ISO 8601 format. + public let updatedAt: String + + /// Current status of the transaction. One of: pending, committing, committed, rolled_back, failed. + public let status: String + + /// Number of operations in the transaction. + public let operations: Int + + /// Expiration time in ISO 8601 format. + public let expiresAt: String + + + init( + id: String, + createdAt: String, + updatedAt: String, + status: String, + operations: Int, + expiresAt: String + ) { + self.id = id + self.createdAt = createdAt + self.updatedAt = updatedAt + self.status = status + self.operations = operations + self.expiresAt = expiresAt + } + + public required init(from decoder: Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + + self.id = try container.decode(String.self, forKey: .id) + self.createdAt = try container.decode(String.self, forKey: .createdAt) + self.updatedAt = try container.decode(String.self, forKey: .updatedAt) + self.status = try container.decode(String.self, forKey: .status) + self.operations = try container.decode(Int.self, forKey: .operations) + self.expiresAt = try container.decode(String.self, forKey: .expiresAt) + } + + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + + try container.encode(id, forKey: .id) + try container.encode(createdAt, forKey: .createdAt) + try container.encode(updatedAt, forKey: .updatedAt) + try container.encode(status, forKey: .status) + try container.encode(operations, forKey: .operations) + try container.encode(expiresAt, forKey: .expiresAt) + } + + public func toMap() -> [String: Any] { + return [ + "$id": id as Any, + "$createdAt": createdAt as Any, + "$updatedAt": updatedAt as Any, + "status": status as Any, + "operations": operations as Any, + "expiresAt": expiresAt as Any + ] + } + + public static func from(map: [String: Any] ) -> Transaction { + return Transaction( + id: map["$id"] as! String, + createdAt: map["$createdAt"] as! String, + updatedAt: map["$updatedAt"] as! String, + status: map["status"] as! String, + operations: map["operations"] as! Int, + expiresAt: map["expiresAt"] as! String + ) + } +} diff --git a/Sources/AppwriteModels/TransactionList.swift b/Sources/AppwriteModels/TransactionList.swift new file mode 100644 index 0000000..49a976a --- /dev/null +++ b/Sources/AppwriteModels/TransactionList.swift @@ -0,0 +1,54 @@ +import Foundation +import JSONCodable + +/// Transaction List +open class TransactionList: Codable { + + enum CodingKeys: String, CodingKey { + case total = "total" + case transactions = "transactions" + } + + /// Total number of transactions that matched your query. + public let total: Int + + /// List of transactions. + public let transactions: [Transaction] + + + init( + total: Int, + transactions: [Transaction] + ) { + self.total = total + self.transactions = transactions + } + + public required init(from decoder: Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + + self.total = try container.decode(Int.self, forKey: .total) + self.transactions = try container.decode([Transaction].self, forKey: .transactions) + } + + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + + try container.encode(total, forKey: .total) + try container.encode(transactions, forKey: .transactions) + } + + public func toMap() -> [String: Any] { + return [ + "total": total as Any, + "transactions": transactions.map { $0.toMap() } as Any + ] + } + + public static func from(map: [String: Any] ) -> TransactionList { + return TransactionList( + total: map["total"] as! Int, + transactions: (map["transactions"] as! [[String: Any]]).map { Transaction.from(map: $0) } + ) + } +} diff --git a/docs/examples/databases/create-document.md b/docs/examples/databases/create-document.md index c044eee..d7fa796 100644 --- a/docs/examples/databases/create-document.md +++ b/docs/examples/databases/create-document.md @@ -17,6 +17,7 @@ let document = try await databases.createDocument( "age": 30, "isAdmin": false ], - permissions: ["read("any")"] // optional + permissions: ["read("any")"], // optional + transactionId: "" // optional ) diff --git a/docs/examples/databases/create-operations.md b/docs/examples/databases/create-operations.md new file mode 100644 index 0000000..7c22de3 --- /dev/null +++ b/docs/examples/databases/create-operations.md @@ -0,0 +1,23 @@ +import Appwrite + +let client = Client() + .setEndpoint("https://.cloud.appwrite.io/v1") // Your API Endpoint + .setProject("") // Your project ID + +let databases = Databases(client) + +let transaction = try await databases.createOperations( + transactionId: "", + operations: [ + { + "action": "create", + "databaseId": "", + "collectionId": "", + "documentId": "", + "data": { + "name": "Walter O'Brien" + } + } + ] // optional +) + diff --git a/docs/examples/databases/create-transaction.md b/docs/examples/databases/create-transaction.md new file mode 100644 index 0000000..4319907 --- /dev/null +++ b/docs/examples/databases/create-transaction.md @@ -0,0 +1,12 @@ +import Appwrite + +let client = Client() + .setEndpoint("https://.cloud.appwrite.io/v1") // Your API Endpoint + .setProject("") // Your project ID + +let databases = Databases(client) + +let transaction = try await databases.createTransaction( + ttl: 60 // optional +) + diff --git a/docs/examples/databases/decrement-document-attribute.md b/docs/examples/databases/decrement-document-attribute.md index 8ef2637..714d7ba 100644 --- a/docs/examples/databases/decrement-document-attribute.md +++ b/docs/examples/databases/decrement-document-attribute.md @@ -12,6 +12,7 @@ let document = try await databases.decrementDocumentAttribute( documentId: "", attribute: "", value: 0, // optional - min: 0 // optional + min: 0, // optional + transactionId: "" // optional ) diff --git a/docs/examples/databases/delete-document.md b/docs/examples/databases/delete-document.md index 301203d..4d8f507 100644 --- a/docs/examples/databases/delete-document.md +++ b/docs/examples/databases/delete-document.md @@ -9,6 +9,7 @@ let databases = Databases(client) let result = try await databases.deleteDocument( databaseId: "", collectionId: "", - documentId: "" + documentId: "", + transactionId: "" // optional ) diff --git a/docs/examples/databases/delete-transaction.md b/docs/examples/databases/delete-transaction.md new file mode 100644 index 0000000..134d72d --- /dev/null +++ b/docs/examples/databases/delete-transaction.md @@ -0,0 +1,12 @@ +import Appwrite + +let client = Client() + .setEndpoint("https://.cloud.appwrite.io/v1") // Your API Endpoint + .setProject("") // Your project ID + +let databases = Databases(client) + +let result = try await databases.deleteTransaction( + transactionId: "" +) + diff --git a/docs/examples/databases/get-document.md b/docs/examples/databases/get-document.md index 6e4dc55..89c89a3 100644 --- a/docs/examples/databases/get-document.md +++ b/docs/examples/databases/get-document.md @@ -10,6 +10,7 @@ let document = try await databases.getDocument( databaseId: "", collectionId: "", documentId: "", - queries: [] // optional + queries: [], // optional + transactionId: "" // optional ) diff --git a/docs/examples/databases/get-transaction.md b/docs/examples/databases/get-transaction.md new file mode 100644 index 0000000..6274ff8 --- /dev/null +++ b/docs/examples/databases/get-transaction.md @@ -0,0 +1,12 @@ +import Appwrite + +let client = Client() + .setEndpoint("https://.cloud.appwrite.io/v1") // Your API Endpoint + .setProject("") // Your project ID + +let databases = Databases(client) + +let transaction = try await databases.getTransaction( + transactionId: "" +) + diff --git a/docs/examples/databases/increment-document-attribute.md b/docs/examples/databases/increment-document-attribute.md index f64b2cd..9c771a7 100644 --- a/docs/examples/databases/increment-document-attribute.md +++ b/docs/examples/databases/increment-document-attribute.md @@ -12,6 +12,7 @@ let document = try await databases.incrementDocumentAttribute( documentId: "", attribute: "", value: 0, // optional - max: 0 // optional + max: 0, // optional + transactionId: "" // optional ) diff --git a/docs/examples/databases/list-documents.md b/docs/examples/databases/list-documents.md index 0d624f3..528d999 100644 --- a/docs/examples/databases/list-documents.md +++ b/docs/examples/databases/list-documents.md @@ -9,6 +9,7 @@ let databases = Databases(client) let documentList = try await databases.listDocuments( databaseId: "", collectionId: "", - queries: [] // optional + queries: [], // optional + transactionId: "" // optional ) diff --git a/docs/examples/databases/list-transactions.md b/docs/examples/databases/list-transactions.md new file mode 100644 index 0000000..3f889c2 --- /dev/null +++ b/docs/examples/databases/list-transactions.md @@ -0,0 +1,12 @@ +import Appwrite + +let client = Client() + .setEndpoint("https://.cloud.appwrite.io/v1") // Your API Endpoint + .setProject("") // Your project ID + +let databases = Databases(client) + +let transactionList = try await databases.listTransactions( + queries: [] // optional +) + diff --git a/docs/examples/databases/update-document.md b/docs/examples/databases/update-document.md index af224c8..d626d7d 100644 --- a/docs/examples/databases/update-document.md +++ b/docs/examples/databases/update-document.md @@ -11,6 +11,7 @@ let document = try await databases.updateDocument( collectionId: "", documentId: "", data: [:], // optional - permissions: ["read("any")"] // optional + permissions: ["read("any")"], // optional + transactionId: "" // optional ) diff --git a/docs/examples/databases/update-transaction.md b/docs/examples/databases/update-transaction.md new file mode 100644 index 0000000..96705d0 --- /dev/null +++ b/docs/examples/databases/update-transaction.md @@ -0,0 +1,14 @@ +import Appwrite + +let client = Client() + .setEndpoint("https://.cloud.appwrite.io/v1") // Your API Endpoint + .setProject("") // Your project ID + +let databases = Databases(client) + +let transaction = try await databases.updateTransaction( + transactionId: "", + commit: false, // optional + rollback: false // optional +) + diff --git a/docs/examples/databases/upsert-document.md b/docs/examples/databases/upsert-document.md index 3e1bf83..8e2a4a0 100644 --- a/docs/examples/databases/upsert-document.md +++ b/docs/examples/databases/upsert-document.md @@ -11,6 +11,7 @@ let document = try await databases.upsertDocument( collectionId: "", documentId: "", data: [:], - permissions: ["read("any")"] // optional + permissions: ["read("any")"], // optional + transactionId: "" // optional ) diff --git a/docs/examples/tablesdb/create-operations.md b/docs/examples/tablesdb/create-operations.md new file mode 100644 index 0000000..c4032cc --- /dev/null +++ b/docs/examples/tablesdb/create-operations.md @@ -0,0 +1,23 @@ +import Appwrite + +let client = Client() + .setEndpoint("https://.cloud.appwrite.io/v1") // Your API Endpoint + .setProject("") // Your project ID + +let tablesDB = TablesDB(client) + +let transaction = try await tablesDB.createOperations( + transactionId: "", + operations: [ + { + "action": "create", + "databaseId": "", + "tableId": "", + "rowId": "", + "data": { + "name": "Walter O'Brien" + } + } + ] // optional +) + diff --git a/docs/examples/tablesdb/create-row.md b/docs/examples/tablesdb/create-row.md index 2ee6013..4d66980 100644 --- a/docs/examples/tablesdb/create-row.md +++ b/docs/examples/tablesdb/create-row.md @@ -17,6 +17,7 @@ let row = try await tablesDB.createRow( "age": 30, "isAdmin": false ], - permissions: ["read("any")"] // optional + permissions: ["read("any")"], // optional + transactionId: "" // optional ) diff --git a/docs/examples/tablesdb/create-transaction.md b/docs/examples/tablesdb/create-transaction.md new file mode 100644 index 0000000..366aa5b --- /dev/null +++ b/docs/examples/tablesdb/create-transaction.md @@ -0,0 +1,12 @@ +import Appwrite + +let client = Client() + .setEndpoint("https://.cloud.appwrite.io/v1") // Your API Endpoint + .setProject("") // Your project ID + +let tablesDB = TablesDB(client) + +let transaction = try await tablesDB.createTransaction( + ttl: 60 // optional +) + diff --git a/docs/examples/tablesdb/decrement-row-column.md b/docs/examples/tablesdb/decrement-row-column.md index ab8ac5b..8a41d43 100644 --- a/docs/examples/tablesdb/decrement-row-column.md +++ b/docs/examples/tablesdb/decrement-row-column.md @@ -12,6 +12,7 @@ let row = try await tablesDB.decrementRowColumn( rowId: "", column: "", value: 0, // optional - min: 0 // optional + min: 0, // optional + transactionId: "" // optional ) diff --git a/docs/examples/tablesdb/delete-row.md b/docs/examples/tablesdb/delete-row.md index b527aca..6ddd1c5 100644 --- a/docs/examples/tablesdb/delete-row.md +++ b/docs/examples/tablesdb/delete-row.md @@ -9,6 +9,7 @@ let tablesDB = TablesDB(client) let result = try await tablesDB.deleteRow( databaseId: "", tableId: "", - rowId: "" + rowId: "", + transactionId: "" // optional ) diff --git a/docs/examples/tablesdb/delete-transaction.md b/docs/examples/tablesdb/delete-transaction.md new file mode 100644 index 0000000..12cf45f --- /dev/null +++ b/docs/examples/tablesdb/delete-transaction.md @@ -0,0 +1,12 @@ +import Appwrite + +let client = Client() + .setEndpoint("https://.cloud.appwrite.io/v1") // Your API Endpoint + .setProject("") // Your project ID + +let tablesDB = TablesDB(client) + +let result = try await tablesDB.deleteTransaction( + transactionId: "" +) + diff --git a/docs/examples/tablesdb/get-row.md b/docs/examples/tablesdb/get-row.md index bc2e2c6..7a3aa40 100644 --- a/docs/examples/tablesdb/get-row.md +++ b/docs/examples/tablesdb/get-row.md @@ -10,6 +10,7 @@ let row = try await tablesDB.getRow( databaseId: "", tableId: "", rowId: "", - queries: [] // optional + queries: [], // optional + transactionId: "" // optional ) diff --git a/docs/examples/tablesdb/get-transaction.md b/docs/examples/tablesdb/get-transaction.md new file mode 100644 index 0000000..fe3cbf7 --- /dev/null +++ b/docs/examples/tablesdb/get-transaction.md @@ -0,0 +1,12 @@ +import Appwrite + +let client = Client() + .setEndpoint("https://.cloud.appwrite.io/v1") // Your API Endpoint + .setProject("") // Your project ID + +let tablesDB = TablesDB(client) + +let transaction = try await tablesDB.getTransaction( + transactionId: "" +) + diff --git a/docs/examples/tablesdb/increment-row-column.md b/docs/examples/tablesdb/increment-row-column.md index 550d668..29b346e 100644 --- a/docs/examples/tablesdb/increment-row-column.md +++ b/docs/examples/tablesdb/increment-row-column.md @@ -12,6 +12,7 @@ let row = try await tablesDB.incrementRowColumn( rowId: "", column: "", value: 0, // optional - max: 0 // optional + max: 0, // optional + transactionId: "" // optional ) diff --git a/docs/examples/tablesdb/list-rows.md b/docs/examples/tablesdb/list-rows.md index 94853c2..dee2ab9 100644 --- a/docs/examples/tablesdb/list-rows.md +++ b/docs/examples/tablesdb/list-rows.md @@ -9,6 +9,7 @@ let tablesDB = TablesDB(client) let rowList = try await tablesDB.listRows( databaseId: "", tableId: "", - queries: [] // optional + queries: [], // optional + transactionId: "" // optional ) diff --git a/docs/examples/tablesdb/list-transactions.md b/docs/examples/tablesdb/list-transactions.md new file mode 100644 index 0000000..b7edd2d --- /dev/null +++ b/docs/examples/tablesdb/list-transactions.md @@ -0,0 +1,12 @@ +import Appwrite + +let client = Client() + .setEndpoint("https://.cloud.appwrite.io/v1") // Your API Endpoint + .setProject("") // Your project ID + +let tablesDB = TablesDB(client) + +let transactionList = try await tablesDB.listTransactions( + queries: [] // optional +) + diff --git a/docs/examples/tablesdb/update-row.md b/docs/examples/tablesdb/update-row.md index 87a8f27..cd5591d 100644 --- a/docs/examples/tablesdb/update-row.md +++ b/docs/examples/tablesdb/update-row.md @@ -11,6 +11,7 @@ let row = try await tablesDB.updateRow( tableId: "", rowId: "", data: [:], // optional - permissions: ["read("any")"] // optional + permissions: ["read("any")"], // optional + transactionId: "" // optional ) diff --git a/docs/examples/tablesdb/update-transaction.md b/docs/examples/tablesdb/update-transaction.md new file mode 100644 index 0000000..2c0e6a7 --- /dev/null +++ b/docs/examples/tablesdb/update-transaction.md @@ -0,0 +1,14 @@ +import Appwrite + +let client = Client() + .setEndpoint("https://.cloud.appwrite.io/v1") // Your API Endpoint + .setProject("") // Your project ID + +let tablesDB = TablesDB(client) + +let transaction = try await tablesDB.updateTransaction( + transactionId: "", + commit: false, // optional + rollback: false // optional +) + diff --git a/docs/examples/tablesdb/upsert-row.md b/docs/examples/tablesdb/upsert-row.md index ed95da7..955935a 100644 --- a/docs/examples/tablesdb/upsert-row.md +++ b/docs/examples/tablesdb/upsert-row.md @@ -11,6 +11,7 @@ let row = try await tablesDB.upsertRow( tableId: "", rowId: "", data: [:], // optional - permissions: ["read("any")"] // optional + permissions: ["read("any")"], // optional + transactionId: "" // optional )