db.transform

Malli-based JSON transformation utilities for database columns.

Provides bidirectional transforms between database JSON (string keys) and application data (keyword keys), driven by Malli schemas. Use these utilities in model repositories to explicitly transform JSON columns read from PostgreSQL.

For simple flat maps without complex nesting, use keywordize-keys. For schema-driven transforms that handle nested structures, enums, and special key types, use decode with a Malli schema.

For multi schemas, use db-dispatch instead of a plain keyword for the dispatch function to handle both string and keyword keys/values.

db-decoding-transformer

Composite transformer for decoding DB JSON to application data.

Transforms: - String keys in :map schemas to kebab-case keywords (via key-transformer) - String enum values to keywords (via enum-value-transformer)

Does NOT transform keys in :map-of schemas, preserving data value keys like player IDs and hex positions as their original type.

For multi schemas, use db-dispatch for the dispatch function.

db-dispatch

(db-dispatch k)

Creates a dispatch function for multi schemas that handles DB JSON.

Malli’s multi dispatch happens before transformers run, so the dispatch function must handle both string and keyword keys/values from the database.

Usage in schema: [:multi {:dispatch (db-dispatch :status)} :POSSESSED BallPossessedSchema :LOOSE BallLooseSchema]

db-encoding-transformer

Composite transformer for encoding application data to DB JSON.

Transforms kebab-case keyword keys to snake_case strings.

decode

(decode data schema)

Decodes database JSON to application data using a Malli schema.

Takes raw JSON data (with string keys) from the database and transforms it according to the provided schema. String keys in :map schemas become kebab-case keywords, while keys in :map-of schemas are preserved as-is.

Multi schemas with keyword dispatch are automatically wrapped with db-dispatch to handle both string and keyword keys/values from the database. This means shared CLJC schemas work without modification.

Returns nil if data is nil.

encode

(encode data schema)

Encodes application data to database JSON format using a Malli schema.

Takes application data (with keyword keys) and transforms it for storage in PostgreSQL JSON columns. Kebab-case keyword keys become snake_case strings.

Returns nil if data is nil.

keywordize-keys

(keywordize-keys x)

Recursively converts string keys to keywords in a data structure.

Use for simple JSON columns that don’t require schema-driven transforms. For complex nested structures with special key handling, use decode with a Malli schema instead.