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
}| Method | Purpose |
|---|---|
Name() | Returns the dialect's Type identifier. |
CreateDataTableSQL | CREATE TABLE IF NOT EXISTS ... for the data audit table. |
CreateAPITableSQL | Same for the API audit table. |
CreateIndexesSQL | List of CREATE INDEX statements (empty for MySQL, which declares indexes inline). |
CreateAPIIndexesSQL | Same for API audit indexes. |
Placeholder(n) | Parameter placeholder for n-th arg ($1 for Postgres, ? for MySQL/SQLite). |
Built-In Dialects
audit.PostgreSQL—JSONB+ GIN indexes; richest JSON query capability downstream (though Go Audit itself does not push JSON predicates into queries).audit.MySQL—JSONcolumn type; inline B-tree indexes; InnoDB.audit.SQLite— stores JSON asTEXT; B-tree indexes; perfect for tests.
Registering a Custom Dialect
-
Implement
audit.Dialectfor your database. -
Register it globally:
audit.RegisterDialect(audit.DialectType("cockroach"), myDialect) -
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.