@capawesome/capacitor-sqlite-kysely
v0.0.1
Published
Kysely dialect for the Capawesome SQLite plugin.
Maintainers
Readme
@capawesome/capacitor-sqlite-kysely
Kysely dialect for @capawesome-team/capacitor-sqlite.
[!WARNING] This package is in early development and may have breaking changes. Feedback and contributions are welcome!
Installation
npm install @capawesome/capacitor-sqlite-kysely kysely@^0.28.0Usage
Setup
import { Sqlite } from '@capawesome-team/capacitor-sqlite';
import { Kysely } from 'kysely';
import { CapacitorSqliteDialect } from '@capawesome/capacitor-sqlite-kysely';
const { databaseId } = await Sqlite.open({ path: 'my.db' });
const db = new Kysely<DB>({
dialect: new CapacitorSqliteDialect(Sqlite, { databaseId }),
});Define Your Database Types
Create a type definition for your database schema:
import { Generated } from 'kysely';
interface Database {
users: UsersTable;
posts: PostsTable;
}
interface UsersTable {
id: Generated<number>;
name: string;
email: string;
}
interface PostsTable {
id: Generated<number>;
title: string;
content: string | null;
author_id: number;
}Queries
// Insert
await db.insertInto('users').values({ name: 'Alice', email: '[email protected]' }).execute();
// Select all
const allUsers = await db.selectFrom('users').selectAll().execute();
// Select with filter
const user = await db
.selectFrom('users')
.selectAll()
.where('email', '=', '[email protected]')
.executeTakeFirst();
// Update
await db.updateTable('users').set({ name: 'Bob' }).where('id', '=', 1).execute();
// Delete
await db.deleteFrom('users').where('id', '=', 1).execute();Transactions
Kysely manages BEGIN, COMMIT, and ROLLBACK automatically:
await db.transaction().execute(async (trx) => {
await trx.insertInto('users').values({ name: 'Alice', email: '[email protected]' }).execute();
await trx.insertInto('posts').values({ title: 'Hello', content: 'World', author_id: 1 }).execute();
});Migrations
Kysely has a built-in migration system. Since CapacitorSqliteDialect implements the standard Dialect interface, you can use Kysely's Migrator directly:
import { Kysely, Migrator, Migration, MigrationProvider } from 'kysely';
const migrationProvider: MigrationProvider = {
async getMigrations() {
return {
'001_create_users': {
async up(db: Kysely<any>) {
await db.schema
.createTable('users')
.addColumn('id', 'integer', (col) => col.primaryKey().autoIncrement())
.addColumn('name', 'text', (col) => col.notNull())
.addColumn('email', 'text', (col) => col.notNull().unique())
.execute();
},
},
};
},
};
const migrator = new Migrator({ db, provider: migrationProvider });
await migrator.migrateToLatest();API
CapacitorSqliteDialect
A Kysely Dialect implementation for @capawesome-team/capacitor-sqlite.
new CapacitorSqliteDialect(client, config);| Param | Type | Description |
| -------- | ------------------------------ | ----------------------------------------------------- |
| client | SqlitePlugin | The Capacitor SQLite plugin instance (e.g. Sqlite). |
| config | CapacitorSqliteDialectConfig | Configuration object. Must include databaseId. |
CapacitorSqliteDialectConfig
| Property | Type | Description |
| ------------ | -------- | ---------------------------------------------------- |
| databaseId | string | The database identifier returned by Sqlite.open(). |
