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
50 changes: 21 additions & 29 deletions client-sdk-references/swift.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

## Kotlin Multiplatform -> Native Swift SDK

The PowerSync Swift SDK currently makes use of the [PowerSync Kotlin Multiplatform SDK](https://github.com/powersync-ja/powersync-kotlin) with the API tool [SKIE](https://skie.touchlab.co/) and KMMBridge under the hood to help generate and publish a Swift package. We will move to an entirely Swift native SDK in v1 and do not expect there to be any breaking changes.

Check warning on line 24 in client-sdk-references/swift.mdx

View check run for this annotation

Mintlify / Mintlify Validation - vale-spellcheck

client-sdk-references/swift.mdx#L24

Did you really mean 'KMMBridge'?

<Tip>
**Demo App**
Expand Down Expand Up @@ -72,7 +72,7 @@
)
```

2. Using Xcode:

Check warning on line 75 in client-sdk-references/swift.mdx

View check run for this annotation

Mintlify / Mintlify Validation - vale-spellcheck

client-sdk-references/swift.mdx#L75

Did you really mean 'Xcode'?

1. Follow [this](https://developer.apple.com/documentation/xcode/adding-package-dependencies-to-your-app#Add-a-package-dependency) guide to add a package to your project.
2. When searching for the package use [https://github.com/powersync-ja/powersync-swift.git](https://github.com/powersync-ja/powersync-swift.git) as the URL and include the exact version e.g. `1.0.0-Beta.x` as shown below:
Expand All @@ -81,17 +81,6 @@
<img src="/images/Swift-Xcode.png"></img>
</Frame>

### Migrating from the Alpha to the Beta SDK

The beta version of this SDK introduces a Swift-native wrapper around the package generated from the Kotlin Multiplatform SDK, bringing a more Swift-native interface for developers. This results in several API updates from the more Kotlin-based alpha version. The main changes are:
* The `PowerSyncDatabase` no longer needs a driver argument and it must be removed.
* The interface for `PowerSyncDatabase` now uses `PowerSyncDatabaseProtocol` which may require some changes to databases uses.
* If you were using `__uploadData` and `__fetchCredentials` in your `PowerSyncBackendConnector` you must remove the `__` and update the methods to `uploadData` and `fetchCredentials`.
* `@MainThread` usage is no longer required and should be removed.
* Implementing `SuspendTaskWrapper` for database transactions is no longer required and should be removed.

See the [To-Do List Demo app](https://github.com/powersync-ja/powersync-swift/tree/main/Demo) as a reference.

## Getting Started

Before implementing the PowerSync SDK in your project, make sure you have completed these steps:
Expand Down Expand Up @@ -203,12 +192,12 @@
// implement fetchCredentials to obtain the necessary credentials to connect to your backend
// See an example implementation in https://github.com/powersync-ja/powersync-swift/blob/main/Demo/PowerSyncExample/PowerSync/SupabaseConnector.swift

return {
endpoint: '[Your PowerSync instance URL or self-hosted endpoint]',
return PowerSyncCredentials(
endpoint: "Your PowerSync instance URL or self-hosted endpoint",
// Use a development token (see Authentication Setup https://docs.powersync.com/installation/authentication-setup/development-tokens)
// to get up and running quickly) to get up and running quickly
token: 'An authentication token'
}
token: "An authentication token"
)
}

override func uploadData(database: PowerSyncDatabaseProtocol) async throws {
Expand Down Expand Up @@ -288,19 +277,23 @@
```swift
// You can watch any SQL query
func watchLists(_ callback: @escaping (_ lists: [ListContent]) -> Void ) async {
for await lists in self.db.watch(
sql: "SELECT * FROM \(LISTS_TABLE)",
parameters: [],
mapper: { cursor in
ListContent(
id: try cursor.getString(name: "id")!,
name: try cursor.getString(name: "name")!,
createdAt: try cursor.getString(name: "created_at")!,
ownerId: try cursor.getString(name: "owner_id")!
)
do {
for try await lists in try self.db.watch<ListContent>(
sql: "SELECT * FROM \(LISTS_TABLE)",
parameters: [],
mapper: { cursor in
try ListContent(
id: cursor.getString(name: "id"),
name: cursor.getString(name: "name"),
createdAt: cursor.getString(name: "created_at"),
ownerId: cursor.getString(name: "owner_id")
)
}
) {
callback(lists)
}
) {
callback(lists)
} catch {
print("Error in watch: \(error)")
}
}
```
Expand All @@ -326,11 +319,10 @@

func deleteTodo(id: String) async throws {
try await db.writeTransaction(callback: { transaction in
_ = transaction.execute(
_ = try transaction.execute(
sql: "DELETE FROM \(TODOS_TABLE) WHERE id = ?",
parameters: [id]
)
return
})
}
```
14 changes: 14 additions & 0 deletions client-sdk-references/swift/migrating-from-alpha-to-beta.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
title: "Migrating from Alpha to Beta"
---

### Steps to migrate

The beta version of this SDK introduces a Swift-native wrapper around the package generated from the Kotlin Multiplatform SDK, bringing a more Swift-native interface for developers. This results in several API updates from the more Kotlin-based alpha version. The main changes are:
* The `PowerSyncDatabase` no longer needs a driver argument and it must be removed.
* The interface for `PowerSyncDatabase` now uses `PowerSyncDatabaseProtocol` which may require some changes to databases uses.
* If you were using `__uploadData` and `__fetchCredentials` in your `PowerSyncBackendConnector` you must remove the `__` and update the methods to `uploadData` and `fetchCredentials`.
* `@MainThread` usage is no longer required and should be removed.
* Implementing `SuspendTaskWrapper` for database transactions is no longer required and should be removed.

See the [To-Do List Demo app](https://github.com/powersync-ja/powersync-swift/tree/main/Demo) as a reference.
2 changes: 1 addition & 1 deletion migration-guides/mongodb-atlas.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
Here is a quick overview of the resulting PowerSync architecture:
* **PowerSync Service** is available as a cloud-hosted service ([PowerSync Cloud](https://powersync.com/pricing)), or you can self-host using our Open Edition.
* **Authentication**: PowerSync piggybacks off your app’s existing authentication, and JWTs are used to authenticate between clients and the PowerSync Service. If you are using Atlas Device SDKs for authentication, you will need to implement an authentication provider.
* **PowerSync Client SDKs** use **SQLite** under the hood. Even though MongoDB is a “NoSQL” document database, PowerSync’s use of SQLite works well with MongoDB, since the [PowerSync protocol](/architecture/powersync-protocol) is schemaless (it syncs schemaless JSON data) and we dynamically apply a [client-side schema](/installation/client-side-setup/define-your-schema) to the data in SQLite using SQLite views. Client-side queries can be written in SQL or you can make use of an ORM (we provide a few [ORM integrations](https://www.powersync.com/blog/using-orms-with-powersync))

Check warning on line 48 in migration-guides/mongodb-atlas.mdx

View check run for this annotation

Mintlify / Mintlify Validation - vale-spellcheck

migration-guides/mongodb-atlas.mdx#L48

Did you really mean 'schemaless'?

Check warning on line 48 in migration-guides/mongodb-atlas.mdx

View check run for this annotation

Mintlify / Mintlify Validation - vale-spellcheck

migration-guides/mongodb-atlas.mdx#L48

Did you really mean 'schemaless'?
* **Reads vs Writes**: PowerSync handles syncing of reads differently from writes.
* **Reads**: The PowerSync Service connects to your MongoDB database and replicates data in real-time to PowerSync clients. Reads are configured using PowerSync’s [“Sync Rules”](/usage/sync-rules/). Sync Rules are more flexible than MongoDB Realm Flexible Sync, but are defined on the server-side, not on the client-side.
* **Writes**: The client-side application can perform writes directly on the local SQLite database. The writes are also automatically placed into an upload queue by the PowerSync Client SDK. The SDK then uses a developer-defined `uploadData()` function to manage the uploading of those writes sequentially to the backend.
Expand All @@ -54,7 +54,7 @@
* **Writes**: The backend controls authorization for how users can modify data.
* **Backend**: PowerSync requires a backend API interface to upload writes to MongoDB. There are currently two options:
* **Custom self-hosted backend**: If you already have a backend application as part of your stack, you should use your existing backend. If you don’t yet have one: We have Node.js, Django and Rails [example implementations](/resources/demo-apps-example-projects#custom-backend-examples) available.
* **Serverless cloud functions (hosted/managed)**: An alternative option is to use CloudCode, a serverless cloud functions environment provided by PowerSync. We have a template available that you can use as a turnkey starting point.

Check warning on line 57 in migration-guides/mongodb-atlas.mdx

View check run for this annotation

Mintlify / Mintlify Validation - vale-spellcheck

migration-guides/mongodb-atlas.mdx#L57

Did you really mean 'Serverless'?

Check warning on line 57 in migration-guides/mongodb-atlas.mdx

View check run for this annotation

Mintlify / Mintlify Validation - vale-spellcheck

migration-guides/mongodb-atlas.mdx#L57

Did you really mean 'serverless'?


## Migration Steps
Expand Down Expand Up @@ -83,7 +83,7 @@

If you have a PowerSync Service instance set up and connected, open the `sync-rules.yaml` file associated with your PowerSync project and edit the SQL-like queries based on your database schema. Below is a simple Sync Rules example using a simple database schema. Sync Rules involve organizing data into ["buckets"](/usage/sync-rules/organize-data-into-buckets) (a bucket is a grouping of data). The example below uses a ["global bucket"](/usage/sync-rules/example-global-data) as a simple starting point — data in a "global bucket" will be synced to all users.

<Info>Note that MongoDB uses “_id” as the name of the ID field in collections whereas PowerSync uses “id” in its client-side database. This is why `SELECT _id as id` should always be used in the data queries when pairing PowerSync with MongoDB.</Info>

Check warning on line 86 in migration-guides/mongodb-atlas.mdx

View check run for this annotation

Mintlify / Mintlify Validation - vale-spellcheck

migration-guides/mongodb-atlas.mdx#L86

Did you really mean '_id'?

```yaml
bucket_definitions:
Expand Down Expand Up @@ -312,7 +312,7 @@

```swift Swift
let schema = AppSchema
let connector = Connector();
let connector = Connector(); // This connector must conform to PowerSyncBackendConnector

let db = PowerSyncDatabase(
schema: schema,
Expand Down Expand Up @@ -443,16 +443,16 @@

MongoDB Atlas Device Sync provides built-in writes/uploads to the backend MongoDB database.

PowerSync offers full custommizability regarding how writes are applied. This gives you control to apply your own business logic, data validations, authorization and conflict resolution logic.

Check warning on line 446 in migration-guides/mongodb-atlas.mdx

View check run for this annotation

Mintlify / Mintlify Validation - vale-spellcheck

migration-guides/mongodb-atlas.mdx#L446

Did you really mean 'custommizability'?

There are two options:

* **Serverless cloud functions (hosted/managed)**: PowerSync offers serverless cloud functions hosted on the same infrastructure as PowerSync Cloud which can used for the needed backend functionality needed. We provide a MongoDB-specific template for this which can be used as a turnkey solution.

Check warning on line 450 in migration-guides/mongodb-atlas.mdx

View check run for this annotation

Mintlify / Mintlify Validation - vale-spellcheck

migration-guides/mongodb-atlas.mdx#L450

Did you really mean 'Serverless'?

Check warning on line 450 in migration-guides/mongodb-atlas.mdx

View check run for this annotation

Mintlify / Mintlify Validation - vale-spellcheck

migration-guides/mongodb-atlas.mdx#L450

Did you really mean 'serverless'?
* **Custom self-hosted backend**: Alternatively, writes can be processed through your own backend.

#### Using PowerSync’s serverless cloud functions

Check warning on line 453 in migration-guides/mongodb-atlas.mdx

View check run for this annotation

Mintlify / Mintlify Validation - vale-spellcheck

migration-guides/mongodb-atlas.mdx#L453

Did you really mean 'serverless'?

PowerSync provides serverless cloud functions for backend functionality, with a template available for MongoDB. See the [step-by-step instructions](/usage/tools/cloudcode) on how to use the template. The template can be customized, or it can be used as-is.

Check warning on line 455 in migration-guides/mongodb-atlas.mdx

View check run for this annotation

Mintlify / Mintlify Validation - vale-spellcheck

migration-guides/mongodb-atlas.mdx#L455

Did you really mean 'serverless'?

The template provides [turnkey conflict resolution](https://www.powersync.com/blog/turnkey-backend-functionality-conflict-resolution-for-powersync#turnkey-conflict-resolution) which roughly matches the built-in conflict resolution behavior provided by MongoDB Atlas Device Sync.

Expand Down
5 changes: 3 additions & 2 deletions mint.json
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,8 @@
"group": "Swift",
"icon": "swift",
"pages": [
"client-sdk-references/swift"
"client-sdk-references/swift",
"client-sdk-references/swift/migrating-from-alpha-to-beta"
]
}
]
Expand Down Expand Up @@ -598,4 +599,4 @@
"youtube": "https://www.youtube.com/@powersync_",
"linkedin": "https://www.linkedin.com/showcase/journeyapps-powersync/"
}
}
}