-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
92 lines (77 loc) · 2.21 KB
/
index.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { Database, Statement } from 'bun:sqlite'
type Row = {
key: string
value: string
}
const _rows2obj = <T>(rows: Row[]) => {
try {
return rows.reduce<Record<string, T>>((acc, row) => {
const key = row.key
acc[key] = JSON.parse(row.value) as T
return acc
}, {})
} catch (error) {
throw new Error("Can't parse value from database")
}
}
class KV<T> {
db: Database
getQuery: Statement<Row, [string]>
insertQuery: Statement<Row, [string, string, number, number]>
updateQuery: Statement<Row, [string, number, string]>
getAllQuery: Statement<Row, []>
findQuery: Statement<Row, [string]>
deleteQuery: Statement<unknown, [string]>
constructor(path: string) {
const db = (this.db = new Database(path))
db.run(
'CREATE TABLE IF NOT EXISTS items(' +
' key TEXT PRIMARY KEY,' +
' value TEXT,' +
' ctime INTEGER,' +
' mtime INTEGER)',
)
this.getQuery = db.prepare('SELECT * FROM items WHERE key=? LIMIT 1')
this.insertQuery = db.prepare('INSERT INTO items (key,value,ctime,mtime) VALUES (?,?,?,?)')
this.updateQuery = db.prepare('UPDATE items SET value=?, mtime=? WHERE key=?')
this.getAllQuery = db.prepare('SELECT * FROM items')
this.findQuery = db.prepare('SELECT * FROM items WHERE key LIKE ?')
this.deleteQuery = db.prepare('DELETE FROM items WHERE key = ?')
}
close() {
this.db.close()
}
get(key: string) {
const row = this.getQuery.get(key)
if (!row || !row.value) {
return undefined
}
return JSON.parse(row.value) as T
}
set(key: string, value: T) {
const t = Date.now()
const value_p = JSON.stringify(value)
const update = this.get(key)
if (update) {
// update content ==> value=?, mtime=?, key=?'
this.updateQuery.run(value_p, t, key)
return this
}
// initial input ==> key,value,ctime,mtime
this.insertQuery.run(key, value_p, t, t)
return this
}
delete(key: string) {
this.deleteQuery.run(key)
return this
}
all() {
const rows = this.getAllQuery.all()
return _rows2obj<T>(rows)
}
find(prefix: string) {
const rows = this.findQuery.all(prefix + '%')
return _rows2obj<T>(rows)
}
}
export { KV }