Skip to content

Commit 7589720

Browse files
chore: add kysely test example for transaction with raw sql and update deps (#384)
1 parent 96f1a87 commit 7589720

File tree

7 files changed

+1070
-353
lines changed

7 files changed

+1070
-353
lines changed

.changeset/big-ducks-peel.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@powersync/kysely-driver': patch
3+
---
4+
5+
Update kysely to 0.27.4

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ dist
77
.idea
88
.fleet
99
# Useful if running repository in VSCode dev container
10-
.pnpm-store
10+
.pnpm-store
11+
__screenshots__

packages/kysely-driver/README.md

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ The `kysely-driver` package is currently in a beta release.
1010

1111
Set 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
1614
import { wrapPowerSyncWithKysely } from '@powersync/kysely-driver';
1715
import { PowerSyncDatabase } from '@powersync/web';
16+
17+
// Define schema as in: https://docs.powersync.com/usage/installation/client-side-setup/define-your-schema
1818
import { appSchema } from './schema';
1919

2020
export const powerSyncDb = new PowerSyncDatabase({
@@ -27,8 +27,7 @@ export const powerSyncDb = new PowerSyncDatabase({
2727
export 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
3433
import { 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
3837
import { 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-
4939
export const powerSyncDb = new PowerSyncDatabase({
5040
database: {
5141
dbFilename: "test.sqlite"
@@ -68,15 +58,15 @@ Now you are able to use Kysely queries:
6858
```js
6959
const 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
7767
const 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');
8777
await db.insertInto('users').values({ id: '1', name: 'John' }).execute();
8878
const 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();
9686
await powerSyncDb.execute('INSERT INTO users (id, name) VALUES(1, ?)', ['John']);
9787
const 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();
10898
await db.deleteFrom('users').where('name', '=', 'Ben').execute();
10999
const 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']);
118108
await powerSyncDb.execute(`DELETE FROM users WHERE name = ?`, ['Ben']);
119109
const 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();
130120
await db.updateTable('users').where('name', '=', 'Lucy').set('name', 'Lucy Smith').execute();
131121
const 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
139129
await powerSyncDb.execute('INSERT INTO users (id, name) VALUES(3, ?)', ['Lucy']);
140130
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'])
142132

143-
// { id: '3', name: 'Lucy Smith' }
133+
// 'Lucy Smith'
144134
```
145135

146136
### Transaction
@@ -154,7 +144,19 @@ await db.transaction().execute(async (transaction) => {
154144
});
155145
const result = await db.selectFrom('users').select('name').executeTakeFirstOrThrow();
156146

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'
158160
```
159161

160162
- In PowerSync
@@ -164,7 +166,7 @@ const result = await db.selectFrom('users').select('name').executeTakeFirstOrThr
164166
await transaction.execute('INSERT INTO users (id, name) VALUES(4, ?)', ['James']);
165167
await transaction.execute("UPDATE users SET name = ? WHERE name = ?", ['James Smith', 'James']);
166168
})
167-
const result = await powerSyncDb.getAll('SELECT * from users')
169+
const result = await powerSyncDb.get('SELECT name FROM users WHERE name = ?', ['James Smith'])
168170

169-
// { id: '4', name: 'James Smith' }
171+
// 'James Smith'
170172
```

packages/kysely-driver/package.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,20 @@
2929
"@powersync/common": "workspace:^1.20.1"
3030
},
3131
"dependencies": {
32-
"kysely": "^0.27.2"
32+
"kysely": "^0.27.4"
3333
},
3434
"devDependencies": {
3535
"@powersync/web": "workspace:*",
3636
"@journeyapps/wa-sqlite": "^0.4.1",
37-
"@types/node": "^20.11.17",
38-
"@vitest/browser": "^1.3.1",
37+
"@types/node": "^20.17.6",
38+
"@vitest/browser": "^2.1.4",
3939
"ts-loader": "^9.5.1",
4040
"ts-node": "^10.9.2",
41-
"typescript": "^5.5.3",
42-
"vite": "^5.1.1",
43-
"vite-plugin-top-level-await": "^1.4.1",
41+
"typescript": "^5.6.3",
42+
"vite": "^5.4.10",
43+
"vite-plugin-top-level-await": "^1.4.4",
4444
"vite-plugin-wasm": "^3.3.0",
45-
"vitest": "^1.3.0",
46-
"webdriverio": "^8.32.3"
45+
"vitest": "^2.1.4",
46+
"webdriverio": "^9.2.8"
4747
}
4848
}

packages/kysely-driver/tests/setup/db.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import { Schema, TableV2, PowerSyncDatabase, column } from '@powersync/web';
2-
import { wrapPowerSyncWithKysely } from '../../src/sqlite/db';
3-
import { Database } from './types';
1+
import { Schema, PowerSyncDatabase, column, Table } from '@powersync/web';
42

5-
const users = new TableV2({
3+
const users = new Table({
64
name: column.text
75
});
86

packages/kysely-driver/tests/sqlite/db.test.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
22
import * as SUT from '../../src/sqlite/db';
3-
import { Kysely } from 'kysely';
3+
import { Kysely, sql } from 'kysely';
44
import { getPowerSyncDb } from '../setup/db';
55
import { AbstractPowerSyncDatabase } from '@powersync/common';
6-
import { Database } from '../setup/types';
6+
import { Database, UsersTable } from '../setup/types';
77

88
describe('CRUD operations', () => {
99
let powerSyncDb: AbstractPowerSyncDatabase;
@@ -50,4 +50,16 @@ describe('CRUD operations', () => {
5050

5151
expect(result.name).toEqual('James Smith');
5252
});
53+
54+
55+
it('should insert a user and update that user within a transaction when raw sql is used', async () => {
56+
await db.transaction().execute(async (transaction) => {
57+
await sql`INSERT INTO users (id, name) VALUES ('4', 'James');`.execute(transaction)
58+
await transaction.updateTable('users').where('name', '=', 'James').set('name', 'James Smith').execute();
59+
});
60+
console.log(await db.selectFrom('users').selectAll().execute())
61+
const result = await db.selectFrom('users').select('name').executeTakeFirstOrThrow();
62+
63+
expect(result.name).toEqual('James Smith');
64+
});
5365
});

0 commit comments

Comments
 (0)