Skip to content

Commit d995c93

Browse files
chore: change examples to use cursor get by column name: (#106)
* chore: change examples to use cursor get by column name: * docs: update implementations
1 parent 4953d88 commit d995c93

File tree

4 files changed

+50
-50
lines changed

4 files changed

+50
-50
lines changed

client-sdk-references/kotlin-multiplatform.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,9 @@ fun watchCustomers(): Flow<List<User>> {
242242
// TODO: implement your UI based on the result set
243243
return database.watch("SELECT * FROM customers", mapper = { cursor ->
244244
User(
245-
id = cursor.getString(0)!!,
246-
name = cursor.getString(1)!!,
247-
email = cursor.getString(2)!!
245+
id = cursor.getString("id"),
246+
name = cursor.getString("name"),
247+
email = cursor.getString("email")
248248
)
249249
})
250250
}

client-sdk-references/kotlin-multiplatform/usage-examples.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ fun watchCustomers(): Flow<List<User>> {
3030
// TODO: implement your UI based on the result set
3131
return database.watch("SELECT * FROM customers", mapper = { cursor ->
3232
User(
33-
id = cursor.getString(0)!!,
34-
name = cursor.getString(1)!!,
35-
email = cursor.getString(2)!!
33+
id = cursor.getString("id"),
34+
name = cursor.getString("name"),
35+
email = cursor.getString("email")
3636
)
3737
})
3838
}

client-sdk-references/swift.mdx

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,10 @@ func getList(_ id: String) async throws {
249249
parameters: [id],
250250
mapper: { cursor in
251251
ListContent(
252-
id: cursor.getString(index: 0)!,
253-
name: cursor.getString(index: 1)!,
254-
createdAt: cursor.getString(index: 2)!,
255-
ownerId: cursor.getString(index: 3)!
252+
id: try cursor.getString(name: "id")!,
253+
name: try cursor.getString(name: "name")!,
254+
createdAt: try cursor.getString(name: "created_at")!,
255+
ownerId: try cursor.getString(name: "owner_id")!
256256
)
257257
}
258258
)
@@ -271,10 +271,10 @@ func getLists() async throws {
271271
parameters: [],
272272
mapper: { cursor in
273273
ListContent(
274-
id: cursor.getString(index: 0)!,
275-
name: cursor.getString(index: 1)!,
276-
createdAt: cursor.getString(index: 2)!,
277-
ownerId: cursor.getString(index: 3)!
274+
id: try cursor.getString(name: "id")!,
275+
name: try cursor.getString(name: "name")!,
276+
createdAt: try cursor.getString(name: "created_at")!,
277+
ownerId: try cursor.getString(name: "owner_id")!
278278
)
279279
}
280280
)
@@ -293,10 +293,10 @@ func watchLists(_ callback: @escaping (_ lists: [ListContent]) -> Void ) async {
293293
parameters: [],
294294
mapper: { cursor in
295295
ListContent(
296-
id: cursor.getString(index: 0)!,
297-
name: cursor.getString(index: 1)!,
298-
createdAt: cursor.getString(index: 2)!,
299-
ownerId: cursor.getString(index: 3)!
296+
id: try cursor.getString(name: "id")!,
297+
name: try cursor.getString(name: "name")!,
298+
createdAt: try cursor.getString(name: "created_at")!,
299+
ownerId: try cursor.getString(name: "owner_id")!
300300
)
301301
}
302302
) {

migration-guides/mongodb-atlas.mdx

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Here is a quick overview of the resulting PowerSync architecture:
4848
* **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))
4949
* **Reads vs Writes**: PowerSync handles syncing of reads differently from writes.
5050
* **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.
51-
* **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.
51+
* **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.
5252
* **Authorization**: Authorization is controlled separately for reads vs. writes.
5353
* **Reads**: The [Sync Rules](/usage/sync-rules/) control which users can access which data.
5454
* **Writes**: The backend controls authorization for how users can modify data.
@@ -59,41 +59,41 @@ Here is a quick overview of the resulting PowerSync architecture:
5959

6060
## Migration Steps
6161

62-
Follow the steps below to migrate a MongoDB Atlas Device Sync app to PowerSync.
62+
Follow the steps below to migrate a MongoDB Atlas Device Sync app to PowerSync.
6363

6464
### 1. Remove Realm
6565

6666
Before adding PowerSync, remove MongoDB Realm from your project. This includes uninstalling the Realm SDK and deleting all Realm-related code and dependencies. It is also possible to initially run Realm and PowerSync in parallel, and remove Realm later.
6767

6868
### 2. Create PowerSync account and instance
6969

