localio
v0.5.7
Published
A local Cloud-like API
Maintainers
Readme
LocalIO
LocalIO is a versatile library designed to simplify data storage directly within the end user's browser. It provides a straightforward API for handling local NoSQL databases, S3-compatible storage, and a time-to-live (TTL) cache.
Features
Local NoSQL Database
- Structured and queryable local database for your web application needs.
- Use collections, documents, and advanced queries.
Local S3-compatible Storage
- Bucket-based storage for file-like data management.
Local Cache with TTL
- Fast and efficient cache storage with optional expiration times.
Built on Top of Native Browser APIs
- Utilizes IndexedDB, BucketManager, and CacheStorage for efficient storage and retrieval.
Installation
Install the package using npm:
npm install localioAPI Usage
1. Bucket (Local S3-compatible Storage)
The Bucket API provides a straightforward way to store and retrieve file-like data.
Example:
import { openBucket } from 'localio';
// Initialize a bucket
const bucket = await openBucket('my-bucket');
// Store data
await bucket.put('path/to/key', 'value');
// Retrieve data
const data = await bucket.get('path/to/key'); // Returns null if the key is not found2. Database (Local NoSQL Storage)
The Database API allows you to work with collections and documents, similar to a NoSQL database.
Example:
import { getDatabase } from 'localio';
// Initialize a database
const db = await getDatabase('my-database');
// Access a collection
const ref = await db.get('collection_name');
// Add or update a document
await ref.set('document_id', { field: 'value' });
// Query documents
const document = await ref.where('field', '==', 'value').limit(10).get();3. Cache (Local Cache with TTL)
The Cache API provides fast in-memory storage with support for TTL (time-to-live) and options to preserve existing TTL values.
Example:
import { getCache } from 'localio';
// Initialize a cache
const cache = await getCache('my-cache');
// Store data with a TTL of 3600 seconds (1 hour)
cache.set('key', 'value', { ttl: 3600 });
// Retrieve data
const data = await cache.get('key');
// Update value while preserving existing TTL
await cache.set('key', 'newValue', { preserveTTL: true });
// Delete a key
cache.delete('key');Why LocalIO?
- Browser-native: No server is required for storage; all data resides locally.
- Modular API: Use only the components you need (Bucket, Database, or Cache).
- Ease of Use: Familiar APIs for storage, querying, and caching.
- Performance: Optimized for fast read/write operations in modern browsers.
For more details, explore the official documentation or examples provided in the library.
