keyv-nedb-store
v0.0.10
Published
A Keyv store implementation using NeDB as the backend storage
Downloads
5,692
Maintainers
Readme
keyv-nedb-store
A Keyv store implementation using NeDB (@seald-io/nedb) as the backend storage.
Features
- 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
Installation
bun install keyv-nedb-store @seald-io/nedbOr with npm:
npm install keyv-nedb-store @seald-io/nedbUsage
Basic Example
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();With Namespace
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"With TTL (Time-To-Live)
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"); // undefinedWith Custom Serialization
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"] });API
new KeyvNedbStore(options)
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)
Store Methods
Implements the Keyv Store Adapter interface:
get(key)- Get a value by keyset(key, value, ttl?)- Set a value with optional TTL in millisecondsdelete(key)- Delete a value by keyclear()- Clear all values (respects namespace)
Why NeDB?
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
Development
Built with Bun:
# Install dependencies
bun install
# Format code
bun run fmt
# Build
bun run buildLicense
MIT
