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 🙏

© 2025 – Pkg Stats / Ryan Hefner

db-sandbox

v1.0.1

Published

Universal database sandbox for testing. Runs logic in isolated transactions and automatically rolls back changes. Supports PostgreSQL, MySQL, and SQLite.

Readme

DbSandbox


💡 Why DbSandbox?

Testing databases is painful. You have to create records, run tests, and then cleanup the mess. If a test fails before cleanup, your database remains dirty, causing subsequent tests to fail.

  • The Problem: Manual cleanup (DELETE FROM users) is error-prone and slow. Spinning up Docker containers for every test suite is resource-heavy.
  • The Solution: DbSandbox wraps your test logic in a database transaction. When your test finishes (success or failure), it issues a ROLLBACK command. Your database remains pristine, always.

✨ Features

  • 🛡️ Auto-Rollback: Guaranteed cleanup. Logic runs in a transaction that never commits.
  • 🔌 Driver Agnostic: First-class support for PostgreSQL (pg), MySQL (mysql2), and SQLite (better-sqlite3).
  • 🚀 Zero-Config: No need to setup complex test environments or Docker containers.
  • 🧩 Typescript First: Fully typed for great DX.
  • ⚡ Lightweight: Minimal abstraction overhead.

📦 Installation

Install the core package along with the driver for your database of choice.

# For PostgreSQL
npm install db-sandbox pg

# For MySQL
npm install db-sandbox mysql2

# For SQLite
npm install db-sandbox better-sqlite3

🚀 Quick Start

1. PostgreSQL Example

import { Pool } from 'pg';
import { DbSandbox, PostgresDriver } from 'db-sandbox';

// Setup your real connection pool
const pool = new Pool({ connectionString: 'postgres://...' });

// Initialize Sandbox
const sandbox = new DbSandbox(new PostgresDriver(pool));

await sandbox.run(async (tx) => {
  console.log('🔒 Inside Sandbox: Transaction Started');

  // Perform operations (Insert, Update, Delete)
  await tx.query('INSERT INTO users (name) VALUES ($1)', ['Alice']);
  
  const res = await tx.query('SELECT * FROM users');
  console.log(res[0].name); // 'Alice' exists here!
  
  // When this function returns (or throws), ROLLBACK is called automatically.
});

console.log('🔓 Outside Sandbox: Data is gone!');

2. SQLite Example

import Database from 'better-sqlite3';
import { DbSandbox, SQLiteDriver } from 'db-sandbox';

const db = new Database('my.db');
const sandbox = new DbSandbox(new SQLiteDriver(db));

await sandbox.run(async (tx) => {
  await tx.query('INSERT INTO items (name) VALUES (?)', ['Sword']);
  // ... test logic
});

🧠 Architecture

DbSandbox uses the Adapter Pattern to provide a unified interface over different SQL drivers.

  1. Connect: Acquires a dedicated client from the pool.
  2. Begin: Executes BEGIN / START TRANSACTION.
  3. Run: Executes your callback function.
  4. Rollback: Executes ROLLBACK in a finally block, ensuring it runs even if your code crashes.
  5. Release: Returns the connection to the pool.

📚 API Reference

new DbSandbox(driver)

Creates a new sandbox instance.

  • driver: An instance of PostgresDriver, MySQLDriver, or SQLiteDriver.

sandbox.run(callback)

Executes the callback within a transaction.

  • callback: async (driver) => Promise<void>
  • Returns: The result of your callback.

driver.query(sql, params?)

Executes a raw SQL query within the transaction.

  • Returns: An array of rows (standardized across all drivers).

driver.getNativeClient()

Returns the underlying database client (e.g., pg.PoolClient). Useful if you need to pass the transactional client to an ORM like Drizzle or TypeORM.

await sandbox.run(async (driver) => {
  const pgClient = driver.getNativeClient();
  // Pass pgClient to your ORM...
});

🤝 Contributing

Contributions are welcome!

  1. Fork the project
  2. Create your feature branch
  3. Commit your changes
  4. Push to the branch
  5. Open a Pull Request

📄 License

Distributed under the MIT License.