@@ -10,11 +10,11 @@ The `kysely-driver` package is currently in a beta release.
10
10
11
11
Set up the PowerSync Database and wrap it with Kysely.
12
12
13
- Table column object type definitions are not yet available in JavaScript.
14
-
15
13
``` js
16
14
import { wrapPowerSyncWithKysely } from ' @powersync/kysely-driver' ;
17
15
import { PowerSyncDatabase } from ' @powersync/web' ;
16
+
17
+ // Define schema as in: https://docs.powersync.com/usage/installation/client-side-setup/define-your-schema
18
18
import { appSchema } from ' ./schema' ;
19
19
20
20
export const powerSyncDb = new PowerSyncDatabase ({
@@ -27,8 +27,7 @@ export const powerSyncDb = new PowerSyncDatabase({
27
27
export const db = wrapPowerSyncWithKysely (powerSyncDb);
28
28
```
29
29
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:
32
31
33
32
``` TypeScript
34
33
import { wrapPowerSyncWithKysely } from ' @powersync/kysely-driver' ;
@@ -37,15 +36,6 @@ import { PowerSyncDatabase } from "@powersync/web";
37
36
// Define schema as in: https://docs.powersync.com/usage/installation/client-side-setup/define-your-schema
38
37
import { appSchema } from " ./schema" ;
39
38
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
-
49
39
export const powerSyncDb = new PowerSyncDatabase ({
50
40
database: {
51
41
dbFilename: " test.sqlite"
@@ -68,15 +58,15 @@ Now you are able to use Kysely queries:
68
58
``` js
69
59
const result = await db .selectFrom (' users' ).selectAll ().execute ();
70
60
71
- // { id: '1', name: 'user1', id: '2', name: 'user2'}
61
+ // [{ id: '1', name: 'user1' }, { id: '2', name: 'user2' }]
72
62
```
73
63
74
64
- In PowerSync
75
65
76
66
``` js
77
67
const result = await powerSyncDb .getAll (' SELECT * from users' );
78
68
79
- // { id: '1', name: 'user1', id: '2', name: 'user2'}
69
+ // [{ id: '1', name: 'user1' }, { id: '2', name: 'user2' }]
80
70
```
81
71
82
72
### Insert
@@ -87,7 +77,7 @@ const result = await powerSyncDb.getAll('SELECT * from users');
87
77
await db .insertInto (' users' ).values ({ id: ' 1' , name: ' John' }).execute ();
88
78
const result = await db .selectFrom (' users' ).selectAll ().execute ();
89
79
90
- // { id: '1', name: 'John'}
80
+ // [{ id: '1', name: 'John' }]
91
81
```
92
82
93
83
- In PowerSync
@@ -96,7 +86,7 @@ const result = await db.selectFrom('users').selectAll().execute();
96
86
await powerSyncDb .execute (' INSERT INTO users (id, name) VALUES(1, ?)' , [' John' ]);
97
87
const result = await powerSyncDb .getAll (' SELECT * from users' );
98
88
99
- // { id: '1', name: 'John'}
89
+ // [{ id: '1', name: 'John' }]
100
90
```
101
91
102
92
### Delete
@@ -108,7 +98,7 @@ await db.insertInto('users').values({ id: '2', name: 'Ben' }).execute();
108
98
await db .deleteFrom (' users' ).where (' name' , ' =' , ' Ben' ).execute ();
109
99
const result = await db .selectFrom (' users' ).selectAll ().execute ();
110
100
111
- // { }
101
+ // []
112
102
```
113
103
114
104
- In PowerSync
@@ -118,7 +108,7 @@ await powerSyncDb.execute('INSERT INTO users (id, name) VALUES(2, ?)', ['Ben']);
118
108
await powerSyncDb .execute (` DELETE FROM users WHERE name = ?` , [' Ben' ]);
119
109
const result = await powerSyncDb .getAll (' SELECT * from users' );
120
110
121
- // { }
111
+ // []
122
112
```
123
113
124
114
### Update
@@ -130,17 +120,17 @@ await db.insertInto('users').values({ id: '3', name: 'Lucy' }).execute();
130
120
await db .updateTable (' users' ).where (' name' , ' =' , ' Lucy' ).set (' name' , ' Lucy Smith' ).execute ();
131
121
const result = await db .selectFrom (' users' ).select (' name' ).executeTakeFirstOrThrow ();
132
122
133
- // { id: '3', name: ' Lucy Smith' }
123
+ // ' Lucy Smith'
134
124
```
135
125
136
126
- In PowerSync
137
127
138
128
``` js
139
129
await powerSyncDb .execute (' INSERT INTO users (id, name) VALUES(3, ?)' , [' Lucy' ]);
140
130
await 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 ' ])
142
132
143
- // { id: '3', name: ' Lucy Smith' }
133
+ // ' Lucy Smith'
144
134
```
145
135
146
136
### Transaction
@@ -154,7 +144,19 @@ await db.transaction().execute(async (transaction) => {
154
144
});
155
145
const result = await db .selectFrom (' users' ).select (' name' ).executeTakeFirstOrThrow ();
156
146
157
- // { id: '4', name: 'James Smith' }
147
+ // 'James Smith'
148
+ ```
149
+
150
+ - In Kysely also using raw SQL
151
+
152
+ ``` js
153
+ await db .transaction ().execute (async (transaction ) => {
154
+ await sql ` INSERT INTO users (id, name) VALUES (' 4' , ' James' );` .execute (transaction)
155
+ await transaction .updateTable (' users' ).where (' name' , ' =' , ' James' ).set (' name' , ' James Smith' ).execute ();
156
+ });
157
+ const result = await db .selectFrom (' users' ).select (' name' ).executeTakeFirstOrThrow ();
158
+
159
+ // 'James Smith'
158
160
```
159
161
160
162
- In PowerSync
@@ -164,7 +166,7 @@ const result = await db.selectFrom('users').select('name').executeTakeFirstOrThr
164
166
await transaction .execute (' INSERT INTO users (id, name) VALUES(4, ?)' , [' James' ]);
165
167
await transaction .execute (" UPDATE users SET name = ? WHERE name = ?" , [' James Smith' , ' James' ]);
166
168
})
167
- const result = await powerSyncDb .getAll (' SELECT * from users' )
169
+ const result = await powerSyncDb .get (' SELECT name FROM users WHERE name = ? ' , [ ' James Smith ' ] )
168
170
169
- // { id: '4', name: ' James Smith' }
171
+ // ' James Smith'
170
172
```
0 commit comments