@powersync/kysely-driver
TypeScript icon, indicating that this package has built-in type declarations

0.2.6 • Public • Published

PowerSync Kysely Driver

PowerSync is a service and set of SDKs that keeps Postgres databases in sync with on-device SQLite databases.

This package (packages/kysely-driver) brings the benefits of an ORM through our maintained Kysely driver to PowerSync.

Beta Release

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

Getting started

Set up the PowerSync Database and wrap it with Kysely.

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

import { wrapPowerSyncWithKysely } from '@powersync/kysely-driver';
import { WASQLitePowerSyncDatabaseOpenFactory } from '@powersync/web';
import { appSchema } from './schema';

const factory = new WASQLitePowerSyncDatabaseOpenFactory({
  schema: appSchema,
  dbFilename: 'test.sqlite'
});

export const powerSyncDb = factory.getInstance();

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 of defining the app schema with TableV2.

import { wrapPowerSyncWithKysely } from '@powersync/kysely-driver';
import { WASQLitePowerSyncDatabaseOpenFactory } 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
};

const factory = new WASQLitePowerSyncDatabaseOpenFactory({
  schema: appSchema,
  dbFilename: "test.sqlite",
});

export const powerSyncDb = factory.getInstance();

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

For more information on Kysely typing, see here.

Now you are able to use Kysely queries:

Select

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

// {id: '1', name: 'user1', id: '2', name: 'user2'}
  • In PowerSync
const result = await powerSyncDb.getAll('SELECT * from users');

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

Insert

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

// {id: '1', name: 'John'}
  • In PowerSync
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
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
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
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' }
  • In PowerSync
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');

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

Transaction

  • In Kysely
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();

// { id: '4', name: 'James Smith' }
  • In PowerSync
  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.getAll('SELECT * from users')

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

Readme

Keywords

none

Package Sidebar

Install

npm i @powersync/kysely-driver

Weekly Downloads

47

Version

0.2.6

License

Apache-2.0

Unpacked Size

198 kB

Total Files

14

Last publish

Collaborators

  • journeyapps-platform
  • journeyapps-admin