70-
To get started quickly with PowerSync, sign up for a free PowerSync Cloud account [here](https://accounts.journeyapps.com/portal/powersync-signup?s=mongodb-migration-guide).
70+
To get started quickly with PowerSync, sign up for a free PowerSync Cloud account [here](https://accounts.journeyapps.com/portal/powersync-signup?s=mongodb-migration-guide).
7171

7272
It is also possible to self-host PowerSync. An end-to-end demo app using Docker Compose is available [here](https://github.com/powersync-ja/self-host-demo/tree/main/demos/nodejs-mongodb).
7373

7474
### 3. Connect PowerSync to MongoDB
7575

76-
Once your account is set up, [create a new PowerSync instance](/installation/database-connection#create-a-powersync-cloud-instance) and configure the instance to connect to your source [MongoDB database](/installation/database-connection#mongodb-beta-specifics).
76+
Once your account is set up, [create a new PowerSync instance](/installation/database-connection#create-a-powersync-cloud-instance) and configure the instance to connect to your source [MongoDB database](/installation/database-connection#mongodb-beta-specifics).
7777

7878
### 4. Define Sync Rules
7979

8080
Sync Rules allow you to control which data gets synced to which users/devices. Each PowerSync Service instance has a Sync Rules definition that consists of SQL-like queries in a YAML file.
8181

8282
To get a good understanding of how Sync Rules operate, have a look at our blog post: [Sync Rules from First Principles: Partial Replication to SQLite](https://www.powersync.com/blog/sync-rules-from-first-principles-partial-replication-to-sqlite).
8383

84-
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.
84+
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.
8585

8686
<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>
8787

8888
```yaml
8989
bucket_definitions:
9090
# This is the name of the bucket, in this case the global bucket synced to all users.
91-
global:
91+
global:
9292
# This is the query used to determine the data in each bucket
93-
data:
93+
data:
9494
# Note that we select the MongoDB _id field as id
95-
- SELECT _id as id, * FROM lists
96-
- SELECT _id as id, * FROM todos
95+
- SELECT _id as id, * FROM lists
96+
- SELECT _id as id, * FROM todos
9797
```
9898
9999
To filter data based on the user and other more advanced use cases, refer to the [Sync Rules documentation](/usage/sync-rules).
@@ -109,13 +109,13 @@ Add PowerSync to your app project by following the instructions for the relevant
109109
110110
The PowerSync client-side schema represents a “view” of the data synced from the PowerSync Service to the client app. No migrations are required — the schema is applied directly when the local PowerSync SQLite database is constructed.
111111
112-
To make this step easy for you, the [PowerSync Dashboard](/usage/tools/powersync-dashboard) allows automatically generating the client-side schema based on the Sync Rules defined for a PowerSync instance. To generate the schema, go to the [dashboard](https://powersync.journeyapps.com/), right-click on the instance, and select “Generate Client Schema”. Alternatively you can use the PowerSync [CLI](/usage/tools/cli) to generate the schema.
112+
To make this step easy for you, the [PowerSync Dashboard](/usage/tools/powersync-dashboard) allows automatically generating the client-side schema based on the Sync Rules defined for a PowerSync instance. To generate the schema, go to the [dashboard](https://powersync.journeyapps.com/), right-click on the instance, and select “Generate Client Schema”. Alternatively you can use the PowerSync [CLI](/usage/tools/cli) to generate the schema.
113113
114114
Here is an example of a client-side schema for PowerSync using a simple `todos` table:
115115

116116
<CodeGroup>
117117

118-
```typescript TypeScript - React Native
118+
```typescript TypeScript - React Native
119119
import { column, Schema, Table } from '@powersync/react-native';
120120
121121
const todos = new Table(
@@ -127,14 +127,14 @@ Here is an example of a client-side schema for PowerSync using a simple `todos`
127127
created_by: column.text,
128128
completed_by: column.text,
129129
completed: column.integer
130-
},
130+
},
131131
{ indexes: { list: ['list_id'] } }
132132
);
133133
134134
export const AppSchema = new Schema({
135135
todos
136136
});
137-
```
137+
```
138138

139139
```typescript TypeScript - Web
140140
import { column, Schema, Table } from '@powersync/web';
@@ -212,7 +212,7 @@ Here is an example of a client-side schema for PowerSync using a simple `todos`
212212
let AppSchema = Schema(todos)
213213
```
214214

215-
```dart Flutter
215+
```dart Flutter
216216
import 'package:powersync/powersync.dart';
217217
218218
const schema = Schema(([
@@ -235,7 +235,7 @@ Here is an example of a client-side schema for PowerSync using a simple `todos`
235235

236236
A few things to note regarding the PowerSync client-side schema:
237237

238-
* The schema does not explicitly specify an `id` column, since the PowerSync automatically creates an `id` column of type `text`.
238+
* The schema does not explicitly specify an `id` column, since the PowerSync automatically creates an `id` column of type `text`.
239239
* SQLite has very simple data types which are [used by](/usage/sync-rules/types#types) PowerSync.
240240
* For MongoDB specific data types, refer to [MongoDB Type Mapping](/usage/sync-rules/types#mongodb-beta-type-mapping).
241241
* PowerSync also supports [syncing attachments or files](/usage/use-case-examples/attachments-files#attachments-files) using helper packages.
@@ -247,7 +247,7 @@ Now that we have our Sync Rules and client-side schema defined, we can instantia
247247

248248
<CodeGroup>
249249

250-
```typescript TypeScript - React Native
250+
```typescript TypeScript - React Native
251251
import { PowerSyncDatabase } from '@powersync/react-native';
252252
import { Connector } from './Connector';
253253
import { AppSchema } from './Schema';
@@ -263,7 +263,7 @@ Now that we have our Sync Rules and client-side schema defined, we can instantia
263263
const connector = new Connector();
264264
db.connect(connector);
265265
};
266-
```
266+
```
267267

268268
```typescript TypeScript - Web
269269
import { PowerSyncDatabase } from '@powersync/web';
@@ -322,7 +322,7 @@ Now that we have our Sync Rules and client-side schema defined, we can instantia
322322
await db.connect(connector: connector);
323323
```
324324

325-
```dart Flutter
325+
```dart Flutter
326326
import 'package:powersync/powersync.dart';
327327
import 'package:path_provider/path_provider.dart';
328328
import 'package:path/path.dart';
@@ -340,7 +340,7 @@ Now that we have our Sync Rules and client-side schema defined, we can instantia
340340

341341
### 8. Reading and writing data
342342

343-
Reading data in the application which uses PowerSync is very simple: we use SQLite syntax to query data in our local database.
343+
Reading data in the application which uses PowerSync is very simple: we use SQLite syntax to query data in our local database.
344344

345345
<CodeGroup>
346346

@@ -350,7 +350,7 @@ Reading data in the application which uses PowerSync is very simple: we use SQLi
350350
const results = await db.getAll('SELECT * FROM todos');
351351
return results;
352352
}
353-
```
353+
```
354354

355355
```java Kotlin
356356
// Reading Data
@@ -367,19 +367,19 @@ Reading data in the application which uses PowerSync is very simple: we use SQLi
367367
sql: "SELECT * FROM todos",
368368
mapper: { cursor in
369369
TodoContent(
370-
list_id: cursor.getString(index: 0)!,
371-
description: cursor.getString(index: 1)!,
372-
completed: cursor.getString(index: 2)!,
373-
created_by: cursor.getString(index: 3)!,
374-
completed_by: cursor.getString(index: 4)!,
375-
completed_at: cursor.getString(index: 5)!
370+
list_id: try cursor.getString(name: "list_id"),
371+
description: try cursor.getString(name: "description"),
372+
completed: try cursor.getBooleanOptional(name: "completed"),
373+
created_by: try cursor.getString(name: "created_by"),
374+
completed_by: try cursor.getStringOptional(name: "completed_by"),
375+
completed_at: try cursor.getStringOptional(name: "completed_at")
376376
)
377377
}
378378
)
379379
}
380380
```
381381

382-
```dart Flutter
382+
```dart Flutter
383383
/// Reading Data
384384
Future<TodoList> getTodos() async {
385385
final result = await db.get('SELECT * FROM todos');
@@ -398,7 +398,7 @@ The same applies to writing data: `INSERT`, `UPDATE` and `DELETE` statements are
398398
export const insertTodo = async (listId: string, description: string) => {
399399
await db.execute('INSERT INTO todos (id, created_at, list_id, description) VALUES (uuid(), date(), ?, ?)', [listId, description]);
400400
}
401-
```
401+
```
402402

403403
```java Kotlin
404404
// Writing Data
@@ -422,7 +422,7 @@ The same applies to writing data: `INSERT`, `UPDATE` and `DELETE` statements are
422422
}
423423
```
424424

425-
```dart Flutter
425+
```dart Flutter
426426
/// Writing Data
427427
await db.execute(
428428
'INSERT INTO todos (id, created_at, list_id, description) VALUES (uuid(), date(), ?, ?)',
@@ -452,7 +452,7 @@ There are two options:
452452

453453
#### Using PowerSync’s serverless cloud functions
454454

455-
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.
455+
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.
456456

457457
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.
458458

@@ -465,7 +465,7 @@ For more information, see our blog post: [Turnkey Backend Functionality & Confli
465465

466466
This option gives you complete control over the backend. The simplest backend implementation is to simply apply writes to MongoDB as they are received, which results in a last-write-wins conflict resolution strategy (same as the “turnkey backend functionality” option above). See [Writing Client Changes](/installation/app-backend-setup/writing-client-changes) for more details.
467467

468-
On the client-side, you need to wire up the `uploadData()` function in the “backend connector” to use your own backend API. The [App Backend Setup](/installation/app-backend-setup) section of our docs provides step-by-step instructions for this.
468+
On the client-side, you need to wire up the `uploadData()` function in the “backend connector” to use your own backend API. The [App Backend Setup](/installation/app-backend-setup) section of our docs provides step-by-step instructions for this.
469469

470470
Also see the section on [how to set up a simple backend API](https://www.powersync.com/blog/migrating-a-mongodb-atlas-device-sync-app-to-powersync#backend-api-setup) in our practical MongoDB migration [example](https://www.powersync.com/blog/migrating-a-mongodb-atlas-device-sync-app-to-powersync) on our blog.
471471

@@ -474,4 +474,4 @@ We also have [example backend implementations](/resources/demo-apps-example-proj
474474

475475
## Questions? Need help?
476476

477-
[Get in touch](https://www.powersync.com/contact) with us.
477+
[Get in touch](https://www.powersync.com/contact) with us.

0 commit comments

Comments
 (0)