LinuxCommandLibrary

xo

Extract archives with automatic sub-directory creation

TLDR

Lint files in the "src" directory

$ xo
copy

Lint a given set of files
$ xo [path/to/file1.js path/to/file2.js ...]
copy

Automatically fix any lint issues found
$ xo --fix
copy

Lint using spaces as indentation instead of tabs
$ xo --space
copy

Lint using the "prettier" code style
$ xo --prettier
copy

SYNOPSIS

xo [OPTIONS] <DSN>
xo [OPTIONS] <COMMAND> [ARGS]

Common usage involves specifying a Database Source Name (DSN) directly or using subcommands for specific operations.
Example DSN: postgres://user:pass@host:port/dbname?sslmode=disable

PARAMETERS

-o, --out


    Output directory for generated Go files. Defaults to current directory.

-N, --no-tests
    Do not generate test files.

-t, --template
    Use a specific template set (e.g., 'go', 'json').

-s, --schema
    Specify a database schema to inspect (e.g., 'public' for PostgreSQL).

-T, --table
    Only generate code for specific tables (can be repeated).

-v, --verbose
    Enable verbose output during generation.

-V, --version
    Display xo version information.

--fk
    Specify custom foreign key columns for specific tables.

--suffix
    Suffix to append to generated file names.

--single-file
    Output all generated code into a single file.

DESCRIPTION

xo is an open-source command-line tool written in Go that generates Go source code from a database schema. It connects to a wide variety of relational databases (PostgreSQL, MySQL, SQLite3, Oracle, SQL Server) and introspects their structure (tables, views, columns, foreign keys, stored procedures). Based on this introspection, xo creates Go types, functions, and methods that provide a type-safe and idiomatic Go API for interacting with the database. This approach helps reduce boilerplate code, prevents common SQL injection vulnerabilities by encouraging parameterized queries, and improves development efficiency by providing compile-time checks for database interactions. It's often used in Go projects to build data access layers, serving as an alternative to full-fledged ORMs by providing a lightweight, code-generation approach.

CAVEATS

xo is not a standard, pre-installed Linux command-line utility. It must be explicitly installed, typically via Go's package management tools (e.g., go install github.com/xo/xo@latest). While powerful, it has a learning curve, especially for configuring templates and handling complex database schemas or custom Go types. It's primarily useful for Go developers working with relational databases and may not be suitable for all data access patterns.

CUSTOM TEMPLATES

xo supports custom user-defined templates, allowing developers to tailor the generated Go code to specific project conventions or add specialized functionalities. This flexibility is a key differentiator from many traditional ORMs.

DATABASE SUPPORT

A notable strength of xo is its broad support for various relational database systems, including PostgreSQL, MySQL, SQLite3, SQL Server, and Oracle, making it versatile for diverse development environments.

HISTORY

xo was initially developed by Kyle F. Baker (knq) as an open-source project to address the need for type-safe database interactions in Go applications. Its development began around 2015, gaining traction within the Go community for its flexible code generation capabilities. It has since been maintained and extended by various contributors, evolving with Go language features and supporting a broader range of database systems. Its focus has remained on providing a robust, flexible, and non-opinionated way to generate Go code directly from database schemas.

SEE ALSO

go(1), psql(1), mysql(1), dbmate(1)

Copied to clipboard