Registry / database / sql-mirror

sql-mirror

JSON →
library0.4.1jsnpmunverified

sql-mirror is an opinionated SQL migration generator that produces up/down SQL files from reusable raw SQL chunks. Version 0.4.1 is the latest stable release with no set release cadence. It focuses on consistency: automatically adds id columns using UUID v4, created_at/updated_at timestamps, and enforces topological ordering of tables based on foreign key references. Differentiators include a built-in trigger function for updated_at, zero runtime dependencies, and a JSON config output for version control.

npm install sql-mirror
INSTALL
IMPORT
SIG · SQL-MIRROR
S
sql-mirror
databasejavascriptv0.4.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

sql
import { sql } from 'sql-mirror'
const sql = require('sql-mirror')
ESM-only package; CommonJS require is not supported.
generateSqlFileContent
import { generateSqlFileContent } from 'sql-mirror'
import { generateSqlFileContent } from 'sql-mirror/generateSqlFileContent'
Function is exported from the main entry point, not a subpath.
SqlMirrorConfig
import type { SqlMirrorConfig } from 'sql-mirror'
Type is exported for TypeScript users; use import type for better compilation.

Generates up/down SQL migration files from a configuration object with table definitions.

import { sql, generateSqlFileContent } from 'sql-mirror'; import fs from 'fs'; const config = { extensions: [sql.extension['uuid-ossp']], tables: [ { name: 'user', columns: [ sql.column.email('email'), 'password_hash VARCHAR(255) NOT NULL', ], }, ], }; const { up, down } = generateSqlFileContent(config); fs.mkdirSync('./migrations', { recursive: true }); fs.writeFileSync('./migrations/up.sql', up); fs.writeFileSync('./migrations/down.sql', down);
Debug
Known issues
breakingThe sqlMirrorConfig object must have correct table references; missing references cause silent empty output.
fix
Ensure all foreign key references point to existing tables defined in the same config.
affects: all
gotchaThe package does not apply migrations; it only generates SQL files.
fix
Use a separate migration tool (e.g., pg-migrate, node-pg-migrate) to apply generated SQL.
affects: all
gotchaUUID extension is required but configurable; missing extension will cause errors.
fix
Always include 'uuid-ossp' in the extensions array to generate UUID primaries.
affects: all
gotchaTable names are case-sensitive and will be quoted in SQL; unexpected quoting may occur.
fix
Use lowercase table names to avoid double-quoting issues.
affects: all
Errors
Common errors & fixes
Error: Cannot find module 'sql-mirror'
Package not installed or used in a CommonJS context.
fix
Install the package: npm install -D sql-mirror, and use ESM imports (import ... from 'sql-mirror').
TypeError: sql.column.email is not a function
Missing import of 'sql' object or incorrect version.
fix
Ensure import { sql } from 'sql-mirror' is present and package version >=0.4.0.
Error: Cannot write file './migrations/up.sql': ENOENT, no such file or directory
Migrations directory does not exist.
fix
Create the directory with mkdirSync('./migrations', { recursive: true }) before writing files.
Upgrade
Version history
0.4.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
11 hits · last 30 days
node
8
Meta
1
OpenAI (training)
1
Resources
sql-mirror — npm install sql-mirror · libregistry