Skip to content

Storage Methods

These methods handle individual storage operations like setting, getting, and removing items.

Stores a JSON-serializable value associated with a key.

await storage.set(key, value)
  • key (String) — The key to store the value under
  • value (Any JSON-serializable value) — The value to store (object, array, string, number, boolean, null)

Promise<void>

Throws an error if the value is not JSON-serializable.

// Store primitive values
await storage.set('username', 'Alice');
await storage.set('count', 42);
await storage.set('isActive', true);
// Store objects
await storage.set('user', {
name: 'Alice',
email: 'alice@example.com',
roles: ['admin', 'user']
});
// Store arrays
await storage.set('items', [1, 2, 3, 4, 5]);

Retrieves the value associated with a key.

await storage.get(key)
  • key (String) — The key to retrieve

Promise<any> — The stored value (parsed from JSON), or null if:

  • The key doesn’t exist
  • The item has expired (and is automatically removed)
const user = await storage.get('user');
console.log(user); // Output: { name: 'Alice', email: '...' }
const missing = await storage.get('nonexistent');
console.log(missing); // Output: null

Sets or updates the expiration time for an existing key.

await storage.expires(key, expiresIn)
  • key (String) — The key to set expiration for
  • expiresIn (Number) — The time in seconds from now until the item expires

Promise<void>

// Store a value
await storage.set('tempData', { info: 'test' });
// Set it to expire in 10 minutes (600 seconds)
await storage.expires('tempData', 600);
// Update expiration to 5 minutes
await storage.expires('tempData', 300);

Removes an item from storage and clears any associated expiration timer.

await storage.remove(key)
  • key (String) — The key to remove

Promise<void>

await storage.set('tempToken', 'abc123');
// Later...
await storage.remove('tempToken');
const token = await storage.get('tempToken');
console.log(token); // Output: null

Checks if a key exists in storage and has not expired.

await storage.has(key)
  • key (String) — The key to check

Promise<boolean>true if the key exists and is not expired, false otherwise

await storage.set('user', { name: 'Alice' });
const hasUser = await storage.has('user');
console.log(hasUser); // Output: true
const hasOther = await storage.has('nonexistent');
console.log(hasOther); // Output: false

Returns an array of all keys within the instance’s namespace.

await storage.keys()

Promise<string[]> — An array of key names (without the namespace prefix)

await storage.set('user', { name: 'Alice' });
await storage.set('preferences', { theme: 'dark' });
await storage.set('settings', { lang: 'en' });
const allKeys = await storage.keys();
console.log(allKeys); // Output: ['user', 'preferences', 'settings']

Retrieves all non-expired key-value pairs within the instance’s namespace.

await storage.getAll()

Promise<Object> — An object mapping keys to their values. Expired items are excluded.

await storage.set('user', { name: 'Alice' });
await storage.set('preferences', { theme: 'dark' });
await storage.set('expired', 'test');
await storage.expires('expired', 1); // Expires in 1 second
// Wait for expiration
await new Promise(resolve => setTimeout(resolve, 1100));
const allData = await storage.getAll();
console.log(allData);
// Output: { user: { name: 'Alice' }, preferences: { theme: 'dark' } }
// Note: 'expired' is not included

Removes all items associated with the instance’s namespace from storage.

await storage.clear()

Promise<void>

await storage.set('key1', 'value1');
await storage.set('key2', 'value2');
await storage.set('key3', 'value3');
// Remove everything in this namespace
await storage.clear();
const allKeys = await storage.keys();
console.log(allKeys); // Output: []

Manually triggers a cleanup of expired items within the namespace. This is automatically called during instantiation, but you can call it manually if needed.

await storage.cleanup()

Promise<void>

// Manually clean up expired items
await storage.cleanup();

Learn about Batch Operations for efficiently handling multiple items at once.