@ssegrera/ts-goose
v1.1.3
Published
A lightweight database migration tool for TypeScript and Bun
Maintainers
Readme

ts-goose
A lightweight database migration tool for TypeScript and Bun, inspired by goose.
It supports both SQL and TypeScript migrations, and currently supports PostgreSQL and SQLite.
Installation
Using bunx (recommended)
bunx @ssegrera/ts-goose [command]Using pnpmx/npx
pnpx @ssegrera/ts-goose [command]
# or
npx @ssegrera/ts-goose [command]Global installation
# Using bun
bun add -g @ssegrera/ts-goose
# Using pnpm
pnpm add -g @ssegrera/ts-goose
# Using npm
npm install -g @ssegrera/ts-gooseUsage
Commands
ts-goose create <name> [sql|ts]- Create a new migration filets-goose up- Apply all pending migrationsts-goose up-by-one- Apply the next pending migrationts-goose up-to VERSION- Apply all pending migrations up to a specific versionts-goose down-to VERSION- Rollback all migrations up to a specific versionts-goose reset- Rollback all migrationsts-goose down- Rollback the last applied migrationts-goose status- Show migration status
Examples
# Create a new SQL migration
bunx @ssegrera/ts-goose create add_users_table sql
# Create a new TypeScript migration
bunx @ssegrera/ts-goose create add_products_table ts
# Apply the next migration
bunx @ssegrera/ts-goose up
# Check migration status
bunx @ssegrera/ts-goose status
# Rollback the last migration
bunx @ssegrera/ts-goose downConfiguration
The following environment variables can be used to configure the tool:
TSGOOSE_DRIVER- The database driver to use (postgresorsqlite, defaults topostgres)TSGOOSE_DBSTRING- The database connection string to use (defaults topostgresql://postgres:postgres@localhost:5432/postgresfor postgres, or a file path like./database.sqlitefor sqlite)TSGOOSE_MIGRATION_DIR- The directory containing the migration files (defaults to./migrations)TSGOOSE_TABLE_NAME- The name of the table to use for storing the migration history (defaults totsgoose.migration)
Using as a Library
You can also use ts-goose programmatically in your TypeScript/Bun projects:
PostgreSQL Example
import { SQL } from "bun";
import { upCommand, PostgresStore, DEFAULT_CONFIG } from "@ssegrera/ts-goose";
// Connect to postgres database
const db = new SQL(DEFAULT_CONFIG.db_url, { adapter: "postgres" });
// Run migrations
await upCommand(db, PostgresStore, {
migration_dir: DEFAULT_CONFIG.migration_dir,
table_name: DEFAULT_CONFIG.table_name,
});
// Close connection
await db.end();This allows you to integrate migration execution into your application startup, tests, or custom deployment scripts.
Development
To install dependencies:
bun installTo run locally:
bun run index.ts [command]To run tests:
bun testTo build:
bun run buildThis project was created using bun init in bun v1.3.0. Bun is a fast all-in-one JavaScript runtime.
