Skip to content

chore: add kysely test example for transaction with raw sql and update deps #384

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/big-ducks-peel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@powersync/kysely-driver': patch
---

Update kysely to 0.27.4
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ dist
.idea
.fleet
# Useful if running repository in VSCode dev container
.pnpm-store
.pnpm-store
__screenshots__
52 changes: 27 additions & 25 deletions packages/kysely-driver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ The `kysely-driver` package is currently in a beta release.

Set up the PowerSync Database and wrap it with Kysely.

Table column object type definitions are not yet available in JavaScript.

```js
import { wrapPowerSyncWithKysely } from '@powersync/kysely-driver';
import { PowerSyncDatabase } from '@powersync/web';

// Define schema as in: https://docs.powersync.com/usage/installation/client-side-setup/define-your-schema
import { appSchema } from './schema';

export const powerSyncDb = new PowerSyncDatabase({
Expand All @@ -27,8 +27,7 @@ export const powerSyncDb = new PowerSyncDatabase({
export const db = wrapPowerSyncWithKysely(powerSyncDb);
```

When defining the app schema with new `TableV2` declarations, the TypeScript types for tables can be automatically generated.
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`.
With typing for TypeScript:

```TypeScript
import { wrapPowerSyncWithKysely } from '@powersync/kysely-driver';
Expand All @@ -37,15 +36,6 @@ import { PowerSyncDatabase } from "@powersync/web";
// Define schema as in: https://docs.powersync.com/usage/installation/client-side-setup/define-your-schema
import { appSchema } from "./schema";

// If using Schema with TableV2
export type Database = (typeof appSchema)['types'];

// If using Schema with v1 tables
export type Database = {
todos: TodoRecord; // Interface defined externally for Todo item object
lists: ListsRecord; // Interface defined externally for list item object
};

