Skip to content

Commit 54e4e4f

Browse files
committed
feat(dialect-tauri): add support for mysql and postgres
1 parent 05259c1 commit 54e4e4f

File tree

2 files changed

+44
-15
lines changed

2 files changed

+44
-15
lines changed

packages/dialect-tauri/src/driver.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@ import { CompiledQuery } from 'kysely'
33
import type { TauriSqlDB } from './type'
44
import type { TauriSqlDialectConfig } from '.'
55

6-
export class TaruiSqlDriver {
7-
private config: TauriSqlDialectConfig
6+
export class TaruiSqlDriver<T extends 'sqlite' | 'mysql' | 'postgres'> {
7+
private config: TauriSqlDialectConfig<T>
88
private db?: TauriSqlDB
99
private connectionMutex = new ConnectionMutex()
1010
private connection?: DatabaseConnection
11-
constructor(config: TauriSqlDialectConfig) {
11+
constructor(config: TauriSqlDialectConfig<T>) {
1212
this.config = config
1313
}
1414

1515
async init(): Promise<void> {
1616
this.db = typeof this.config.database === 'function'
17-
? await this.config.database()
17+
? await this.config.database(`${this.config.type}:${this.config.type === 'sqlite' ? '' : '//'}` as any)
1818
: await this.config.database
1919
this.connection = new TauriSqlConnection(this.db)
2020
if (this.config.onCreateConnection) {
Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
import type { DatabaseConnection, DatabaseIntrospector, DialectAdapter, Driver, Kysely, QueryCompiler } from 'kysely'
2-
import { SqliteAdapter, SqliteIntrospector, SqliteQueryCompiler } from 'kysely'
2+
import { MysqlAdapter, MysqlIntrospector, MysqlQueryCompiler, PostgresAdapter, PostgresIntrospector, PostgresQueryCompiler, SqliteAdapter, SqliteIntrospector, SqliteQueryCompiler } from 'kysely'
33
import { TaruiSqlDriver } from './driver'
44
import type { Promisable, TauriSqlDB } from './type'
55

6-
export interface TauriSqlDialectConfig {
6+
export interface TauriSqlDialectConfig<T extends 'sqlite' | 'mysql' | 'postgres'> {
77
/**
88
* Tauri database instance.
99
*
1010
* @example
1111
* ```ts
12+
* import Database from "tauri-plugin-sql-api"
13+
* import { appDataDir } from "@tauri-apps/api/path"
14+
*
1215
* const kysely = new Kysely<DB>({
16+
* type: 'sqlite',
1317
* dialect: new TauriSqlDialect({
14-
* database: Database.load(`sqlite:${await appDataDir()}test.db`)
18+
* database: (prefix) => Database.load(`${prefix}${await appDataDir()}test.db`)
1519
* }),
1620
* })
1721
* ```
1822
*/
19-
database: Promisable<TauriSqlDB> | (() => Promisable<TauriSqlDB>)
23+
database: Promisable<TauriSqlDB> | ((prefix: T extends 'sqlite' ? `${T}:` : `${T}://`) => Promisable<TauriSqlDB>)
24+
type: T
2025
/**
2126
* Called once when the first query is executed.
2227
*
@@ -27,28 +32,52 @@ export interface TauriSqlDialectConfig {
2732
/**
2833
* https://github.com/tauri-apps/plugins-workspace/tree/dev/plugins/sql
2934
*/
30-
export class TauriSqlDialect {
31-
#config: TauriSqlDialectConfig
35+
export class TauriSqlDialect<T extends 'sqlite' | 'mysql' | 'postgres'> {
36+
#config: TauriSqlDialectConfig<T>
3237
/**
3338
* currently no support for bigint
3439
*/
35-
constructor(config: TauriSqlDialectConfig) {
36-
this.#config = config
40+
constructor(config: TauriSqlDialectConfig<T>) {
41+
this.#config = {
42+
...config,
43+
type: config.type ?? 'sqlite',
44+
}
3745
}
3846

3947
createDriver(): Driver {
4048
return new TaruiSqlDriver(this.#config)
4149
}
4250

4351
createQueryCompiler(): QueryCompiler {
44-
return new SqliteQueryCompiler()
52+
switch (this.#config.type) {
53+
case 'mysql':
54+
return new MysqlQueryCompiler()
55+
case 'postgres':
56+
return new PostgresQueryCompiler()
57+
default:
58+
return new SqliteQueryCompiler()
59+
}
4560
}
4661

4762
createAdapter(): DialectAdapter {
48-
return new SqliteAdapter()
63+
switch (this.#config.type) {
64+
case 'mysql':
65+
return new MysqlAdapter()
66+
case 'postgres':
67+
return new PostgresAdapter()
68+
default:
69+
return new SqliteAdapter()
70+
}
4971
}
5072

5173
createIntrospector(db: Kysely<any>): DatabaseIntrospector {
52-
return new SqliteIntrospector(db)
74+
switch (this.#config.type) {
75+
case 'mysql':
76+
return new MysqlIntrospector(db)
77+
case 'postgres':
78+
return new PostgresIntrospector(db)
79+
default:
80+
return new SqliteIntrospector(db)
81+
}
5382
}
5483
}

0 commit comments

Comments
 (0)