1 | ;
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.DB = exports.ENCODING_OPTS = void 0;
|
4 | const level = require('level-mem');
|
5 | exports.ENCODING_OPTS = { keyEncoding: 'binary', valueEncoding: 'binary' };
|
6 | /**
|
7 | * DB is a thin wrapper around the underlying levelup db,
|
8 | * which validates inputs and sets encoding type.
|
9 | */
|
10 | class DB {
|
11 | /**
|
12 | * Initialize a DB instance. If `leveldb` is not provided, DB
|
13 | * defaults to an [in-memory store](https://github.com/Level/memdown).
|
14 | * @param leveldb - An abstract-leveldown compliant store
|
15 | */
|
16 | constructor(leveldb) {
|
17 | this._leveldb = leveldb !== null && leveldb !== void 0 ? leveldb : level();
|
18 | }
|
19 | /**
|
20 | * Retrieves a raw value from leveldb.
|
21 | * @param key
|
22 | * @returns A Promise that resolves to `Buffer` if a value is found or `null` if no value is found.
|
23 | */
|
24 | async get(key) {
|
25 | let value = null;
|
26 | try {
|
27 | value = await this._leveldb.get(key, exports.ENCODING_OPTS);
|
28 | }
|
29 | catch (error) {
|
30 | if (error.notFound) {
|
31 | // not found, returning null
|
32 | }
|
33 | else {
|
34 | throw error;
|
35 | }
|
36 | }
|
37 | return value;
|
38 | }
|
39 | /**
|
40 | * Writes a value directly to leveldb.
|
41 | * @param key The key as a `Buffer`
|
42 | * @param value The value to be stored
|
43 | */
|
44 | async put(key, val) {
|
45 | await this._leveldb.put(key, val, exports.ENCODING_OPTS);
|
46 | }
|
47 | /**
|
48 | * Removes a raw value in the underlying leveldb.
|
49 | * @param keys
|
50 | */
|
51 | async del(key) {
|
52 | await this._leveldb.del(key, exports.ENCODING_OPTS);
|
53 | }
|
54 | /**
|
55 | * Performs a batch operation on db.
|
56 | * @param opStack A stack of levelup operations
|
57 | */
|
58 | async batch(opStack) {
|
59 | await this._leveldb.batch(opStack, exports.ENCODING_OPTS);
|
60 | }
|
61 | /**
|
62 | * Returns a copy of the DB instance, with a reference
|
63 | * to the **same** underlying leveldb instance.
|
64 | */
|
65 | copy() {
|
66 | return new DB(this._leveldb);
|
67 | }
|
68 | }
|
69 | exports.DB = DB;
|
70 | //# sourceMappingURL=db.js.map |
\ | No newline at end of file |