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 🙏

© 2026 – Pkg Stats / Ryan Hefner

universal-db-switcher

v2.0.0

Published

A universal database switching and migration tool for MongoDB, MySQL, PostgreSQL, and SQLite

Readme

universal-db-switcher

A universal database switching and migration tool that supports MongoDB, MySQL, PostgreSQL, and SQLite.

Features

  • Switch between MongoDB, MySQL, PostgreSQL, and SQLite
  • Extract schema from source database
  • Convert schema to target database format
  • Migrate data with configurable batch size
  • CLI tool for command-line migrations

v2.0 Enterprise Features

  • Relationship Detection – Automatically detect foreign keys using field name heuristics (userIdusers.id)
  • Join Table Generation – Convert arrays of objects into normalized SQL join tables
  • Index Migration – Migrate MongoDB indexes to SQL indexes
  • Constraint Mapping – Map required fields → NOT NULL, unique → UNIQUE constraints
  • Incremental Migration – Migrate only new/updated records since last sync
  • Live Sync Mode – Keep source and target databases synchronized in real-time

v1.1 Enhancements

  • Nested Object Flattening – MongoDB nested objects → flat SQL columns (profile.ageprofile_age)
  • Array Handling – Arrays stored as JSON in SQL (JSON type when supported, else TEXT)
  • Schema Detection – Scans first 100 documents to detect all possible fields
  • Improved Type Mapping – Mongo types mapped to appropriate SQL (BIGINT, DOUBLE, JSON, etc.)
  • Data Transformation Pipeline – Flatten + array serialization before insertion
  • Report Generator – Migration reports with statistics

Installation

npm install universal-db-switcher

Quick Start

Programmatic API

import { switchDB } from 'universal-db-switcher';

const result = await switchDB(
  {
    type: 'mongodb',
    host: 'localhost',
    port: 27017,
    database: 'mydb',
  },
  {
    type: 'mysql',
    host: 'localhost',
    port: 3306,
    database: 'mydb',
    username: 'root',
    password: 'secret',
  },
  { batchSize: 1000 }
);

console.log(result.success ? 'Migration complete!' : result.error);

CLI

# Migrate from MySQL to MongoDB
npx udb-switch migrate -s mysql -t mongodb \
  --source-uri "mysql://user:pass@localhost/sourcedb" \
  --target-uri "mongodb://localhost:27017/targetdb"

# Extract schema only (no data)
npx udb-switch migrate -s mysql -t mongodb --schema-only \
  --source-uri "mysql://user:pass@localhost/db" \
  --target-uri "mongodb://localhost/db"

# Extract schema to file
npx udb-switch schema -t mysql --uri "mysql://user:pass@localhost/db" -o schema.json

# Migrate from MongoDB to MySQL (with flatten + array→JSON transform)
npx udb-switch migrate -s mongodb -t mysql --report \
  --source-uri "mongodb://localhost:27017/source" \
  --target-uri "mysql://root:pass@localhost/target"

# Incremental migration (only new records)
npx udb-switch migrate -s mongodb -t mysql --incremental \
  --change-field "updatedAt" \
  --last-sync "2024-01-01T00:00:00Z"

Supported Databases

| Database | Status | |-----------|-----------| | MongoDB | ✅ Supported | | MySQL | ✅ Supported | | PostgreSQL| 🔜 Planned | | SQLite | 🔜 Planned |

API Reference

switchDB(sourceConfig, targetConfig, options?)

Migrates data from source to target database.

Options:

  • batchSize - Batch size for migration (default: 1000)
  • schemaOnly - Only extract schema, don't migrate data
  • skipSchemaCreation - Skip table creation
  • skipTransformation - Disable flatten/array transform
  • detectRelationships - Detect foreign keys (default: true)
  • generateJoinTables - Convert arrays to join tables (default: true)
  • migrateIndexes - Migrate indexes (default: true)
  • applyConstraints - Apply NOT NULL/UNIQUE (default: true)
  • incremental - Only migrate new/updated records
  • changeField - Field to track for incremental migration
  • lastSyncTimestamp - Timestamp for incremental migration
  • tables - Filter specific tables/collections

SyncEngine

Keep databases synchronized in real-time:

import { SyncEngine } from 'universal-db-switch';

const sync = new SyncEngine(sourceAdapter, targetAdapter, 'mongodb', 'mysql', {
  interval: 5000, // Poll every 5 seconds
  changeField: 'updatedAt',
});

await sync.start();
// ... later
sync.stop();
const status = sync.getStatus();

detectRelationships(schema, options?)

Detect foreign key relationships using heuristics.

generateJoinTables(parentTable, arrayFields)

Generate join table schemas from array fields.

convertMongoIndexes(mongoIndexes, tableName)

Convert MongoDB indexes to SQL index definitions.

mapConstraints(sourceSchema, targetType)

Map MongoDB constraints to SQL constraints.

extractSchema(config)

Extracts schema from a database. For MongoDB, scans first 100 documents.

transformDocuments(docs, options?)

Transformation pipeline: flatten nested objects, convert arrays to JSON.

detectSchema(documents, options?)

Detect schema from document sample. Options: sampleSize (default 100), separator.

generateReport(result, options?)

Generate migration report from MigrationResult.

createAdapter(config)

Creates a database adapter instance.

License

MIT