storage-box
v1.0.4
Published
A memory-based key–value storage for javascript.
Maintainers
Readme
storage-box is a versatile JavaScript key–value storage library supporting memory, file system, and browser drivers with expiration and rich data structures.
📦 Installation
npm install storage-boxpnpm
pnpm install storage-boxyarn
yarn add storage-box📖 Usage
Basic Usage
import { Client } from 'storage-box';
const client = new Client();
client.set('key', 'value');
console.log(client.get('key')); // 'value'Storage Drivers
Memory (Default)
In-memory storage driver requiring zero setup.
import { Client, MemoryDriver } from 'storage-box';
const client = new Client(new MemoryDriver());File-based Storage (fs)
File system driver supporting Node.js and Bun environments with JSON or MessagePack serialization.
import path from 'node:path';
import { FsDriver, MSGPack } from 'storage-box/node';
import { Client } from 'storage-box';
// JSON file storage (default)
const jsonDriver = new FsDriver(path.resolve('data.json'));
const client = new Client(jsonDriver);
// MessagePack binary storage
const msgpackDriver = new FsDriver(path.resolve('data.b64'), {
parser: MSGPack,
});
const mpClient = new Client(msgpackDriver);Browser Storage
Local or Session storage driver for web applications.
import { Client } from 'storage-box';
import { BrowserDriver } from 'storage-box/browser';
const driver = new BrowserDriver('local'); // or 'session'
const client = new Client(driver);
client.set('theme', 'dark');Automatic Key Expiration (TTL)
Set keys with expiration time in seconds and inspect remaining TTL.
import { Client } from 'storage-box';
const client = new Client();
client.setex('session', 'secret-token', 10); // expires in 10 seconds
console.log(client.ttl('session')); // remaining time in seconds
console.log(client.ttl('session', true)); // remaining time in millisecondsHash Operations
Store key-value pairs inside nested hash structures.
import { Client } from 'storage-box';
const client = new Client();
client.hset('user:100', 'name', 'John');
client.hset('user:100', 'email', '[email protected]');
console.log(client.hget('user:100', 'name')); // 'John'
console.log(client.hgetall('user:100')); // { name: 'John', email: '[email protected]' }List Operations
Work with ordered arrays and push/pop elements.
import { Client } from 'storage-box';
const client = new Client();
client.lpush('tasks', 'write docs');
client.lpush('tasks', 'publish package');
console.log(client.lpop('tasks')); // 'publish package'
console.log(client.lrange('tasks', 0, -1)); // ['write docs']Custom HashMap & List Classes
Extend HashMap or List for custom data structures and domain logic.
import { Client, HashMap } from 'storage-box';
interface User {
first: string;
last: string;
}
class UserMap extends HashMap<string, User> {
async getInitials(): Promise<string[]> {
const all = await this.getall();
return Object.values(all).map((u) => `${u.first[0]}${u.last[0]}`);
}
}
const users = client.createHashMap('users', UserMap);
await users.set('u1', { first: 'John', last: 'Doe' });📚 Documentation
For all configuration options, please see the API docs.
🤝 Contributing
Want to contribute? Awesome! To show your support is to star the project, or to raise issues on GitHub.
Thanks again for your support, it is much appreciated! 🙏
License
MIT © Shahrad Elahi and contributors.
