@@ -10,11 +10,11 @@ The `kysely-driver` package is currently in a beta release.
1010
1111Set up the PowerSync Database and wrap it with Kysely.
1212
13- Table column object type definitions are not yet available in JavaScript.
14-
1513``` js
1614import { wrapPowerSyncWithKysely } from ' @powersync/kysely-driver' ;
1715import { PowerSyncDatabase } from ' @powersync/web' ;
16+
17+ // Define schema as in: https://docs.powersync.com/usage/installation/client-side-setup/define-your-schema
1818import { appSchema } from ' ./schema' ;
1919
2020export const powerSyncDb = new PowerSyncDatabase ({
@@ -27,8 +27,7 @@ export const powerSyncDb = new PowerSyncDatabase({
2727export const db = wrapPowerSyncWithKysely (powerSyncDb);
2828```
2929
30- When defining the app schema with new ` TableV2 ` declarations, the TypeScript types for tables can be automatically generated.
31- See an [ example] ( https://github.com/powersync-ja/powersync-js/blob/main/demos/react-supabase-todolist/src/library/powersync/AppSchema.ts ) of defining the app schema with ` TableV2 ` .
30+ With typing for TypeScript:
3231
3332``` TypeScript
3433import { wrapPowerSyncWithKysely } from ' @powersync/kysely-driver' ;
@@ -37,15 +36,6 @@ import { PowerSyncDatabase } from "@powersync/web";
3736// Define schema as in: https://docs.powersync.com/usage/installation/client-side-setup/define-your-schema
3837import { appSchema } from " ./schema" ;
3938
40- // If using Schema with TableV2
41- export type Database = (typeof appSchema )[' types' ];
42-
43- // If using Schema with v1 tables
44- export type Database = {
45- todos: TodoRecord ; // Interface defined externally for Todo item object
46- lists: ListsRecord ; // Interface defined externally for list item object
47- };
48-
4939export const powerSyncDb = new PowerSyncDatabase ({
5040 database: {
5141 dbFilename: " test.sqlite"
@@ -68,15 +58,15 @@ Now you are able to use Kysely queries:
6858``` js
6959const result = await db .selectFrom (' users' ).selectAll ().execute ();
7060
71- // { id: '1', name: 'user1', id: '2', name: 'user2'}
61+ // [{ id: '1', name: 'user1' }, { id: '2', name: 'user2' }]
7262```
7363
7464- In PowerSync
7565
7666``` js
7767const result = await powerSyncDb .getAll (' SELECT * from users' );
7868
79- // { id: '1', name: 'user1', id: '2', name: 'user2'}
69+ // [{ id: '1', name: 'user1' }, { id: '2', name: 'user2' }]
8070```
8171
8272### Insert
@@ -87,7 +77,7 @@ const result = await powerSyncDb.getAll('SELECT * from users');
8777await db .insertInto (' users' ).values ({ id: ' 1' , name: ' John' }).execute ();
8878const result = await db .selectFrom (' users' ).selectAll ().execute ();
8979
90- // { id: '1', name: 'John'}
80+ // [{ id: '1', name: 'John' }]
9181```
9282
9383- In PowerSync
@@ -96,7 +86,7 @@ const result = await db.selectFrom('users').selectAll().execute();
9686await powerSyncDb .execute (' INSERT INTO users (id, name) VALUES(1, ?)' , [' John' ]);
9787const result = await powerSyncDb .getAll (' SELECT * from users' );
9888
99- // { id: '1', name: 'John'}
89+ // [{ id: '1', name: 'John' }]
10090```
10191
10292### Delete
@@ -108,7 +98,7 @@ await db.insertInto('users').values({ id: '2', name: 'Ben' }).execute();
10898await db .deleteFrom (' users' ).where (' name' , ' =' , ' Ben' ).execute ();
10999const result = await db .selectFrom (' users' ).selectAll ().execute ();
110100
111- // { }
101+ // []
112102```
113103
114104- In PowerSync
@@ -118,7 +108,7 @@ await powerSyncDb.execute('INSERT INTO users (id, name) VALUES(2, ?)', ['Ben']);
118108await powerSyncDb .execute (` DELETE FROM users WHERE name = ?` , [' Ben' ]);
119109const result = await powerSyncDb .getAll (' SELECT * from users' );
120110
121- // { }
111+ // []
122112```
123113
124114### Update
@@ -130,17 +120,17 @@ await db.insertInto('users').values({ id: '3', name: 'Lucy' }).execute();
130120await db .updateTable (' users' ).where (' name' , ' =' , ' Lucy' ).set (' name' , ' Lucy Smith' ).execute ();
131121const result = await db .selectFrom (' users' ).select (' name' ).executeTakeFirstOrThrow ();
132122
133- // { id: '3', name: ' Lucy Smith' }
123+ // ' Lucy Smith'
134124```
135125
136126- In PowerSync
137127
138128``` js
139129await powerSyncDb .execute (' INSERT INTO users (id, name) VALUES(3, ?)' , [' Lucy' ]);
140130await powerSyncDb .execute (' UPDATE users SET name = ? WHERE name = ?' , [' Lucy Smith' , ' Lucy' ]);
141- const result = await powerSyncDb .getAll (' SELECT * from users' );
131+ const result = await powerSyncDb .get (' SELECT name FROM users WHERE name = ? ' , [ ' Lucy Smith ' ])
142132
143- // { id: '3', name: ' Lucy Smith' }
133+ // ' Lucy Smith'
144134```
145135
146136### Transaction
@@ -154,7 +144,7 @@ await db.transaction().execute(async (transaction) => {
154144});
155145const result = await db .selectFrom (' users' ).select (' name' ).executeTakeFirstOrThrow ();
156146
157- // { id: '4', name: ' James Smith' }
147+ // ' James Smith'
158148```
159149
160150- In PowerSync
@@ -164,7 +154,33 @@ const result = await db.selectFrom('users').select('name').executeTakeFirstOrThr
164154 await transaction .execute (' INSERT INTO users (id, name) VALUES(4, ?)' , [' James' ]);
165155 await transaction .execute (" UPDATE users SET name = ? WHERE name = ?" , [' James Smith' , ' James' ]);
166156 })
167- const result = await powerSyncDb .getAll (' SELECT * from users' )
157+ const result = await powerSyncDb .get (' SELECT name FROM users WHERE name = ?' , [' James Smith' ])
158+
159+ // 'James Smith'
160+ ```
161+
162+ ### Transaction with raw SQL
163+
164+ - In Kysely
165+
166+ ``` js
167+ await db .transaction ().execute (async (transaction ) => {
168+ await sql ` INSERT INTO users (id, name) VALUES (' 4' , ' James' );` .execute (transaction)
169+ await transaction .updateTable (' users' ).where (' name' , ' =' , ' James' ).set (' name' , ' James Smith' ).execute ();
170+ });
171+ const result = await db .selectFrom (' users' ).select (' name' ).executeTakeFirstOrThrow ();
172+
173+ // 'James Smith'
174+ ```
175+
176+ - In PowerSync
177+
178+ ``` js
179+ await powerSyncDb .writeTransaction ((transaction ) => {
180+ await sql ` INSERT INTO users (id, name) VALUES (' 4' , ' James' );` .execute (transaction)
181+ await transaction .execute (" UPDATE users SET name = ? WHERE name = ?" , [' James Smith' , ' James' ]);
182+ })
183+ const result = await powerSyncDb .get (' SELECT name FROM users WHERE name = ?' , [' James Smith' ])
168184
169- // { id: '4', name: ' James Smith' }
185+ // ' James Smith'
170186```
0 commit comments