Skip to content

Commit a655179

Browse files
committed
feat(builder): convert db state to const object
1 parent ba64fad commit a655179

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

packages/sqlite-builder/src/builder.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
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'
22
import { Kysely, sql } from 'kysely'
33
import { SqliteSerializePlugin } from 'kysely-plugin-serialize'
44
import type { Simplify } from 'kysely/dist/cjs/util/type-utils'
55
import { parseTableMap, precompileQuery, runCreateTable } from './utils'
66
import type { AvailableBuilder, Logger, QueryBuilderOutput, QueryBuilderResult, SqliteBuilderOption, Table } from './types'
77
import { Stack } from './stack'
88

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
1415

1516
export class SqliteBuilder<DB extends Record<string, any>> {
1617
public kysely: Kysely<DB>
17-
private status: DBStatus
18+
private status: typeof DBState[keyof typeof DBState]
1819
private tableMap: Map<string, Table<DB[keyof DB & string]>>
1920
private logger?: Logger
2021
private trxs: Stack<Transaction<DB>>
2122
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
2324
this.logger = logger
24-
const plugins: KyselyPlugin[] = additionalPlugin ?? []
2525
plugins.push(new SqliteSerializePlugin())
2626
this.kysely = new Kysely<DB>({
2727
dialect,
@@ -32,27 +32,27 @@ export class SqliteBuilder<DB extends Record<string, any>> {
3232
},
3333
plugins,
3434
})
35-
this.status = truncateBeforeInit
36-
? 'needDrop'
37-
: 'noNeedDrop'
35+
this.status = dropTableBeforeInit
36+
? DBState.needDrop
37+
: DBState.noNeedDrop
3838
this.tableMap = parseTableMap(tables)
3939
this.trxs = new Stack()
4040
}
4141

4242
public async init(dropTableBeforeInit = false): Promise<SqliteBuilder<DB>> {
43-
const drop = dropTableBeforeInit || this.status === 'needDrop'
43+
const drop = dropTableBeforeInit || this.status === DBState.needDrop
4444
await runCreateTable(this.kysely, this.tableMap, drop)
45-
this.status = 'ready'
45+
this.status = DBState.ready
4646
return this
4747
}
4848

4949
private async isFailToInitDB(): Promise<boolean> {
50-
if (this.status === 'destroy') {
50+
if (this.status === DBState.destroyed) {
5151
this.logger?.error('DB have been destroyed')
5252
return true
5353
}
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) {
5656
return false
5757
}
5858
this.logger?.error('fail to init DB')
@@ -199,7 +199,7 @@ export class SqliteBuilder<DB extends Record<string, any>> {
199199
*/
200200
public async destroy() {
201201
await this.kysely.destroy()
202-
this.status = 'destroy'
202+
this.status = DBState.destroyed
203203
this.tableMap.clear()
204204
this.trxs.clear()
205205
}

0 commit comments

Comments
 (0)