|
| 1 | +<p align="center"> |
| 2 | + <a href="https://www.powersync.com" target="_blank"><img src="https://github.com/powersync-ja/.github/assets/19345049/602bafa0-41ce-4cee-a432-56848c278722"/></a> |
| 3 | +</p> |
| 4 | + |
| 5 | +# PowerSync Kysely Driver |
| 6 | + |
| 7 | +[PowerSync](https://powersync.com) is a service and set of SDKs that keeps Postgres databases in sync with on-device SQLite databases. |
| 8 | + |
| 9 | +This package (`packages/kysely-driver`) brings the benefits of an ORM through our maintained [Kysely](https://kysely.dev/) driver to PowerSync. |
| 10 | + |
| 11 | +## Getting started |
| 12 | + |
| 13 | +Setup the PowerSync Database and wrap it with Kysely. |
| 14 | + |
| 15 | +```js |
| 16 | +import { wrapPowerSyncWithKysely } from '@powersync/kysely-driver'; |
| 17 | +import { WASQLitePowerSyncDatabaseOpenFactory } from "@journeyapps/powersync-sdk-web"; |
| 18 | +import { appSchema } from "./schema"; |
| 19 | +import { Database } from "./types"; |
| 20 | + |
| 21 | +const factory = new WASQLitePowerSyncDatabaseOpenFactory({ |
| 22 | + schema: appSchema, |
| 23 | + dbFilename: "test.sqlite", |
| 24 | +}); |
| 25 | + |
| 26 | +export const powerSyncDb = factory.getInstance(); |
| 27 | + |
| 28 | +export const db = wrapPowerSyncWithKysely<Database>(powerSyncDb) |
| 29 | +``` |
| 30 | + |
| 31 | +For more information on Kysely typing [here](https://kysely.dev/docs/getting-started#types). |
| 32 | + |
| 33 | +Now you are able to use Kysely queries: |
| 34 | + |
| 35 | +### Select |
| 36 | + |
| 37 | +* In Kysely |
| 38 | + |
| 39 | +```js |
| 40 | + const result = await db.selectFrom('users').selectAll().execute(); |
| 41 | + |
| 42 | + // {id: '1', name: 'user1', id: '2', name: 'user2'} |
| 43 | +``` |
| 44 | + |
| 45 | +* In PowerSync |
| 46 | + |
| 47 | +```js |
| 48 | + const result = await powerSyncDb.getAll('SELECT * from users') |
| 49 | + |
| 50 | + // {id: '1', name: 'user1', id: '2', name: 'user2'} |
| 51 | +``` |
| 52 | + |
| 53 | +### Insert |
| 54 | + |
| 55 | +* In Kysely |
| 56 | + |
| 57 | +```js |
| 58 | + await db.insertInto('users').values({ id: '1', name: 'John' }).execute(); |
| 59 | + const result = await db.selectFrom('users').selectAll().execute(); |
| 60 | + |
| 61 | + // {id: '1', name: 'John'} |
| 62 | +``` |
| 63 | + |
| 64 | +* In PowerSync |
| 65 | + |
| 66 | +```js |
| 67 | + await powerSyncDb.execute('INSERT INTO users (id, name) VALUES(1, ?)', ['John']); |
| 68 | + const result = await powerSyncDb.getAll('SELECT * from users') |
| 69 | + |
| 70 | + // {id: '1', name: 'John'} |
| 71 | +``` |
| 72 | + |
| 73 | +### Delete |
| 74 | + |
| 75 | +* In Kysely |
| 76 | + |
| 77 | +```js |
| 78 | + await db.insertInto('users').values({ id: '2', name: 'Ben' }).execute(); |
| 79 | + await db.deleteFrom('users').where('name', '=', 'Ben').execute(); |
| 80 | + const result = await db.selectFrom('users').selectAll().execute(); |
| 81 | + |
| 82 | + // { } |
| 83 | +``` |
| 84 | + |
| 85 | +* In PowerSync |
| 86 | + |
| 87 | +```js |
| 88 | + await powerSyncDb.execute('INSERT INTO users (id, name) VALUES(2, ?)', ['Ben']); |
| 89 | + await powerSyncDb.execute(`DELETE FROM users WHERE name = ?`, ['Ben']); |
| 90 | + const result = await powerSyncDb.getAll('SELECT * from users') |
| 91 | + |
| 92 | + // { } |
| 93 | +``` |
| 94 | + |
| 95 | +### Update |
| 96 | + |
| 97 | +* In Kysely |
| 98 | + |
| 99 | +```js |
| 100 | + await db.insertInto('users').values({ id: '3', name: 'Lucy' }).execute(); |
| 101 | + await db.updateTable('users').where('name', '=', 'Lucy').set('name', 'Lucy Smith').execute(); |
| 102 | + const result = await db.selectFrom('users').select('name').executeTakeFirstOrThrow(); |
| 103 | + |
| 104 | + // { id: '3', name: 'Lucy Smith' } |
| 105 | +``` |
| 106 | + |
| 107 | +* In PowerSync |
| 108 | + |
| 109 | +```js |
| 110 | + await powerSyncDb.execute('INSERT INTO users (id, name) VALUES(3, ?)', ['Lucy']); |
| 111 | + await powerSyncDb.execute("UPDATE users SET name = ? WHERE name = ?", ['Lucy Smith', 'Lucy']); |
| 112 | + const result = await powerSyncDb.getAll('SELECT * from users') |
| 113 | + |
| 114 | + // { id: '3', name: 'Lucy Smith' } |
| 115 | +``` |
| 116 | + |
| 117 | +### Transaction |
| 118 | + |
| 119 | +* In Kysely |
| 120 | + |
| 121 | +```js |
| 122 | + await db.transaction().execute(async (transaction) => { |
| 123 | + await transaction.insertInto('users').values({ id: '4', name: 'James' }).execute(); |
| 124 | + await transaction.updateTable('users').where('name', '=', 'James').set('name', 'James Smith').execute(); |
| 125 | + }); |
| 126 | + const result = await db.selectFrom('users').select('name').executeTakeFirstOrThrow(); |
| 127 | + |
| 128 | + // { id: '4', name: 'James Smith' } |
| 129 | +``` |
| 130 | + |
| 131 | +* In PowerSync |
| 132 | + |
| 133 | +```js |
| 134 | + await powerSyncDb.writeTransaction((transaction) => { |
| 135 | + await transaction.execute('INSERT INTO users (id, name) VALUES(4, ?)', ['James']); |
| 136 | + await transaction.execute("UPDATE users SET name = ? WHERE name = ?", ['James Smith', 'James']); |
| 137 | + }) |
| 138 | + const result = await powerSyncDb.getAll('SELECT * from users') |
| 139 | + |
| 140 | + // { id: '4', name: 'James Smith' } |
| 141 | +``` |
0 commit comments