Storage Methods
These methods handle individual storage operations like setting, getting, and removing items.
Stores a JSON-serializable value associated with a key.
Syntax
Section titled “Syntax”await storage.set(key, value)Parameters
Section titled “Parameters”key(String) — The key to store the value undervalue(Any JSON-serializable value) — The value to store (object, array, string, number, boolean, null)
Returns
Section titled “Returns”Promise<void>
Errors
Section titled “Errors”Throws an error if the value is not JSON-serializable.
Example
Section titled “Example”// Store primitive valuesawait storage.set('username', 'Alice');await storage.set('count', 42);await storage.set('isActive', true);
// Store objectsawait storage.set('user', { name: 'Alice', email: 'alice@example.com', roles: ['admin', 'user']});
// Store arraysawait storage.set('items', [1, 2, 3, 4, 5]);Retrieves the value associated with a key.
Syntax
Section titled “Syntax”await storage.get(key)Parameters
Section titled “Parameters”key(String) — The key to retrieve
Returns
Section titled “Returns”Promise<any> — The stored value (parsed from JSON), or null if:
- The key doesn’t exist
- The item has expired (and is automatically removed)
Example
Section titled “Example”const user = await storage.get('user');console.log(user); // Output: { name: 'Alice', email: '...' }
const missing = await storage.get('nonexistent');console.log(missing); // Output: nullexpires()
Section titled “expires()”Sets or updates the expiration time for an existing key.
Syntax
Section titled “Syntax”await storage.expires(key, expiresIn)Parameters
Section titled “Parameters”key(String) — The key to set expiration forexpiresIn(Number) — The time in seconds from now until the item expires
Returns
Section titled “Returns”Promise<void>
Example
Section titled “Example”// Store a valueawait storage.set('tempData', { info: 'test' });
// Set it to expire in 10 minutes (600 seconds)await storage.expires('tempData', 600);
// Update expiration to 5 minutesawait storage.expires('tempData', 300);remove()
Section titled “remove()”Removes an item from storage and clears any associated expiration timer.
Syntax
Section titled “Syntax”await storage.remove(key)Parameters
Section titled “Parameters”key(String) — The key to remove
Returns
Section titled “Returns”Promise<void>
Example
Section titled “Example”await storage.set('tempToken', 'abc123');// Later...await storage.remove('tempToken');
const token = await storage.get('tempToken');console.log(token); // Output: nullChecks if a key exists in storage and has not expired.
Syntax
Section titled “Syntax”await storage.has(key)Parameters
Section titled “Parameters”key(String) — The key to check
Returns
Section titled “Returns”Promise<boolean> — true if the key exists and is not expired, false otherwise
Example
Section titled “Example”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: falsekeys()
Section titled “keys()”Returns an array of all keys within the instance’s namespace.
Syntax
Section titled “Syntax”await storage.keys()Returns
Section titled “Returns”Promise<string[]> — An array of key names (without the namespace prefix)
Example
Section titled “Example”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']getAll()
Section titled “getAll()”Retrieves all non-expired key-value pairs within the instance’s namespace.
Syntax
Section titled “Syntax”await storage.getAll()Returns
Section titled “Returns”Promise<Object> — An object mapping keys to their values. Expired items are excluded.
Example
Section titled “Example”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 expirationawait 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 includedclear()
Section titled “clear()”Removes all items associated with the instance’s namespace from storage.
Syntax
Section titled “Syntax”await storage.clear()Returns
Section titled “Returns”Promise<void>
Example
Section titled “Example”await storage.set('key1', 'value1');await storage.set('key2', 'value2');await storage.set('key3', 'value3');
// Remove everything in this namespaceawait storage.clear();
const allKeys = await storage.keys();console.log(allKeys); // Output: []cleanup()
Section titled “cleanup()”Manually triggers a cleanup of expired items within the namespace. This is automatically called during instantiation, but you can call it manually if needed.
Syntax
Section titled “Syntax”await storage.cleanup()Returns
Section titled “Returns”Promise<void>
Example
Section titled “Example”// Manually clean up expired itemsawait storage.cleanup();Next Steps
Section titled “Next Steps”Learn about Batch Operations for efficiently handling multiple items at once.