Skip to content
プロジェクト

概要

💾 A Memory-based key-value storage system for JavaScript.

システム・インフラ

この数値の測り方

計測したスター増加

24時間
0
7日間
0
15日間
-1
1か月
履歴を収集中

期間ごとの純増数です。期間は重複しており、日別の値ではありません。未計測の値は空欄です。スター数は注目度の指標であり、品質の保証ではありません。

storage-box
CI NPM Version MIT License npm bundle size Install Size

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-box
Install using your favorite package manager

pnpm

pnpm install storage-box

yarn

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 milliseconds

Hash 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.

関連プロジェクトを探す

読み込み中…