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

@loom_project/db-migrate

v1.0.0

Published

Zero-dependency PostgreSQL migration tool for Bun

Downloads

4

Readme

@loom_project/db-migrate

Zero-dependency PostgreSQL migration tool for Bun with built-in idempotency support.

Features

  • Zero Dependencies: Uses Bun's native SQL client
  • Idempotent Migrations: Write safe, repeatable migrations
  • Transaction Safety: Each migration runs in its own transaction
  • Checksum Verification: Detects modified migration files
  • Simple CLI: Easy-to-use command-line interface
  • Schema Isolation: Migrations tracked per schema

Installation

bun add @loom_project/db-migrate

Or install globally:

bun add -g @loom_project/db-migrate

Quick Start

1. Configure Database

Create a .env file in your project root:

DATABASE_URL=postgres://user:password@localhost:5432/mydb
DB_SCHEMA=public

2. Create Migration Directory

mkdir migrate

3. Create Your First Migration

db-migrate create create_users_table

This creates a file like migrate/001__update__create_users_table.sql

4. Write Migration SQL

Edit the generated file with idempotent SQL:

-- Create table with IF NOT EXISTS
CREATE TABLE IF NOT EXISTS users (
  id SERIAL PRIMARY KEY,
  email VARCHAR(255) NOT NULL UNIQUE,
  created_at TIMESTAMP DEFAULT NOW()
);

-- Add column conditionally
DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_name = 'users' AND column_name = 'role'
  ) THEN
    ALTER TABLE users ADD COLUMN role VARCHAR(50);
  END IF;
END $$;

5. Run Migrations

db-migrate

Migration File Format

Migration files must follow this naming pattern:

<version>__<type>__<description>.sql
  • version: 3-digit number (001, 002, 003, ...)
  • type: Category (init, update, fix, etc.)
  • description: Brief description using underscores

Examples:

  • 001__init__create_users_table.sql
  • 002__update__add_email_index.sql
  • 003__fix__remove_duplicate_users.sql

CLI Commands

Run Migrations (Default)

db-migrate
db-migrate migrate

Check Migration Status

db-migrate status

Create New Migration

db-migrate create <name>

Specify Migration Directory

db-migrate --dir ./my-migrations

Show Help

db-migrate help

Environment Variables

| Variable | Description | Default | |----------|-------------|---------| | DATABASE_URL | PostgreSQL connection URL | - | | DB_HOST | Database host | - | | DB_PORT | Database port | 5432 | | DB_NAME | Database name | - | | DB_USER | Database username | - | | DB_PASSWORD | Database password | - | | DB_SCHEMA | Target schema | public |

Migration Tracking

Migrations are tracked in the loom_migrations schema:

loom_migrations.migrate_<schema_name>

Examples:

  • For public schema → loom_migrations.migrate_public
  • For app schema → loom_migrations.migrate_app

Each table stores:

  • Version number
  • Migration name and description
  • File checksum (SHA256)
  • Execution timestamp
  • Execution time in milliseconds

Writing Idempotent Migrations

Table Creation

CREATE TABLE IF NOT EXISTS my_table (
  id SERIAL PRIMARY KEY,
  name VARCHAR(255)
);

Adding Columns

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_name = 'my_table' AND column_name = 'new_column'
  ) THEN
    ALTER TABLE my_table ADD COLUMN new_column TEXT;
  END IF;
END $$;

Creating Indexes

CREATE INDEX IF NOT EXISTS idx_name ON my_table(name);

Creating Types

DO $$
BEGIN
  IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'my_enum') THEN
    CREATE TYPE my_enum AS ENUM ('value1', 'value2');
  END IF;
END $$;

Programmatic API

You can also use the library programmatically:

import { loadConfig, createConnection, runMigrations } from '@loom_project/db-migrate';

const config = loadConfig();
createConnection(config);

const result = await runMigrations(config.targetSchema, './migrate');

console.log(`Executed ${result.executed.length} migrations`);

Best Practices

  1. Always use idempotent SQL: Migrations should be safe to run multiple times
  2. One logical change per migration: Keep migrations focused and atomic
  3. Never modify applied migrations: Create new migrations to fix issues
  4. Test migrations: Run them against a test database first
  5. Use transactions: Each migration runs in a transaction and rolls back on error
  6. Sequential versioning: Keep version numbers in order (001, 002, 003...)

Error Handling

  • Migrations run in transactions and rollback automatically on failure
  • Execution stops at the first failed migration
  • Check logs for detailed error messages
  • Fix the migration and run again

Examples

Multi-Schema Setup

# Migrate public schema
DB_SCHEMA=public db-migrate

# Migrate app schema
DB_SCHEMA=app db-migrate

Check What Will Run

db-migrate status

Output:

Total migrations: 5
Applied: 3
Pending: 2

Migrations:
  ✓ 001 - 001__init__create_users.sql (2025-01-07T10:30:00Z)
  ✓ 002 - 002__update__add_roles.sql (2025-01-07T10:30:05Z)
  ✓ 003 - 003__fix__email_index.sql (2025-01-07T10:30:10Z)
  ○ 004 - 004__update__add_timestamps.sql
  ○ 005 - 005__init__create_posts.sql

Requirements

  • Bun >= 1.2.0 (for native PostgreSQL support)
  • PostgreSQL >= 9.6

License

MIT

Contributing

Issues and pull requests are welcome!

Links