npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@powersync/kysely-driver

v0.2.5

Published

Kysely driver for PowerSync

Downloads

935

Readme

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