Routzie - documentation

Storage - storing data localy

The storage object provides a simple persistent key-value store for your machine code. Under the hood, it uses LevelDB, which is a fast and reliable embedded database. Each machine has its own isolated storage, so data is not shared between machines.

Features

  • Persistent: Data survives machine restarts and redeployments.
  • Isolated: Each machine has its own storage namespace.
  • Flexible: You can store any serializable JavaScript value (object, array, string, number, boolean).
  • Simple API: Just use setValue, getValue, getAll, and removeValue.

Typical use cases

  • Caching results of expensive operations
  • Storing user or session data
  • Saving configuration or state
  • Implementing simple queues or lists

API Reference

Store data

await storage.setValue('user:123', { name: 'Alice', age: 30 }); await storage.setValue('counter', 42); await storage.setValue('isActive', true);

Retrieve data

const user = await storage.getValue('user:123'); // { name: 'Alice', age: 30 } const counter = await storage.getValue('counter'); // 42 const isActive = await storage.getValue('isActive'); // true

Get all stored data

const allData = await storage.getAll(); // { 'user:123': { name: 'Alice', age: 30 }, counter: 42, isActive: true }

Remove data

await storage.removeValue('counter');

Best practices

  • Use clear, unique keys (e.g. user:123, settings, session:abc).
  • Store only serializable data (no functions, no circular references).
  • For lists or complex objects, always read-modify-write atomically.
  • Remove unused keys to keep storage clean.

Notes on LevelDB

  • LevelDB is optimized for fast reads and writes.
  • Data is stored on disk, not in memory.

Troubleshooting

  • If you get undefined from getValue, the key does not exist.
  • All operations are asynchronous and return Promises.

Examples:

Example: User settings

// Save user settings await storage.setValue('settings', { theme: 'dark', notifications: true }); // Load user settings const settings = await storage.getValue('settings');

Example: Storing a list

let todos = await storage.getValue('todos') || []; todos.push({ text: 'Buy milk', done: false }); await storage.setValue('todos', todos);

Example: Simple counter

let count = await storage.getValue('count') || 0; count++; await storage.setValue('count', count);

© 2025 Routzie Routzie.com