Official Documentation

PingDartDB Node.js SDK

Seamlessly integrate your local databases with the PingDart ecosystem. Create complex dynamic queries, handle nested joins, and perform geospatial searches effortlessly.

Installation

Install the PingDartDB Node.js SDK via npm:

npm install pingdartdb-node

1. Get Your SDK Key

Before initializing the SDK, you need a local SDK key.

  1. Log in to your PingDart User Dashboard.
  2. Navigate to Settings > Security.
  3. Scroll down to the PingDartDB Local SDK Keys section.
  4. Click Create SDK Key.
  5. Provide an app name and choose your tier (Free for Local Sync, Pro for Cloud Remote).
  6. Copy the generated key (e.g., pd_...).

2. Initialization

Initialize the SDK with your API key and your local database credentials.

const PingDartDB = require('pingdartdb-node');

const db = new PingDartDB('YOUR_SDK_KEY_HERE', {
    host: 'localhost',
    user: 'root',
    password: 'your_password',
    database: 'my_app',
    type: 'mysql' // or 'postgres'
});

async function startApp() {
    await db.connect();
    console.log("Connected to PingDartDB Local!");
}
startApp();

3. Reading Data (with Joins & Range)

Use the read() method to fetch data. It supports advanced filtering, pagination, geo-fencing (range), and deeply nested joins (margedata).

const results = await db.table('users').read({
    conditions: { status: 'active' },
    search: { username: 'john' }, // OR search across specified fields
    pagination: { page: 1, limit: 10 },
    orderBy: 'created_at DESC',
    
    // Merge related data from other tables (Joins)
    margedata: [
        {
            target_table: 'orders',
            target_column: 'user_id',
            target_value: 'id',
            target_label: 'user_orders',
            search_fields: [{ status: 'orders.status' }],
            margedata: [
                {
                    target_table: 'order_items',
                    target_column: 'order_id',
                    target_value: 'id',
                    target_label: 'items'
                }
            ]
        }
    ],
    
    // Geospatial search (Haversine formula)
    range: {
        latitude: 12.97,
        longitude: 77.59,
        radius: 10, // km
        target_latitude: 'lat',
        target_longitude: 'lng'
    }
});

console.log(results.data);

4. Insert, Update, Delete & Count

Insert

// Single Insert
await db.table('users').insert({ name: 'Alice', email: 'alice@example.com' });

// Bulk Insert
await db.table('users').insert([
    { name: 'Bob', email: 'bob@example.com' },
    { name: 'Charlie', email: 'charlie@example.com' }
]);

Update

// Update records matching condition
await db.table('users').update({ status: 'active' }, { role: 'admin' });

Delete

// Delete records matching condition
await db.table('users').delete({ status: 'banned' });

Count

const count = await db.table('users').count({ status: 'active' });
console.log("Total active users:", count);

Need more help?

Go to Dashboard