dbmodel - SQL Database Schema Management
raw JSON →dbmodel is a command-line utility and JavaScript library within the DbGate ecosystem, designed for managing SQL database schemas. It allows users to `load` an existing database structure into a local file-based project, `deploy` a local project's structure to a database, and `build` SQL scripts offline from a project. The model is represented as a collection of YAML files for tables (columns, indexes, primary keys, foreign keys) and SQL files for views, stored procedures, and functions. It leverages DbGate tooling and plugins to support various databases including MySQL, MS SQL Server, PostgreSQL, SQLite, Oracle, MariaDB, CockroachDB, and Amazon Redshift. The current stable version is 7.1.9, with frequent patch releases addressing fixes and minor enhancements. Its key differentiator is its model-first approach for schema management, avoiding data-loss operations during deployment by design, and its integration with the broader DbGate platform. For programmatic JavaScript interface usage, the related `dbgate-api` package is recommended.
Common errors
error ERROR: Cannot find module 'dbgate-plugin-mssql' ↓
npm install --save dbgate-plugin-mssql (or the appropriate plugin for your database) in your project. error Error: Database connection failed: Invalid connection string ↓
error Error: Command 'xyz' not found. ↓
load, deploy, or build. Refer to the dbmodel --help output for available commands. Warnings
gotcha The `deploy` command is explicitly designed not to perform any actions that lead to data loss. This means it will create new tables/columns, alter flags (NULL/NOT NULL), and manage views/procedures, but it will NOT delete tables, columns, or indexes that exist in the database but are no longer present in the local project model. These destructive changes must be handled manually. ↓
breaking Version 7.1.9 included a change where public DbGate cloud endpoints were migrated to new infrastructure. Users relying on DbGate cloud services might need to update their configurations or connection parameters to ensure continued availability and proper functioning. ↓
gotcha To connect to specific database engines (e.g., MS SQL Server, PostgreSQL), you must explicitly install and specify the corresponding `dbgate-plugin-*` package (e.g., `dbgate-plugin-mssql`). `dbmodel` does not bundle these drivers. ↓
security Version 7.1.9 fixed security issues related to insufficient validation for function and file names. Prior versions may have been vulnerable to injection or other security risks if user-provided names were not properly sanitized. ↓
Install
npm install dbmodel yarn add dbmodel pnpm add dbmodel Imports
- runCli wrong
const runCli = require('dbmodel').runCli;correctimport { runCli } from 'dbmodel'; - load wrong
const load = require('dbmodel').load;correctimport { load } from 'dbmodel'; - DatabaseEngine
import type { DatabaseEngine } from 'dbmodel/lib/types';
Quickstart
import { load, deploy, build } from 'dbmodel';
import { initializeApiEnvironment } from 'dbgate-api';
import { registerPlugins } from 'dbgate-api';
import dbgatePluginPostgres from 'dbgate-plugin-postgres';
async function main() {
// It's crucial to initialize the API environment and register plugins for programmatic use
initializeApiEnvironment();
registerPlugins(dbgatePluginPostgres);
const connectionOptions = {
server: process.env.DB_SERVER ?? 'localhost',
user: process.env.DB_USER ?? 'postgres',
password: process.env.DB_PASSWORD ?? '',
database: process.env.DB_DATABASE ?? 'mydatabase',
engine: 'postgres@dbgate-plugin-postgres' // Ensure the plugin is installed and registered
};
const projectFolder = './my-db-model';
const outputSqlFile = './my-schema.sql';
console.log('Loading database schema...');
await load({ connection: connectionOptions, folder: projectFolder });
console.log(`Schema loaded into ${projectFolder}`);
// Make some changes to the YAML files in projectFolder if needed
console.log('Deploying schema changes...');
await deploy({ connection: connectionOptions, folder: projectFolder });
console.log('Schema deployed.');
console.log('Building SQL script...');
await build({ engine: 'postgres@dbgate-plugin-postgres', folder: projectFolder, file: outputSqlFile });
console.log(`SQL script built to ${outputSqlFile}`);
}
main().catch(console.error);