1
1
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'
3
3
import { TaruiSqlDriver } from './driver'
4
4
import type { Promisable , TauriSqlDB } from './type'
5
5
6
- export interface TauriSqlDialectConfig {
6
+ export interface TauriSqlDialectConfig < T extends 'sqlite' | 'mysql' | 'postgres' > {
7
7
/**
8
8
* Tauri database instance.
9
9
*
10
10
* @example
11
11
* ```ts
12
+ * import Database from "tauri-plugin-sql-api"
13
+ * import { appDataDir } from "@tauri-apps/api/path"
14
+ *
12
15
* const kysely = new Kysely<DB>({
16
+ * type: 'sqlite',
13
17
* dialect: new TauriSqlDialect({
14
- * database: Database.load(`sqlite: ${await appDataDir()}test.db`)
18
+ * database: (prefix) => Database.load(`${prefix} ${await appDataDir()}test.db`)
15
19
* }),
16
20
* })
17
21
* ```
18
22
*/
19
- database : Promisable < TauriSqlDB > | ( ( ) => Promisable < TauriSqlDB > )
23
+ database : Promisable < TauriSqlDB > | ( ( prefix : T extends 'sqlite' ? `${T } :` : `${T } ://`) => Promisable < TauriSqlDB > )
24
+ type : T
20
25
/**
21
26
* Called once when the first query is executed.
22
27
*
@@ -27,28 +32,52 @@ export interface TauriSqlDialectConfig {
27
32
/**
28
33
* https://github.com/tauri-apps/plugins-workspace/tree/dev/plugins/sql
29
34
*/
30
- export class TauriSqlDialect {
31
- #config: TauriSqlDialectConfig
35
+ export class TauriSqlDialect < T extends 'sqlite' | 'mysql' | 'postgres' > {
36
+ #config: TauriSqlDialectConfig < T >
32
37
/**
33
38
* currently no support for bigint
34
39
*/
35
- constructor ( config : TauriSqlDialectConfig ) {
36
- this . #config = config
40
+ constructor ( config : TauriSqlDialectConfig < T > ) {
41
+ this . #config = {
42
+ ...config ,
43
+ type : config . type ?? 'sqlite' ,
44
+ }
37
45
}
38
46
39
47
createDriver ( ) : Driver {
40
48
return new TaruiSqlDriver ( this . #config)
41
49
}
42
50
43
51
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
+ }
45
60
}
46
61
47
62
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
+ }
49
71
}
50
72
51
73
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
+ }
53
82
}
54
83
}
0 commit comments