subframe7536 / kysely-sqlite-tools

SQLite tools for Kysely, include auto serialize plugin, sqlite wasm / sqlite worker / Tauri sqlite plugin / bun:sqlite dialects
https://subframe7536.github.io/kysely-sqlite-tools/
MIT License
54 stars 4 forks source link

Tauri's dialect has incorrect types? #1

Closed Aeryle closed 1 year ago

Aeryle commented 1 year ago

Hello,

I have been trying the kysely-dialect-tauri module, but since there is no documentation, or examples in any form or shape that could help, I have been trying to understand how to use it, and here is my code:

import {
    Kysely,
    type Generated,
    DummyDriver,
    SqliteAdapter,
    SqliteIntrospector,
    SqliteQueryCompiler,
} from 'kysely';
import { TauriSqlDialect } from 'kysely-dialect-tauri';
import { writable } from 'svelte/store';
import Db from 'tauri-plugin-sql-api';

interface Person {
    id: Generated<number>;
    first_name: string;
    last_name: string | null;
}

interface Database {
    person: Person;
}

export const initKysely = () =>
    new Kysely<Database>({
        dialect: new TauriSqlDialect({
            database: Db.load('sqlite:test.db'),
        }),
    });

export const dbStore = writable<Kysely<Database>>();

I can't seem to get the types to correspond, and unless I'm overseeing something, I don't understand why not use the type already used by the Db.load function?

subframe7536 commented 1 year ago

maybe just a type error. the return type of close does not affect the logic https://github.com/subframe7536/kysely-sqlite-tools/blob/4fde7802e16cd9d250e0997764d42a131946910d/packages/dialect-tauri/src/driver.ts#L48-L50

Aeryle commented 1 year ago

I think those types are related to the error I get:

image

Maybe Db.load() from the tauri-plugin-sql-api is not the API to use? Do you have an example, docs or template I could check to see how it should be done?

subframe7536 commented 1 year ago
import Database from "tauri-plugin-sql-api";
import { Generated, Kysely } from "kysely";
import { TauriSqlDialect } from "kysely-dialect-tauri";
import { appDataDir } from "@tauri-apps/api/path";

interface DB {
  test: TestTable
}
interface TestTable {
  id: Generated<number>
  name: string
  age: number
  int8: Uint8Array
}
export async function test() {

  const kysely = new Kysely<DB>({
    dialect: new TauriSqlDialect({
      database: async () => await Database.load(
        `sqlite:${await appDataDir()}test.db`
      ) as any // bypass typecheck, fixed in https://github.com/subframe7536/kysely-sqlite-tools/commit/4fde7802e16cd9d250e0997764d42a131946910d
    }),
  })
  await kysely.schema.dropTable('test').execute()
  await kysely.schema.createTable('test')
    .addColumn('id', 'integer', builder => builder.autoIncrement().primaryKey())
    .addColumn('name', 'text')
    .addColumn('age', 'integer')
    .addColumn('int8', 'blob')
    .execute()
  await kysely.insertInto('test')
    .values({
      age: 18,
      name: `test at ${Date.now()}`,
      int8: new Uint8Array([1, 2, 3]),
    })
    .execute()
  console.table(await kysely
    .selectFrom('test')
    .selectAll()
    .executeTakeFirstOrThrow()
  )
}

call test() in Vue image