-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.ts
46 lines (34 loc) · 1.3 KB
/
client.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import type { Logger } from 'kysely'
import { Kysely } from 'kysely'
import { SQLocalKysely } from 'sqlocal/kysely'
import colors from 'tiny-colors'
import type { Database } from './schema'
import { DataTypePlugin } from './plugins/dataType'
const kyselyLogger: Logger = (event) => {
switch (event.level) {
case 'error':
console.error(colors.bgBlack.red.bold('[KYSELY ERROR]'), event.error)
break
case 'query':
// eslint-disable-next-line no-console
console.log(colors.bgBlack.magenta.bold('[KYSELY QUERY]'), event.query.sql, 'took', `${event.queryDurationMillis.toFixed(1)}ms`)
break
}
}
const databasePath = import.meta.env.PROD ? 'db.sqlite3' : 'development/db.sqlite3'
async function deleteDB() {
let opfsRoot = await window.navigator.storage.getDirectory()
const parts = databasePath.split('/')
for (let index = 0; index < parts.length; index++) {
const element = parts[index]
const isLast = index === parts.length - 1
if (isLast) {
await opfsRoot.removeEntry(element)
break
}
opfsRoot = await opfsRoot.getDirectoryHandle(element)
}
}
const { dialect, sql, destroy } = new SQLocalKysely(databasePath)
const db = new Kysely<Database>({ dialect, log: kyselyLogger, plugins: [new DataTypePlugin()] })
export { sql, db, destroy, deleteDB }