Newer
Older
import { AbstractLevel } from 'abstract-level';
import type { AbstractDatabaseOptions } from 'abstract-level';
const MobileLevelPlugin = registerPlugin<MobileLevelPluginT>('MobileLevel', {});
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
interface DatabaseOptions<K, V> extends AbstractDatabaseOptions<K, V> {
/**
* Prefix for the `IDBDatabase` name. Can be set to an empty string.
*
* @defaultValue `'level-js-'`
*/
prefix?: string;
/**
* The version to open the `IDBDatabase` with.
*
* @defaultValue `1`
*/
version?: number | string;
/**
* An {@link AbstractLevel} option that has no effect on {@link BrowserLevel}.
*/
createIfMissing?: boolean;
/**
* An {@link AbstractLevel} option that has no effect on {@link BrowserLevel}.
*/
errorIfExists?: boolean;
}
const textEncoder = new TextEncoder();
class DummyIterator {
public readonly results: [Uint8Array, Uint8Array][];
constructor(results: [Uint8Array, Uint8Array][]) {
this.results = results;
}
all(): [Uint8Array, Uint8Array][] {
return this.results;
}
}
export class MobileLevel extends AbstractLevel<
Uint8Array,
Uint8Array,
Uint8Array
> {
public readonly name: string;
constructor(
name: string,
options: AbstractDatabaseOptions<Uint8Array, Uint8Array> | undefined,
) {
super({}, options);
this.name = name;
if (typeof location !== 'string') {
throw new Error('constructor requires a location string argument');
}
}
async _open(options: unknown): Promise<void> {
if (options)
throw new Error('options not supported, feel free to submit a PR');
await MobileLevelPlugin.open({ path: this.name });
}
async _get(key: Uint8Array): Promise<Uint8Array | null> {
const encodedKey = encode(key);
const { value } = await MobileLevelPlugin.get({ key: encodedKey });
if (value === null) return null;
return decode(value);
}
async _put(key: Uint8Array, value: Uint8Array): Promise<void> {
const encodedKey = encode(key);
const encodedValue = encode(value);
await MobileLevelPlugin.put({ key: encodedKey, value: encodedValue });
}
async _delete(key: Uint8Array): Promise<void> {
const encodedKey = encode(key);
await MobileLevelPlugin.delete({ key: encodedKey });
}
async _close(): Promise<void> {
await MobileLevelPlugin.close();
}
async _batch(
operations: {
type: 'put' | 'delete';
key: Uint8Array;
value?: Uint8Array;
}[],
): Promise<void> {
const encodedOperations = operations.map(({ type, key, value }) => ({
type,
key: encode(key),
value: value ? encode(value) : undefined,
}));
await MobileLevelPlugin.batch({ operations: encodedOperations });
}
async _iterator(options: {
gt?: string;
gte?: string;
lt?: string;
lte?: string;
limit?: number;
reverse?: boolean;
}): Promise<{ results: [key: Uint8Array, value: Uint8Array][] }> {
const { results } = await MobileLevelPlugin.iterator(options);
return {
results: results.map(([key, value]) => [decode(key), decode(value)]),
};
}
async _clear(): Promise<void> {
await MobileLevelPlugin.clear();
}
}