npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

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

  1. 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.