Skip to content

CLI Reference

Complete reference for the SQG command-line interface.

Generate code from a configuration file.

Terminal window
sqg sqg.yaml
sqg path/to/config.yaml

Options:

OptionDescription
--validateValidate configuration without generating code
--format <format>Output format: text (default) or json
--verboseEnable debug logging

Examples:

Terminal window
# Generate code
sqg sqg.yaml
# Validate only
sqg --validate sqg.yaml
# JSON output for tooling
sqg --format json sqg.yaml
# Debug mode
sqg --verbose sqg.yaml
# Combine options
sqg --validate --format json sqg.yaml

Initialize a new SQG project with example files.

Terminal window
sqg init [options]

Options:

OptionDefaultDescription
-t, --generator <generator>typescript/sqliteCode generation generator
-o, --output <dir>./generatedOutput directory for generated files
-f, --forcefalseOverwrite existing files

Examples:

Terminal window
# TypeScript + SQLite (default)
sqg init
# TypeScript + DuckDB project
sqg init --generator typescript/duckdb
# Java + SQLite project
sqg init --generator java/sqlite
# Java + DuckDB with Arrow API
sqg init --generator java/duckdb/arrow
# Custom output directory
sqg init --output ./src/db
# Overwrite existing files
sqg init --force
# Full example
sqg init --generator typescript/duckdb --output ./src/generated --force

Created files:

./
├── sqg.yaml # Project configuration
├── queries.sql # Example SQL file with migrations and queries
└── generated/ # Output directory (empty)

Display SQL annotation syntax reference.

Terminal window
sqg syntax

Shows:

  • Query types (MIGRATE, QUERY, EXEC, TESTDATA)
  • Modifiers (:one, :pluck, :all)
  • Variable syntax (@set, ${var})
  • Examples

Start MCP (Model Context Protocol) server for AI assistants.

Terminal window
sqg mcp

The MCP server runs on stdio and provides AI assistants (like Claude Desktop and Cursor) with access to SQG’s code generation capabilities.

Available tools:

  • generate_code - Generate type-safe code from SQL queries
  • validate_sql - Validate SQL queries with SQG annotations

Example configuration for Claude Desktop:

{
"mcpServers": {
"sqg": {
"command": "sqg",
"args": ["mcp"]
}
}
}

See the Build with AI guide for detailed setup instructions.


These options work with all commands:

OptionDescription
-v, --versionDisplay version number
-h, --helpDisplay help for command
--verboseEnable debug logging
--format <format>Output format: text or json

Use --format json for machine-readable output, ideal for:

  • CI/CD pipelines
  • Editor integrations
  • AI assistants
  • Build tools
Terminal window
sqg --format json sqg.yaml
{
"status": "success",
"generatedFiles": [
"/path/to/generated/my-app.ts"
]
}
Terminal window
sqg --validate --format json sqg.yaml
{
"valid": true,
"project": {
"name": "my-app",
"version": 1
},
"sqlFiles": ["queries.sql"],
"targets": ["typescript/sqlite"]
}
Terminal window
sqg --format json invalid.yaml
{
"status": "error",
"error": {
"code": "FILE_NOT_FOUND",
"message": "SQL file not found: queries.sql",
"suggestion": "Check that queries.sql exists relative to /path/to/project",
"context": {
"file": "/path/to/project/queries.sql"
}
}
}

SQG provides structured error codes for programmatic handling:

CodeDescription
CONFIG_PARSE_ERRORInvalid YAML syntax in config file
CONFIG_VALIDATION_ERRORConfiguration doesn’t match schema
FILE_NOT_FOUNDSQL or config file doesn’t exist
INVALID_GENERATORUnknown or invalid generator
SQL_PARSE_ERRORInvalid SQL annotation syntax
SQL_EXECUTION_ERRORQuery failed during type introspection
DUPLICATE_QUERYTwo queries have the same name
MISSING_VARIABLEVariable referenced but not defined with @set
VALIDATION_ERRORGeneral validation error
DATABASE_ERRORDatabase connection or execution error
TYPE_MAPPING_ERRORUnable to map SQL type to generator language

All errors include actionable suggestions:

{
"error": {
"code": "INVALID_TARGET",
"message": "Invalid generator 'typescript/mysql'",
"suggestion": "Valid targets: typescript/sqlite, typescript/duckdb, java/sqlite, java/duckdb, java/postgres"
}
}

CodeMeaning
0Success
1Error (check output for details)

VariableDescription
SQG_POSTGRES_URLPostgreSQL connection string for type introspection

Example:

Terminal window
export SQG_POSTGRES_URL="postgresql://user:pass@localhost:5432/mydb"
sqg sqg.yaml

See Getting Started for full configuration documentation.

Minimal example:

version: 1
name: my-app
sql:
- files:
- queries.sql
gen:
- generator: typescript/sqlite
output: ./src/generated/

Generators follow the format <language>/<engine>[/<driver>]:

GeneratorEngineDescription
typescript/sqliteSQLiteTypeScript with better-sqlite3 (default driver)
typescript/duckdbDuckDBTypeScript with @duckdb/node-api (default driver)
java/sqliteSQLiteJava with JDBC (default driver)
java/duckdbDuckDBJava with JDBC (default driver)
java/duckdb/arrowDuckDBJava with DuckDB Arrow API
java/postgresPostgreSQLJava with JDBC (default driver)

The driver can be omitted to use the default. For example, typescript/sqlite is equivalent to typescript/sqlite/better-sqlite3.

See individual generator pages for detailed documentation:


Terminal window
sqg --verbose sqg.yaml

Shows:

  • SQL file parsing details
  • Database initialization
  • Query execution
  • Type introspection results
Terminal window
sqg --validate sqg.yaml

Catches configuration errors before running full generation.

Terminal window
sqg --version

Ensure you’re running the expected version.