Configuration
LiteORM is configured at Open time — there is no global default DB and no config file. Every backend’s Open takes the same variadic liteorm.Option values, plus a backend-specific connection argument (a path, a DSN, or a config struct). This page catalogs the portable options and the per-backend config objects; for how to open each backend see Backends, and for the observability options in depth see Observability and Logging.
Open-time options
Section titled “Open-time options”The options are functions of type liteorm.Option. They are portable — the same three work on every backend, because each backend’s Open forwards them to liteorm.New.
| Option | Signature | Effect | Default |
|---|---|---|---|
WithLogger |
WithLogger(l *slog.Logger) Option |
Sets the slog.Logger for statement logging. Every statement logs at debug level, so logging is silent unless l is enabled for slog.LevelDebug. A nil logger is replaced with a discard handler. |
slog.Default() |
WithSQLArgs |
WithSQLArgs(v bool) Option |
Controls whether bind-argument values appear in statement logs. Pass false to log only the argument count when values may be sensitive. Redacts the log only — not observers. |
true |
WithObserver |
WithObserver(o ...Observer) Option |
Registers observers invoked around every statement on the DB and on any transaction started from it — the seam for tracing, metrics, or audit. Multiple WithObserver options accumulate in registration order. Observers run independently of the log level. |
none |
WithLogger
Section titled “WithLogger”Statement logging goes through log/slog. Because every statement logs at debug level, you see nothing until your handler is enabled for slog.LevelDebug. Point the option at a handler you control — a JSON/text handler for structured logs, or the colored development handler in liteorm.org/log.
import ( "log/slog" "os"
liteorm "liteorm.org" "liteorm.org/dialect/sqlite")
h := slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug})db, err := sqlite.Open("app.db", liteorm.WithLogger(slog.New(h)))WithSQLArgs
Section titled “WithSQLArgs”Bind-argument values are shown by default, which is what makes a logged statement traceable. When a statement carries secrets — a password hash, a token — pass false so the log records the argument count instead of the values.
db, err := sqlite.Open("app.db", liteorm.WithLogger(logger), liteorm.WithSQLArgs(false), // log "3 args", never the values)This flag redacts the statement log only. An observer still receives the real values in QueryEvent.Args; redact there too if the observer forwards arguments to a sensitive sink.
WithObserver
Section titled “WithObserver”An observer wraps statement execution with BeforeQuery / AfterQuery. Register one or more; they compose as an onion — BeforeQuery in registration order, AfterQuery in reverse — and fire on both the *DB and any transaction started from it.
db, err := sqlite.Open("app.db", liteorm.WithObserver(metrics, tracing))See Observability for full metrics and tracing recipes.
Backend connection arguments
Section titled “Backend connection arguments”Each backend’s Open takes the portable options above plus a connection argument. That argument is the per-backend configuration surface — for the SQL databases it is a DSN string, and for SQLite it is a path or a gosqlite.Config.
| Backend | Entry point | Connection argument |
|---|---|---|
| SQLite | sqlite.Open(path string, opts ...liteorm.Option) |
A filesystem path or ":memory:". Applies the production pragma preset (WAL, busy timeout, foreign keys on). |
| SQLite (full config) | sqlite.OpenConfig(cfg gosqlite.Config, opts ...liteorm.Option) |
A full gosqlite.Config for a custom VFS, non-default pragmas, or pool sizing. |
| SQLite (encrypted) | sqlite.OpenEncrypted(path string, key []byte, opts ...liteorm.Option) |
A path plus a 32-byte key (Adiantum at rest). See Encryption. |
| Postgres | postgres.Open(ctx context.Context, dsn string, opts ...liteorm.Option) |
A libpq connection string or URL, as accepted by pgx. |
| MySQL | mysql.Open(ctx context.Context, dsn string, opts ...liteorm.Option) |
A go-sql-driver/mysql DSN. Open pings before returning. |
| MSSQL | mssql.Open(ctx context.Context, dsn string, opts ...liteorm.Option) |
A sqlserver://… URL or ADO connection string. Open pings before returning. |
The SQL backends take their driver configuration inside the DSN — pool sizing, TLS, timeouts, and driver flags are all DSN query parameters, so there is no separate config struct to pass. SQLite is the exception: OpenConfig accepts a gosqlite.Config when you need a custom VFS or explicit pragma/pool control that a path alone cannot express.
import ( gosqlite "gosqlite.org" "liteorm.org/dialect/sqlite")
db, err := sqlite.OpenConfig(gosqlite.Config{ Path: "app.db", Pragmas: gosqlite.RecommendedPragmas(),}, liteorm.WithLogger(logger))Reading configuration back
Section titled “Reading configuration back”The constructed *liteorm.DB exposes what it was configured with, which is handy in middleware and tests:
db.Logger() // the *slog.Logger in usedb.LogArgs() // the WithSQLArgs setting (true by default)db.Dialect() // the backend's dialectA transaction started from the DB carries the same logger, LogArgs setting, and observers, so statements inside a transaction log and are observed identically.
See also
Section titled “See also”- Backends — per-backend
Open, DSN shapes, and the capability matrix. - Connecting to a database — opening a database step by step.
- Observability — metrics and tracing through
WithObserver. - Logging — the
log/slogstatement log andWithSQLArgsredaction. - Full API:
liteorm.org.