go-auditGo Audit
API Reference

Dialect Interface

The Dialect abstraction and how to implement a new one.

Dialects encapsulate database-specific DDL and parameter placeholder style. Go Audit ships with three implementations — PostgreSQL, MySQL, and SQLite.

Types

type DialectType = dialect.Type // string alias: "postgres", "mysql", "sqlite", …

const (
    PostgreSQL DialectType = "postgres"
    MySQL      DialectType = "mysql"
    SQLite     DialectType = "sqlite"
)

The Interface

type Dialect interface {
    Name() Type
    CreateDataTableSQL(table string) string
    CreateAPITableSQL(table string) string
    CreateIndexesSQL(table string) []string
    CreateAPIIndexesSQL(table string) []string
    Placeholder(n int) string
}
MethodPurpose
Name()Returns the dialect's Type identifier.
CreateDataTableSQLCREATE TABLE IF NOT EXISTS ... for the data audit table.
CreateAPITableSQLSame for the API audit table.
CreateIndexesSQLList of CREATE INDEX statements (empty for MySQL, which declares indexes inline).
CreateAPIIndexesSQLSame for API audit indexes.
Placeholder(n)Parameter placeholder for n-th arg ($1 for Postgres, ? for MySQL/SQLite).

Built-In Dialects

  • audit.PostgreSQLJSONB + GIN indexes; richest JSON query capability downstream (though Go Audit itself does not push JSON predicates into queries).
  • audit.MySQLJSON column type; inline B-tree indexes; InnoDB.
  • audit.SQLite — stores JSON as TEXT; B-tree indexes; perfect for tests.

Registering a Custom Dialect

  1. Implement audit.Dialect for your database.

  2. Register it globally:

    audit.RegisterDialect(audit.DialectType("cockroach"), myDialect)
  3. Use the same name in Config:

    audit.New(sqlDB, audit.Config{
        Dialect: audit.DialectType("cockroach"),
    })

Registration is thread-safe (guarded by sync.RWMutex), so you can register additional dialects from init code.

When Would You Need This?

  • A cloud database variant (Aurora, AlloyDB, Spanner) where the standard dialect needs tweaks.
  • An analytics-focused backend like DuckDB or ClickHouse for read patterns that benefit from columnar storage.

Helper: audit.DialectFor

d := audit.DialectFor(audit.PostgreSQL)
// returns the registered Dialect (nil if unknown)

Rarely needed directly — the constructor does this lookup for you — but useful for tests or custom tooling.

On this page