npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

tinykv

v0.4.4

Published

A simple, file-backed, human-readable key-value store with TTL support

Readme

TinyKV

A minimal JSON-based key-value store for Rust with TTL support and multi-platform compatibility.

Overview

TinyKV provides a simple persistent key-value store that works across different Rust environments - from standard desktop applications to embedded systems and WebAssembly. Data is stored in human-readable JSON format with optional automatic expiration.

Features

  • JSON file-based persistence with atomic writes
  • TTL (time-to-live) expiration for keys
  • Auto-save functionality
  • Backup support with .bak files
  • Multi-platform: std, no_std, and WebAssembly
  • Flexible serialization: serde or nanoserde
  • Thread-safe operations

Installation

Rust

Add to your Cargo.toml:

# Default configuration (std + serde)
tinykv = "0.4"

# For embedded systems (no_std + nanoserde)
tinykv = { version = "0.4", default-features = false, features = ["nanoserde"] }

# Minimal configuration (no_std only)
tinykv = { version = "0.4", default-features = false }

# WebAssembly support
tinykv = { version = "0.4", features = ["wasm", "nanoserde"] }

JavaScript/TypeScript (WASM)

npm install tinykv
import { TinyKVWasm } from 'tinykv';

Quick Start

Basic Usage

use tinykv::TinyKV;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create or open a store with namespace
    let mut store = TinyKV::open("data.json")?
        .with_auto_save()
        .with_namespace("app1");
    
    // Store some data (automatically prefixed with "app1:")
    store.set("username", "alice")?;
    store.set("count", 42)?;
    
    // Store with expiration (1 hour)
    store.set_with_ttl("session_token", "abc123", 3600)?;
    
    // Retrieve data
    let username: Option<String> = store.get("username")?;
    let count: Option<i32> = store.get("count")?;
    
    // List keys (returns ["username", "count", "session_token"])
    let keys = store.keys();
    
    println!("User: {:?}, Count: {:?}", username, count);
    Ok(())
}

Embedded Systems (no_std)

#![no_std]
extern crate alloc;
use tinykv::TinyKV;

fn main() -> Result<(), tinykv::TinyKVError> {
    let mut store = TinyKV::new();
    
    store.set("device_id", "ESP32_001")?;
    store.set("config", "production")?;
    
    // Serialize for external storage
    let serialized = store.to_data()?;
    
    // Later, restore from serialized data
    let restored_store = TinyKV::from_data(&serialized)?;
    
    Ok(())
}

WebAssembly

import { TinyKVWasm } from 'tinykv';

// Use browser localStorage
const store = TinyKVWasm.openLocalStorage('myapp');
store.set('theme', 'dark');
store.setWithTtl('session', 'abc123', 3600); // 1 hour

// Prefix operations
const userKeys = store.listKeys('user:');     // ['user:123', 'user:456']
const deleted = store.clearPrefix('temp:');   // returns count

Note: For optimal performance, serve WASM files with Content-Type: application/wasm. TinyKV will automatically fallback to slower instantiation if the MIME type is incorrect.

Data Format

TinyKV stores data in a structured JSON format:

{
  "username": {
    "value": "alice",
    "expires_at": null
  },
  "session_token": {
    "value": "abc123",
    "expires_at": 1725300000
  }
}

Feature Flags

  • std (default): Enables file I/O, TTL, and standard library features
  • serde (default): Uses serde for serialization (maximum compatibility)
  • nanoserde: Uses nanoserde for faster compilation and smaller binaries
  • wasm: Enables WebAssembly support with localStorage backend

API Reference

Core Methods

  • TinyKV::open(path) - Open or create file-based store
  • TinyKV::new() - Create in-memory store
  • set(key, value) - Store a value
  • set_with_ttl(key, value, seconds) - Store with expiration
  • get(key) - Retrieve a value
  • remove(key) - Delete a key
  • contains_key(key) - Check if key exists
  • keys() - List all keys
  • list_keys(prefix) - List keys with prefix
  • clear() - Remove all entries
  • clear_prefix(prefix) - Remove entries with prefix
  • save() - Manually save to disk

Configuration

  • with_auto_save() - Enable automatic saving
  • with_backup(enabled) - Enable/disable backup files
  • with_namespace(prefix) - Set key namespace prefix
  • purge_expired() - Remove expired entries

Platform Compatibility

| Platform | File I/O | TTL | Auto-save | Serialization | |----------|----------|-----|-----------|---------------| | std | ✓ | ✓ | ✓ | serde/nanoserde | | no_std | ✗ | ✗ | ✗ | nanoserde/manual | | WASM | localStorage | ✓ | ✓ | nanoserde |

Use Cases

Ideal for:

  • Application configuration files
  • Game save data
  • CLI tool settings
  • Test data persistence
  • Embedded device configuration
  • Browser-based applications
  • Rapid prototyping

Not recommended for:

  • High-performance databases
  • Complex relational queries
  • Concurrent multi-user access
  • Large datasets (>100MB)

Documentation

Full API documentation is available at docs.rs/tinykv.

Changelog

Version 0.4.0

  • BREAKING: WASM setWithTtl now accepts number instead of bigint
  • Added namespace support with with_namespace(prefix) method
  • Added prefix operations: list_keys(prefix) and clear_prefix(prefix)
  • Enhanced WASM bindings with listKeys() and clearPrefix() methods
  • Modernized package.json with exports field and sideEffects: false
  • Improved WASM performance documentation

Version 0.3.0

  • Enhanced no_std support
  • Updated documentation and examples
  • Improved JSON output formatting

Version 0.2.0

  • Added nanoserde support for minimal binary size
  • Improved compilation speed
  • Enhanced embedded systems compatibility

Version 0.1.0

  • Initial release
  • Core key-value operations with TTL
  • File-based JSON persistence
  • Auto-save and backup support

License

MIT License