1
- import type { Compilable , CompiledQuery , KyselyPlugin , LogEvent , QueryResult , RawBuilder , Sql , Transaction } from 'kysely'
1
+ import type { Compilable , CompiledQuery , LogEvent , QueryResult , RawBuilder , Sql , Transaction } from 'kysely'
2
2
import { Kysely , sql } from 'kysely'
3
3
import { SqliteSerializePlugin } from 'kysely-plugin-serialize'
4
4
import type { Simplify } from 'kysely/dist/cjs/util/type-utils'
5
5
import { parseTableMap , precompileQuery , runCreateTable } from './utils'
6
6
import type { AvailableBuilder , Logger , QueryBuilderOutput , QueryBuilderResult , SqliteBuilderOption , Table } from './types'
7
7
import { Stack } from './stack'
8
8
9
- type DBStatus =
10
- | 'needDrop'
11
- | 'noNeedDrop'
12
- | 'ready'
13
- | 'destroy'
9
+ const DBState = {
10
+ needDrop : 0 ,
11
+ noNeedDrop : 1 ,
12
+ ready : 2 ,
13
+ destroyed : 3 ,
14
+ } as const
14
15
15
16
export class SqliteBuilder < DB extends Record < string , any > > {
16
17
public kysely : Kysely < DB >
17
- private status : DBStatus
18
+ private status : typeof DBState [ keyof typeof DBState ]
18
19
private tableMap : Map < string , Table < DB [ keyof DB & string ] > >
19
20
private logger ?: Logger
20
21
private trxs : Stack < Transaction < DB > >
21
22
public constructor ( option : SqliteBuilderOption < DB > ) {
22
- const { dialect, tables, dropTableBeforeInit : truncateBeforeInit , onQuery, plugins : additionalPlugin , logger } = option
23
+ const { dialect, tables, dropTableBeforeInit = false , onQuery, plugins = [ ] , logger } = option
23
24
this . logger = logger
24
- const plugins : KyselyPlugin [ ] = additionalPlugin ?? [ ]
25
25
plugins . push ( new SqliteSerializePlugin ( ) )
26
26
this . kysely = new Kysely < DB > ( {
27
27
dialect,
@@ -32,27 +32,27 @@ export class SqliteBuilder<DB extends Record<string, any>> {
32
32
} ,
33
33
plugins,
34
34
} )
35
- this . status = truncateBeforeInit
36
- ? ' needDrop'
37
- : ' noNeedDrop'
35
+ this . status = dropTableBeforeInit
36
+ ? DBState . needDrop
37
+ : DBState . noNeedDrop
38
38
this . tableMap = parseTableMap ( tables )
39
39
this . trxs = new Stack ( )
40
40
}
41
41
42
42
public async init ( dropTableBeforeInit = false ) : Promise < SqliteBuilder < DB > > {
43
- const drop = dropTableBeforeInit || this . status === ' needDrop'
43
+ const drop = dropTableBeforeInit || this . status === DBState . needDrop
44
44
await runCreateTable ( this . kysely , this . tableMap , drop )
45
- this . status = ' ready'
45
+ this . status = DBState . ready
46
46
return this
47
47
}
48
48
49
49
private async isFailToInitDB ( ) : Promise < boolean > {
50
- if ( this . status === 'destroy' ) {
50
+ if ( this . status === DBState . destroyed ) {
51
51
this . logger ?. error ( 'DB have been destroyed' )
52
52
return true
53
53
}
54
- this . status !== ' ready' && await this . init ( )
55
- if ( this . status === ' ready' ) {
54
+ this . status !== DBState . ready && await this . init ( )
55
+ if ( this . status === DBState . ready ) {
56
56
return false
57
57
}
58
58
this . logger ?. error ( 'fail to init DB' )
@@ -199,7 +199,7 @@ export class SqliteBuilder<DB extends Record<string, any>> {
199
199
*/
200
200
public async destroy ( ) {
201
201
await this . kysely . destroy ( )
202
- this . status = 'destroy'
202
+ this . status = DBState . destroyed
203
203
this . tableMap . clear ( )
204
204
this . trxs . clear ( )
205
205
}
0 commit comments