messagessyncdb
v1.0.0
Published
ElephantSync ElephantSync is a robust offline-first data synchronization library that leverages Dexie.js for managing local IndexedDB storage and Axios for syncing data with a remote server. It’s designed for scalability and flexibility, making it ideal
Downloads
62
Readme
ElephantSync ElephantSync is a robust offline-first data synchronization library that leverages Dexie.js for managing local IndexedDB storage and Axios for syncing data with a remote server. It’s designed for scalability and flexibility, making it ideal for applications requiring offline data handling and synchronization.
Features Offline-First: Store and manage data locally with Dexie.js. Data Synchronization: Sync local changes with a remote server using Axios. Customizable Schema: Define your database tables and indices dynamically. Sync Logs: Track unsynced changes to ensure data consistency. Configurable via .env: Manage database name and other configurations through environment variables. Installation Install the module via npm:
bash Copy code npm install elephant-sync Usage Setting Up javascript Copy code const ElephantSync = require('elephant-sync');
// Define your database schema and server URL const db = new ElephantSync( { users: '++id, name, email', tasks: '++id, title, completed', }, 'https://example.com/api' // Your server's API base URL ); Methods
- addRecord(tableName, data) Add a new record to a specified table.
Parameters: tableName (string): The name of the table. data (object): The record to be added. Returns: Promise: The ID of the added record. Example: javascript Copy code await db.addRecord('users', { name: 'Melvin', email: '[email protected]' }); 2. getAllRecords(tableName) Retrieve all records from a specified table.
Parameters: tableName (string): The name of the table. Returns: Promise<object[]>: An array of records. Example: javascript Copy code const users = await db.getAllRecords('users'); console.log(users); 3. logSyncAction(tableName, action, data) Log an action to the sync log for later synchronization. (This is used internally.)
Parameters: tableName (string): The name of the table. action (string): The action performed (add, delete, etc.). data (object): The data associated with the action. 4. syncData(tableName, syncOptions) Synchronize unsynced actions for a specified table with the remote server.
Parameters: tableName (string): The name of the table to sync. syncOptions (object, optional): Configuration for retry attempts and delay (default: { retries: 3, delay: 2000 }). Returns: Promise Example: javascript Copy code await db.syncData('users'); 5. clearSyncLog(tableName) Clear the sync log for a specified table. (This is used internally after a successful sync.)
Parameters: tableName (string): The name of the table. Returns: Promise 6. deleteRecord(tableName, key) Delete a record from a specified table and log the action for synchronization.
Parameters: tableName (string): The name of the table. key (number|string): The primary key of the record to delete. Returns: Promise Example: javascript Copy code await db.deleteRecord('users', 1); Environment Configuration The database name is configurable via a .env file. Ensure you create a .env file in your project root:
env Copy code DB_NAME=ElephantSyncDB Install the dotenv package if not already installed:
bash Copy code npm install dotenv Error Handling All methods throw detailed errors if something goes wrong. Use try-catch blocks to handle errors effectively:
javascript Copy code try { await db.addRecord('tasks', { title: 'Learn ElephantSync', completed: false }); } catch (err) { console.error('Error:', err.message); } Example Here’s a complete example of using ElephantSync:
javascript Copy code const ElephantSync = require('elephant-sync');
const db = new ElephantSync( { users: '++id, name, email', tasks: '++id, title, completed', }, 'https://example.com/api' );
(async () => { try { // Add a record await db.addRecord('users', { name: 'Melvin', email: '[email protected]' });
// Retrieve records
const users = await db.getAllRecords('users');
console.log('Users:', users);
// Sync data
await db.syncData('users');
console.log('Data synced successfully.');
} catch (err) {
console.error('Error:', err.message);
}
})(); License This module is licensed under the MIT License. See the LICENSE file for details.