-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlevel.ts
More file actions
57 lines (48 loc) · 1.22 KB
/
level.ts
File metadata and controls
57 lines (48 loc) · 1.22 KB
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
import {Level} from "level"
import {Driver} from "../parts/driver.js"
import {Scan, Write} from "../parts/types.js"
export class LevelDriver extends Driver {
#db: Level<string, string>
constructor(path: string) {
super()
this.#db = new Level(path)
}
async gets(...keys: string[]) {
return this.#db.getMany(keys)
}
// TODO when it's released, use level's upcoming .has and .hasMany
// - currently we're using a hack, using .get
// - see https://github.com/Level/community/issues/142
async hasKeys(...keys: string[]) {
if (keys.length === 0) return []
const values = await this.gets(...keys)
return values.map(value => value !== undefined)
}
async *keys(scan: Scan = {}) {
const results = this.#db.keys({
gte: scan.start,
lte: scan.end,
limit: scan.limit,
})
for await (const key of results)
yield key
}
async *entries(scan: Scan = {}) {
const results = this.#db.iterator({
gte: scan.start,
lte: scan.end,
limit: scan.limit,
})
for await (const entry of results)
yield entry
}
async transaction(...writes: Write[]) {
return this.#db.batch(
writes.map(([key, value]) => (
(value === undefined)
? {type: "del", key}
: {type: "put", key, value}
))
)
}
}