A Keyv store implementation using NeDB as the backend storage
npm install keyv-nedb-storeA Keyv store implementation using NeDB (@seald-io/nedb) as the backend storage.
- File-based storage - Persistent key-value storage using NeDB's file system backend
- YAML-friendly - NeDB files are human-readable and linter-friendly
- Namespace support - Organize your data with namespaces
- TTL support - Set time-to-live for automatic key expiration
- Custom serialization - Optional JSON serialization for complex data types
- TypeScript - Fully typed with TypeScript support
``bash`
bun install keyv-nedb-store @seald-io/nedb
Or with npm:
`bash`
npm install keyv-nedb-store @seald-io/nedb
`ts
import Keyv from "keyv";
import { KeyvNedbStore } from "keyv-nedb-store";
// Create a store with file-based persistence
const store = new KeyvNedbStore(".cache/database.nedb.yaml");
const keyv = new Keyv({ store });
// Set a value
await keyv.set("foo", "bar");
// Get a value
const value = await keyv.get("foo"); // "bar"
// Delete a value
await keyv.delete("foo");
// Clear all values
await keyv.clear();
`
`ts
const store = new KeyvNedbStore({
filename: "database.nedb.yaml",
namespace: "myapp",
autoload: true,
});
const keyv = new Keyv({ store });
await keyv.set("user:1", { name: "Alice" });
// Stored with key: "myapp:user:1"
`
`ts
const keyv = new Keyv({ store });
// Set a value that expires in 1 second
await keyv.set("temp", "value", 1000);
// Wait for expiration
await new Promise((resolve) => setTimeout(resolve, 1100));
const value = await keyv.get("temp"); // undefined
`
`ts
const store = new KeyvNedbStore({
filename: "database.nedb.yaml",
serializer: {
stringify: JSON.stringify,
parse: JSON.parse,
},
autoload: true,
});
const keyv = new Keyv({ store });
// Store complex objects
await keyv.set("user", { id: 1, name: "Alice", roles: ["admin", "user"] });
`
Creates a new NeDB store instance.
#### Options
All NeDB DataStoreOptions are supported, plus:
- namespace (string, optional) - Prefix for all keysserializer
- (object, optional) - Custom serialization for valuesstringify
- (function) - Serialize value to stringparse
- (function) - Deserialize string to value
Common NeDB options:
- filename (string) - Path to the database fileautoload
- (boolean) - Automatically load the databaseinMemoryOnly
- (boolean) - Use in-memory only (no persistence)
Implements the Keyv Store Adapter interface:
- get(key) - Get a value by keygetMany(keys)
- - Get multiple values by keysset(key, value, ttl?)
- - Set a value with optional TTL in millisecondsdelete(key)
- - Delete a value by keyclear()
- - Clear all values (respects namespace)iterator(namespace?)
- - Async iterator for all key-value pairs
The store supports iteration over all entries:
`ts
const store = new KeyvNedbStore("database.nedb.yaml");
const keyv = new Keyv({ store });
// Add some data
await keyv.set("user:1", { name: "Alice" });
await keyv.set("user:2", { name: "Bob" });
// Iterate over all entries using the store's iterator
for await (const [key, value] of store.iterator()) {
console.log(key, value);
// Output:
// user:1 { name: 'Alice' }
// user:2 { name: 'Bob' }
}
`
NeDB is a lightweight, embedded database that:
- Requires no separate database server
- Stores data in human-readable files
- Works great with YAML linters
- Provides a MongoDB-like API
- Supports indexing and complex queries
Perfect for:
- Configuration storage
- Cache storage
- Small to medium datasets
- Serverless environments
- Development and testing
Built with Bun:
`bashInstall dependencies
bun install
MIT
- Keyv - Simple key-value storage with support for multiple backends
- NeDB - Embedded persistent database for Node.js