keyv-lmdb 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.d.ts ADDED
@@ -0,0 +1,30 @@
1
+ import { type RootDatabaseOptionsWithPath } from 'lmdb';
2
+ import EventEmitter from 'events';
3
+ import type { KeyvStoreAdapter } from 'keyv';
4
+ export interface Options extends RootDatabaseOptionsWithPath {
5
+ /** LMDB database name (for multiple DBs under one root) */
6
+ name?: string;
7
+ /** @internal Required by keyv for iterable adapter detection */
8
+ dialect: string;
9
+ }
10
+ export declare const defaultOpts: Options;
11
+ export declare class KeyvLmdb extends EventEmitter implements KeyvStoreAdapter {
12
+ ttlSupport: boolean;
13
+ namespace?: string;
14
+ opts: Options;
15
+ private _db;
16
+ private _root;
17
+ constructor(options?: Options);
18
+ private _prefixKey;
19
+ private _unwrapKey;
20
+ get<Value>(key: string): Promise<Value | undefined>;
21
+ getMany<Value>(keys: string[]): Promise<Array<Value | undefined>>;
22
+ set(key: string, value: any, _ttl?: number): Promise<void>;
23
+ delete(key: string): Promise<boolean>;
24
+ deleteMany(keys: string[]): Promise<boolean>;
25
+ clear(): Promise<void>;
26
+ has(key: string): Promise<boolean>;
27
+ disconnect(): Promise<void>;
28
+ iterator(namespace?: string): AsyncGenerator<any[], void, unknown>;
29
+ }
30
+ export default KeyvLmdb;
package/lib/index.js ADDED
@@ -0,0 +1,99 @@
1
+ 'use strict';
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.KeyvLmdb = exports.defaultOpts = void 0;
7
+ const lmdb_1 = require("lmdb");
8
+ const events_1 = __importDefault(require("events"));
9
+ exports.defaultOpts = {
10
+ path: `${process.cwd()}/keyv-lmdb-data`,
11
+ dialect: 'redis',
12
+ };
13
+ class KeyvLmdb extends events_1.default {
14
+ ttlSupport = false;
15
+ namespace;
16
+ opts;
17
+ _db;
18
+ _root;
19
+ constructor(options) {
20
+ super();
21
+ this.opts = Object.assign({}, exports.defaultOpts, options);
22
+ const { name, dialect: _, ...lmdbOpts } = this.opts;
23
+ if (name) {
24
+ this._root = (0, lmdb_1.open)(this.opts.path, lmdbOpts);
25
+ this._db = this._root.openDB({ name });
26
+ }
27
+ else {
28
+ this._root = (0, lmdb_1.open)(this.opts.path, lmdbOpts);
29
+ this._db = this._root;
30
+ }
31
+ }
32
+ _prefixKey(key) {
33
+ return this.namespace ? `${this.namespace}:${key}` : key;
34
+ }
35
+ _unwrapKey(prefixedKey) {
36
+ if (this.namespace && prefixedKey.startsWith(this.namespace + ':')) {
37
+ return prefixedKey.slice(this.namespace.length + 1);
38
+ }
39
+ return prefixedKey;
40
+ }
41
+ async get(key) {
42
+ try {
43
+ return this._db.get(this._prefixKey(key));
44
+ }
45
+ catch {
46
+ return undefined;
47
+ }
48
+ }
49
+ async getMany(keys) {
50
+ return Promise.all(keys.map((key) => this.get(key)));
51
+ }
52
+ async set(key, value, _ttl) {
53
+ await this._db.put(this._prefixKey(key), value);
54
+ }
55
+ async delete(key) {
56
+ const prefixedKey = this._prefixKey(key);
57
+ const existed = this._db.doesExist(prefixedKey);
58
+ await this._db.remove(prefixedKey);
59
+ return existed;
60
+ }
61
+ async deleteMany(keys) {
62
+ let allDeleted = true;
63
+ for (const key of keys) {
64
+ const deleted = await this.delete(key);
65
+ if (!deleted)
66
+ allDeleted = false;
67
+ }
68
+ return allDeleted;
69
+ }
70
+ async clear() {
71
+ const prefix = this.namespace ? this.namespace + ':' : '';
72
+ const keysToDelete = [];
73
+ for (const { key } of this._db.getRange({
74
+ start: prefix,
75
+ end: prefix ? prefix + '\xFF' : undefined,
76
+ })) {
77
+ keysToDelete.push(key);
78
+ }
79
+ for (const k of keysToDelete) {
80
+ await this._db.remove(k);
81
+ }
82
+ }
83
+ async has(key) {
84
+ return this._db.doesExist(this._prefixKey(key));
85
+ }
86
+ async disconnect() {
87
+ this._db.close();
88
+ }
89
+ async *iterator(namespace) {
90
+ const prefix = namespace ? namespace : '';
91
+ const end = prefix ? prefix + '\xFF' : undefined;
92
+ for (const { key, value } of this._db.getRange({ start: prefix, end })) {
93
+ const unwrappedKey = this._unwrapKey(key);
94
+ yield [unwrappedKey, value];
95
+ }
96
+ }
97
+ }
98
+ exports.KeyvLmdb = KeyvLmdb;
99
+ exports.default = KeyvLmdb;
package/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "keyv-lmdb",
3
+ "version": "1.0.0",
4
+ "description": "LMDB storage adapter for Keyv, using lmdb-js for fast and reliable key-value storage.",
5
+ "main": "lib/index.js",
6
+ "typings": "lib/index",
7
+ "types": "./lib/index",
8
+ "scripts": {
9
+ "build": "tsc",
10
+ "test": "tsc && rm -rf ./node_modules/.cache && vitest run",
11
+ "preversion": "pnpm test",
12
+ "postversion": "git add -A && git push origin master --tags"
13
+ },
14
+ "files": [
15
+ "lib"
16
+ ],
17
+ "keywords": [
18
+ "keyv",
19
+ "lmdb",
20
+ "storage",
21
+ "cache",
22
+ "fast"
23
+ ],
24
+ "author": "zaaack",
25
+ "license": "MIT",
26
+ "devDependencies": {
27
+ "@keyv/test-suite": "^2.1.2",
28
+ "@types/node": "^24.5.2",
29
+ "keyv": "^5.6.0",
30
+ "typescript": "^5.9.3",
31
+ "vitest": "^3.2.4"
32
+ },
33
+ "dependencies": {
34
+ "lmdb": "^3.0.0"
35
+ },
36
+ "packageManager": "pnpm@9.6.0+sha512.38dc6fba8dba35b39340b9700112c2fe1e12f10b17134715a4aa98ccf7bb035e76fd981cf0bb384dfa98f8d6af5481c2bef2f4266a24bfa20c34eb7147ce0b5e"
37
+ }