A Go library for PostgreSQL schema management and SQL query building.
sqlok provides a fluent, type-safe API for building SQL queries and managing PostgreSQL schemas. It combines a query builder pattern with reflection-based schema introspection, making it easy to write database operations without manual SQL string concatenation.
- Query Builder - Fluent API for SELECT, INSERT, UPDATE, DELETE operations
- Schema Management - Table, Field, and ForeignKey definitions with constraint support
- Parameterized Queries - Safe against SQL injection via PostgreSQL placeholders (
$1,$2, etc) - Reflection-Based Tags - Define schema constraints using Go struct tags (
primary_key,unique, etc) - CLI Interface - Command-line tools for schema inspection and example generation
- Type-Safe - Leverage Go's type system for compile-time safety
go get github.com/candango/sqlok- Go 1.24 or higher
- PostgreSQL 12 or higher
package main
import "github.com/candango/sqlok"
// SELECT query
sql, args := sqlok.Select("id", "name", "email").
From("users").
Where("id=$1", 1).
Build()
// sql: "SELECT id, name, email FROM users WHERE id=$1"
// args: []any{1}
// INSERT query
sql, args := sqlok.NewInsertBuiler().
InsertInto("users").
Columns("name", "email").
Values("John", "john@example.com").
Returning("id").
Build()
// sql: "INSERT INTO users (name, email) VALUES($1, $2) RETURNING id"
// args: []any{"John", "john@example.com"}
// Complex WHERE with AND/OR
sql, args := sqlok.Select("*").
From("users").
Where("age=$1", 18).
And("status=$2", "active").
Build()import "github.com/candango/sqlok/internal/schema"
table := &schema.Table{
TableName: "users",
Schema: "public",
Fields: []*schema.Field{
{FieldName: "id", Type: "BIGSERIAL", Primary: true},
{FieldName: "name", Type: "VARCHAR(255)", Nullable: false},
{FieldName: "email", Type: "VARCHAR(255)", Nullable: false},
},
}import "github.com/candango/sqlok"
loader := sqlok.NewPostgresLoader("postgresql://user:password@localhost/dbname", ctx)
if err := loader.Connect(); err != nil {
log.Fatal(err)
}
defer loader.Disconnect()
if err := loader.Load(); err != nil {
log.Fatal(err)
}
tables := loader.Tables()-
builder.go(525 LOC) - Query builder implementationsQueryBuilderinterfaceSelectBuilder,InsertBuilder,UpdateBuilder,DeleteBuilder- Join and condition helpers (
And,Or)
-
sqlok.go(159 LOC) - Database connection and schema loadingDatabaseLoaderinterfacePostgresLoaderimplementation- Context management
-
schema/- Schema definitionsTable- Represents a database tableField- Represents a table columnForeignKey- Represents foreign key constraints with reference options
-
cli/- Command-line interfaceroot.go- Main CLI commanddatabase.go- Database operationsinit.go- Schema initializationexample.go- Example code generation
-
mapper.go- Result mapping (in development) -
namefmt.go- Name formatting utilities
make testTests use PostgreSQL with connection credentials from environment:
- Host:
localhost:5432 - User:
sqlok - Password: Set via
PGSQL_SQLOK_PASSWORDenvironment variable
GitHub Actions automatically tests against:
- Go 1.23
- Go 1.24
- Go 1.25
.
cmd/sqlok/ # CLI entry point
internal/
├── builder.go # Query builder (core)
├── sqlok.go # DB connection
├── schema/ # Schema definitions
├── cli/ # CLI commands
└── ...
dummy/ # Example models and tests
scripts/postgres/ # Database setup scripts
makefile # Build targets
- pgx/v5 - PostgreSQL driver
- cobra - CLI framework
- logrus - Structured logging
- testify - Testing utilities
See LICENSE file.
Contributions are welcome! Please ensure tests pass before submitting pull requests.
make test- Complete
mapper.gofor result scanning - Add UPDATE and DELETE builders
- Support for additional databases (MySQL, SQLite)
- Query optimization and performance analysis
- Extended documentation and examples