string.db
v2.0.0
Published
The most amazing database
Downloads
4
Readme
StringDB
A lightweight, fast, string-based key-value database for caching and local storage in JavaScript.
Features:
- Stores all data inside a single string
- Key-value operations:
set,get,delete,has,update - TTL (expiration support)
- Schema validation and type coercion
- Optional compression & XOR encryption
- Efficient parsing without JSON or regex
- Iteration methods:
keys(),values(),entries() - Cleanup for expired keys
- Export/import raw DB string
Installation
npm install string.dbUsage
import StringDB from "string.db";
const db = new StringDB("", {
compress: false,
encrypt: false,
secret: "my-secret-key", // XOR encryption secret if enabled
schema: {
age: "number",
active: "boolean",
tags: "array",
},
coerce: true,
});
// Set values with optional TTL (milliseconds)
db.set("name", "Alice");
db.set("age", 30);
db.set("active", true);
db.set("tags", ["user", "admin"], 60000); // expires in 60 seconds
// Get values
console.log(db.get("name")); // "Alice"
console.log(db.get("age")); // 30 (number coerced)
// Check if key exists
console.log(db.has("active")); // true
// Update a value
db.update("age", age => age + 1);
// Delete a key
db.delete("tags");
// Iterate keys and values
for (const key of db.keys()) {
console.log(key, db.get(key));
}
// Cleanup expired entries
db.cleanup();
// Export and import DB string
const rawDbString = db.export();
db.import(rawDbString);API
| Method | Description |
| -------------------- | -------------------------------------------- |
| set(key, val, ttl) | Set value with optional TTL in ms |
| get(key) | Get value by key (or undefined) |
| has(key) | Check if key exists and not expired |
| delete(key) | Delete a key-value pair |
| update(key, fn) | Update value by applying a function |
| keys(prefix) | Get array of keys (optional prefix filter) |
| values(prefix) | Get array of values (optional prefix filter) |
| entries(prefix) | Get array of [key, value] pairs |
| clear() | Clear entire DB |
| count() | Count entries |
| sizeInBytes() | Get size of stored DB string |
| export() | Export raw DB string |
| import(dbString) | Import raw DB string |
| cleanup() | Remove expired entries |
