|
@@ -13,30 +13,14 @@ import {
|
|
|
} from 'kysely'
|
|
|
import Database from '@tauri-apps/plugin-sql'
|
|
|
|
|
|
-/**
|
|
|
- * Config for the D1 dialect. Pass your D1 instance to this object that you bound in `wrangler.toml`.
|
|
|
- */
|
|
|
-export interface D1DialectConfig {
|
|
|
+export interface TauriDialectConfig {
|
|
|
database: Database
|
|
|
}
|
|
|
|
|
|
-/**
|
|
|
- * D1 dialect that adds support for [Cloudflare D1][0] in [Kysely][1].
|
|
|
- * The constructor takes the instance of your D1 database that you bound in `wrangler.toml`.
|
|
|
- *
|
|
|
- * ```typescript
|
|
|
- * new D1Dialect({
|
|
|
- * database: env.DB,
|
|
|
- * })
|
|
|
- * ```
|
|
|
- *
|
|
|
- * [0]: https://blog.cloudflare.com/introducing-d1/
|
|
|
- * [1]: https://github.com/koskimas/kysely
|
|
|
- */
|
|
|
-export class D1Dialect implements Dialect {
|
|
|
- #config: D1DialectConfig
|
|
|
-
|
|
|
- constructor(config: D1DialectConfig) {
|
|
|
+export class TauriDialect implements Dialect {
|
|
|
+ #config: TauriDialectConfig
|
|
|
+
|
|
|
+ constructor(config: TauriDialectConfig) {
|
|
|
this.#config = config
|
|
|
}
|
|
|
|
|
@@ -58,48 +42,44 @@ export class D1Dialect implements Dialect {
|
|
|
}
|
|
|
|
|
|
class D1Driver implements Driver {
|
|
|
- #config: D1DialectConfig
|
|
|
+ #config: TauriDialectConfig
|
|
|
|
|
|
- constructor(config: D1DialectConfig) {
|
|
|
+ constructor(config: TauriDialectConfig) {
|
|
|
this.#config = config
|
|
|
}
|
|
|
|
|
|
async init(): Promise<void> {}
|
|
|
|
|
|
async acquireConnection(): Promise<DatabaseConnection> {
|
|
|
- return new D1Connection(this.#config)
|
|
|
+ return new TauriConnection(this.#config)
|
|
|
}
|
|
|
|
|
|
- async beginTransaction(conn: D1Connection): Promise<void> {
|
|
|
+ async beginTransaction(conn: TauriConnection): Promise<void> {
|
|
|
return await conn.beginTransaction()
|
|
|
}
|
|
|
|
|
|
- async commitTransaction(conn: D1Connection): Promise<void> {
|
|
|
+ async commitTransaction(conn: TauriConnection): Promise<void> {
|
|
|
return await conn.commitTransaction()
|
|
|
}
|
|
|
|
|
|
- async rollbackTransaction(conn: D1Connection): Promise<void> {
|
|
|
+ async rollbackTransaction(conn: TauriConnection): Promise<void> {
|
|
|
return await conn.rollbackTransaction()
|
|
|
}
|
|
|
|
|
|
- async releaseConnection(_conn: D1Connection): Promise<void> {}
|
|
|
+ async releaseConnection(_conn: TauriConnection): Promise<void> {}
|
|
|
|
|
|
async destroy(): Promise<void> {}
|
|
|
}
|
|
|
|
|
|
-class D1Connection implements DatabaseConnection {
|
|
|
- #config: D1DialectConfig
|
|
|
+class TauriConnection implements DatabaseConnection {
|
|
|
+ #config: TauriDialectConfig
|
|
|
// #transactionClient?: D1Connection
|
|
|
|
|
|
- constructor(config: D1DialectConfig) {
|
|
|
+ constructor(config: TauriDialectConfig) {
|
|
|
this.#config = config
|
|
|
}
|
|
|
|
|
|
async executeQuery<O>(compiledQuery: CompiledQuery): Promise<QueryResult<O>> {
|
|
|
- console.log('compiledQuery: ', compiledQuery)
|
|
|
- // Transactions are not supported yet.
|
|
|
- // if (this.#transactionClient) return this.#transactionClient.executeQuery(compiledQuery)
|
|
|
-
|
|
|
if (compiledQuery.sql.startsWith('select')) {
|
|
|
const results = await this.#config.database.select<O[]>(compiledQuery.sql, [
|
|
|
...compiledQuery.parameters,
|
|
@@ -121,28 +101,19 @@ class D1Connection implements DatabaseConnection {
|
|
|
: BigInt(results.lastInsertId),
|
|
|
rows: [],
|
|
|
numAffectedRows,
|
|
|
- // @ts-ignore deprecated in kysely >= 0.23, keep for backward compatibility.
|
|
|
numUpdatedOrDeletedRows: numAffectedRows,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
async beginTransaction() {
|
|
|
- // this.#transactionClient = this.#transactionClient ?? new PlanetScaleConnection(this.#config)
|
|
|
- // this.#transactionClient.#conn.execute('BEGIN')
|
|
|
throw new Error('Transactions are not supported yet.')
|
|
|
}
|
|
|
|
|
|
async commitTransaction() {
|
|
|
- // if (!this.#transactionClient) throw new Error('No transaction to commit')
|
|
|
- // this.#transactionClient.#conn.execute('COMMIT')
|
|
|
- // this.#transactionClient = undefined
|
|
|
throw new Error('Transactions are not supported yet.')
|
|
|
}
|
|
|
|
|
|
async rollbackTransaction() {
|
|
|
- // if (!this.#transactionClient) throw new Error('No transaction to rollback')
|
|
|
- // this.#transactionClient.#conn.execute('ROLLBACK')
|
|
|
- // this.#transactionClient = undefined
|
|
|
throw new Error('Transactions are not supported yet.')
|
|
|
}
|
|
|
|