export const powerSyncDb = new PowerSyncDatabase({
database: {
dbFilename: "test.sqlite"
Expand All @@ -68,15 +58,15 @@ Now you are able to use Kysely queries:
```js
const result = await db.selectFrom('users').selectAll().execute();

// {id: '1', name: 'user1', id: '2', name: 'user2'}
// [{ id: '1', name: 'user1' }, { id: '2', name: 'user2' }]
```

- In PowerSync

```js
const result = await powerSyncDb.getAll('SELECT * from users');

// {id: '1', name: 'user1', id: '2', name: 'user2'}
// [{ id: '1', name: 'user1' }, { id: '2', name: 'user2' }]
```

### Insert
Expand All @@ -87,7 +77,7 @@ const result = await powerSyncDb.getAll('SELECT * from users');
await db.insertInto('users').values({ id: '1', name: 'John' }).execute();
const result = await db.selectFrom('users').selectAll().execute();

// {id: '1', name: 'John'}
// [{ id: '1', name: 'John' }]
```

- In PowerSync
Expand All @@ -96,7 +86,7 @@ const result = await db.selectFrom('users').selectAll().execute();
await powerSyncDb.execute('INSERT INTO users (id, name) VALUES(1, ?)', ['John']);
const result = await powerSyncDb.getAll('SELECT * from users');

// {id: '1', name: 'John'}
// [{ id: '1', name: 'John' }]
```

### Delete
Expand All @@ -108,7 +98,7 @@ await db.insertInto('users').values({ id: '2', name: 'Ben' }).execute();
await db.deleteFrom('users').where('name', '=', 'Ben').execute();
const result = await db.selectFrom('users').selectAll().execute();

// { }
// []
```

- In PowerSync
Expand All @@ -118,7 +108,7 @@ await powerSyncDb.execute('INSERT INTO users (id, name) VALUES(2, ?)', ['Ben']);
await powerSyncDb.execute(`DELETE FROM users WHERE name = ?`, ['Ben']);
const result = await powerSyncDb.getAll('SELECT * from users');

// { }
// []
```

### Update
Expand All @@ -130,17 +120,17 @@ await db.insertInto('users').values({ id: '3', name: 'Lucy' }).execute();
await db.updateTable('users').where('name', '=', 'Lucy').set('name', 'Lucy Smith').execute();
const result = await db.selectFrom('users').select('name').executeTakeFirstOrThrow();

// { id: '3', name: 'Lucy Smith' }
// 'Lucy Smith'
```

- In PowerSync

```js
await powerSyncDb.execute('INSERT INTO users (id, name) VALUES(3, ?)', ['Lucy']);
await powerSyncDb.execute('UPDATE users SET name = ? WHERE name = ?', ['Lucy Smith', 'Lucy']);
const result = await powerSyncDb.getAll('SELECT * from users');
const result = await powerSyncDb.get('SELECT name FROM users WHERE name = ?', ['Lucy Smith'])

// { id: '3', name: 'Lucy Smith' }
// 'Lucy Smith'
```

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

// { id: '4', name: 'James Smith' }
// 'James Smith'
```

- In Kysely also using raw SQL

```js
await db.transaction().execute(async (transaction) => {
await sql`INSERT INTO users (id, name) VALUES ('4', 'James');`.execute(transaction)
await transaction.updateTable('users').where('name', '=', 'James').set('name', 'James Smith').execute();
});
const result = await db.selectFrom('users').select('name').executeTakeFirstOrThrow();

// 'James Smith'
```

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

// { id: '4', name: 'James Smith' }
// 'James Smith'
```
16 changes: 8 additions & 8 deletions packages/kysely-driver/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,20 @@
"@powersync/common": "workspace:^1.20.1"
},
"dependencies": {
"kysely": "^0.27.2"
"kysely": "^0.27.4"
},
"devDependencies": {
"@powersync/web": "workspace:*",
"@journeyapps/wa-sqlite": "^0.4.1",
"@types/node": "^20.11.17",
"@vitest/browser": "^1.3.1",
"@types/node": "^20.17.6",
"@vitest/browser": "^2.1.4",
"ts-loader": "^9.5.1",
"ts-node": "^10.9.2",
"typescript": "^5.5.3",
"vite": "^5.1.1",
"vite-plugin-top-level-await": "^1.4.1",
"typescript": "^5.6.3",
"vite": "^5.4.10",
"vite-plugin-top-level-await": "^1.4.4",
"vite-plugin-wasm": "^3.3.0",
"vitest": "^1.3.0",
"webdriverio": "^8.32.3"
"vitest": "^2.1.4",
"webdriverio": "^9.2.8"
}
}
6 changes: 2 additions & 4 deletions packages/kysely-driver/tests/setup/db.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { Schema, TableV2, PowerSyncDatabase, column } from '@powersync/web';
import { wrapPowerSyncWithKysely } from '../../src/sqlite/db';
import { Database } from './types';
import { Schema, PowerSyncDatabase, column, Table } from '@powersync/web';

const users = new TableV2({
const users = new Table({
name: column.text
});

Expand Down
16 changes: 14 additions & 2 deletions packages/kysely-driver/tests/sqlite/db.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import * as SUT from '../../src/sqlite/db';
import { Kysely } from 'kysely';
import { Kysely, sql } from 'kysely';
import { getPowerSyncDb } from '../setup/db';
import { AbstractPowerSyncDatabase } from '@powersync/common';
import { Database } from '../setup/types';
import { Database, UsersTable } from '../setup/types';

describe('CRUD operations', () => {
let powerSyncDb: AbstractPowerSyncDatabase;
Expand Down Expand Up @@ -50,4 +50,16 @@ describe('CRUD operations', () => {

expect(result.name).toEqual('James Smith');
});


it('should insert a user and update that user within a transaction when raw sql is used', async () => {
await db.transaction().execute(async (transaction) => {
await sql`INSERT INTO users (id, name) VALUES ('4', 'James');`.execute(transaction)
await transaction.updateTable('users').where('name', '=', 'James').set('name', 'James Smith').execute();
});
console.log(await db.selectFrom('users').selectAll().execute())
const result = await db.selectFrom('users').select('name').executeTakeFirstOrThrow();

expect(result.name).toEqual('James Smith');
});
});
Loading
Loading