@@ -10,17 +10,47 @@ This package (`packages/kysely-driver`) brings the benefits of an ORM through ou
1010
1111## Beta Release
1212
13- The ` kysely-driver ` package is currently in a beta release.
13+ The ` kysely-driver ` package is currently in a beta release.
1414
1515## Getting started
1616
1717Setup the PowerSync Database and wrap it with Kysely.
1818
19+ Table column object type definitions are not yet available in JavaScript.
20+
1921``` js
2022import { wrapPowerSyncWithKysely } from ' @powersync/kysely-driver' ;
23+ import { WASQLitePowerSyncDatabaseOpenFactory } from ' @journeyapps/powersync-sdk-web' ;
24+ import { appSchema } from ' ./schema' ;
25+
26+ const factory = new WASQLitePowerSyncDatabaseOpenFactory ({
27+ schema: appSchema,
28+ dbFilename: ' test.sqlite'
29+ });
30+
31+ export const powerSyncDb = factory .getInstance ();
32+
33+ export const db = wrapPowerSyncWithKysely (powerSyncDb);
34+ ```
35+
36+ When defining the app schema with new ` TableV2 ` declarations the TypeScript types for tables can be automatically generated.
37+ See [ example] ( https://github.com/powersync-ja/powersync-js/blob/main/demos/nextjs-supabase-todolist/src/library/powersync/AppSchema.ts ) for defining the app schema with ` TableV2 ` .
38+
39+ ``` TypeScript
40+ import { wrapPowerSyncWithKysely } from ' @powersync/kysely-driver' ;
2141import { WASQLitePowerSyncDatabaseOpenFactory } from " @journeyapps/powersync-sdk-web" ;
42+
43+ // Define schema as in: https://docs.powersync.com/usage/installation/client-side-setup/define-your-schema
2244import { appSchema } from " ./schema" ;
23- import { Database } from " ./types" ;
45+
46+ // If using Schema with TableV2
47+ export type Database = (typeof appSchema )[' types' ];
48+
49+ // If using Schema with v1 tables
50+ export type Database = {
51+ todos: TodoRecord ; // Interface defined externally for Todo item object
52+ lists: ListsRecord ; // Interface defined externally for list item object
53+ };
2454
2555const factory = new WASQLitePowerSyncDatabaseOpenFactory ({
2656 schema: appSchema ,
@@ -29,6 +59,7 @@ const factory = new WASQLitePowerSyncDatabaseOpenFactory({
2959
3060export const powerSyncDb = factory .getInstance ();
3161
62+ // `db` now automatically contains types for defined tables
3263export const db = wrapPowerSyncWithKysely <Database >(powerSyncDb )
3364```
3465
@@ -38,101 +69,101 @@ Now you are able to use Kysely queries:
3869
3970### Select
4071
41- * In Kysely
72+ - In Kysely
4273
4374``` js
44- const result = await db .selectFrom (' users' ).selectAll ().execute ();
75+ const result = await db .selectFrom (' users' ).selectAll ().execute ();
4576
46- // {id: '1', name: 'user1', id: '2', name: 'user2'}
77+ // {id: '1', name: 'user1', id: '2', name: 'user2'}
4778```
4879
49- * In PowerSync
80+ - In PowerSync
5081
5182``` js
52- const result = await powerSyncDb .getAll (' SELECT * from users' )
83+ const result = await powerSyncDb .getAll (' SELECT * from users' );
5384
54- // {id: '1', name: 'user1', id: '2', name: 'user2'}
85+ // {id: '1', name: 'user1', id: '2', name: 'user2'}
5586```
5687
5788### Insert
5889
59- * In Kysely
90+ - In Kysely
6091
6192``` js
62- await db .insertInto (' users' ).values ({ id: ' 1' , name: ' John' }).execute ();
63- const result = await db .selectFrom (' users' ).selectAll ().execute ();
93+ await db .insertInto (' users' ).values ({ id: ' 1' , name: ' John' }).execute ();
94+ const result = await db .selectFrom (' users' ).selectAll ().execute ();
6495
65- // {id: '1', name: 'John'}
96+ // {id: '1', name: 'John'}
6697```
6798
68- * In PowerSync
99+ - In PowerSync
69100
70101``` js
71- await powerSyncDb .execute (' INSERT INTO users (id, name) VALUES(1, ?)' , [' John' ]);
72- const result = await powerSyncDb .getAll (' SELECT * from users' )
102+ await powerSyncDb .execute (' INSERT INTO users (id, name) VALUES(1, ?)' , [' John' ]);
103+ const result = await powerSyncDb .getAll (' SELECT * from users' );
73104
74- // {id: '1', name: 'John'}
105+ // {id: '1', name: 'John'}
75106```
76107
77108### Delete
78109
79- * In Kysely
110+ - In Kysely
80111
81112``` js
82- await db .insertInto (' users' ).values ({ id: ' 2' , name: ' Ben' }).execute ();
83- await db .deleteFrom (' users' ).where (' name' , ' =' , ' Ben' ).execute ();
84- const result = await db .selectFrom (' users' ).selectAll ().execute ();
113+ await db .insertInto (' users' ).values ({ id: ' 2' , name: ' Ben' }).execute ();
114+ await db .deleteFrom (' users' ).where (' name' , ' =' , ' Ben' ).execute ();
115+ const result = await db .selectFrom (' users' ).selectAll ().execute ();
85116
86- // { }
117+ // { }
87118```
88119
89- * In PowerSync
120+ - In PowerSync
90121
91122``` js
92- await powerSyncDb .execute (' INSERT INTO users (id, name) VALUES(2, ?)' , [' Ben' ]);
93- await powerSyncDb .execute (` DELETE FROM users WHERE name = ?` , [' Ben' ]);
94- const result = await powerSyncDb .getAll (' SELECT * from users' )
123+ await powerSyncDb .execute (' INSERT INTO users (id, name) VALUES(2, ?)' , [' Ben' ]);
124+ await powerSyncDb .execute (` DELETE FROM users WHERE name = ?` , [' Ben' ]);
125+ const result = await powerSyncDb .getAll (' SELECT * from users' );
95126
96- // { }
127+ // { }
97128```
98129
99130### Update
100131
101- * In Kysely
132+ - In Kysely
102133
103134``` js
104- await db .insertInto (' users' ).values ({ id: ' 3' , name: ' Lucy' }).execute ();
105- await db .updateTable (' users' ).where (' name' , ' =' , ' Lucy' ).set (' name' , ' Lucy Smith' ).execute ();
106- const result = await db .selectFrom (' users' ).select (' name' ).executeTakeFirstOrThrow ();
135+ await db .insertInto (' users' ).values ({ id: ' 3' , name: ' Lucy' }).execute ();
136+ await db .updateTable (' users' ).where (' name' , ' =' , ' Lucy' ).set (' name' , ' Lucy Smith' ).execute ();
137+ const result = await db .selectFrom (' users' ).select (' name' ).executeTakeFirstOrThrow ();
107138
108- // { id: '3', name: 'Lucy Smith' }
139+ // { id: '3', name: 'Lucy Smith' }
109140```
110141
111- * In PowerSync
142+ - In PowerSync
112143
113144``` js
114- await powerSyncDb .execute (' INSERT INTO users (id, name) VALUES(3, ?)' , [' Lucy' ]);
115- await powerSyncDb .execute (" UPDATE users SET name = ? WHERE name = ?" , [' Lucy Smith' , ' Lucy' ]);
116- const result = await powerSyncDb .getAll (' SELECT * from users' )
145+ await powerSyncDb .execute (' INSERT INTO users (id, name) VALUES(3, ?)' , [' Lucy' ]);
146+ await powerSyncDb .execute (' UPDATE users SET name = ? WHERE name = ?' , [' Lucy Smith' , ' Lucy' ]);
147+ const result = await powerSyncDb .getAll (' SELECT * from users' );
117148
118- // { id: '3', name: 'Lucy Smith' }
149+ // { id: '3', name: 'Lucy Smith' }
119150```
120151
121152### Transaction
122153
123- * In Kysely
154+ - In Kysely
124155
125156``` js
126- await db .transaction ().execute (async (transaction ) => {
127- await transaction .insertInto (' users' ).values ({ id: ' 4' , name: ' James' }).execute ();
128- await transaction .updateTable (' users' ).where (' name' , ' =' , ' James' ).set (' name' , ' James Smith' ).execute ();
129- });
130- const result = await db .selectFrom (' users' ).select (' name' ).executeTakeFirstOrThrow ();
157+ await db .transaction ().execute (async (transaction ) => {
158+ await transaction .insertInto (' users' ).values ({ id: ' 4' , name: ' James' }).execute ();
159+ await transaction .updateTable (' users' ).where (' name' , ' =' , ' James' ).set (' name' , ' James Smith' ).execute ();
160+ });
161+ const result = await db .selectFrom (' users' ).select (' name' ).executeTakeFirstOrThrow ();
131162
132- // { id: '4', name: 'James Smith' }
163+ // { id: '4', name: 'James Smith' }
133164```
134165
135- * In PowerSync
166+ - In PowerSync
136167
137168``` js
138169 await powerSyncDb .writeTransaction ((transaction ) => {
0 commit comments