Skip to content

Commit

Permalink
chore: move kysely out of beta (#387)
Browse files Browse the repository at this point in the history
Co-authored-by: DominicGBauer <dominic@nomanini.com>
  • Loading branch information
DominicGBauer and DominicGBauer authored Nov 6, 2024
1 parent 7589720 commit 16291ae
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 123 deletions.
5 changes: 5 additions & 0 deletions .changeset/wise-coats-wash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@powersync/kysely-driver': major
---

Move kysely out of Beta
130 changes: 7 additions & 123 deletions packages/kysely-driver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

This package (`packages/kysely-driver`) brings the benefits of an ORM through our maintained [Kysely](https://kysely.dev/) driver to [PowerSync](https://powersync.com).

## Beta Release

The `kysely-driver` package is currently in a beta release.

## Getting started

Set up the PowerSync Database and wrap it with Kysely.
Expand All @@ -25,6 +21,10 @@ export const powerSyncDb = new PowerSyncDatabase({
});

export const db = wrapPowerSyncWithKysely(powerSyncDb);

const result = await db.selectFrom('users').selectAll().execute();

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

With typing for TypeScript:
Expand All @@ -34,7 +34,7 @@ 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";
import { appSchema, Database } from "./schema";

export const powerSyncDb = new PowerSyncDatabase({
database: {
Expand All @@ -45,128 +45,12 @@ export const powerSyncDb = new PowerSyncDatabase({

// `db` now automatically contains types for defined tables
export const db = wrapPowerSyncWithKysely<Database>(powerSyncDb)
```

For more information on Kysely typing, see [here](https://kysely.dev/docs/getting-started#types).

Now you are able to use Kysely queries:

### Select

- In Kysely

```js
const result = await db.selectFrom('users').selectAll().execute();

// [{ 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' }]
```

### Insert

- In Kysely

```js
await db.insertInto('users').values({ id: '1', name: 'John' }).execute();
const result = await db.selectFrom('users').selectAll().execute();

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

- In PowerSync

```js
await powerSyncDb.execute('INSERT INTO users (id, name) VALUES(1, ?)', ['John']);
const result = await powerSyncDb.getAll('SELECT * from users');

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

### Delete

- In Kysely

```js
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

```js
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

- In Kysely

```js
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();

// '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.get('SELECT name FROM users WHERE name = ?', ['Lucy Smith'])

// 'Lucy Smith'
```

### Transaction

- In Kysely

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

// '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

```js
await powerSyncDb.writeTransaction((transaction) => {
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.get('SELECT name FROM users WHERE name = ?', ['James Smith'])
For more information on Kysely typing, see [here](https://kysely.dev/docs/getting-started#types).

// 'James Smith'
```
For more information on how to use Kysely queries in PowerSync, see [here](https://docs.powersync.com/client-sdk-references/javascript-web/javascript-orm/kysely#usage-examples).

0 comments on commit 16291ae

Please sign in to comment.