githubEdit

CLI

The SOM CLI generates type-safe database access code from your Go models.

Basic Command

go run github.com/go-surreal/som@latest -i <input_dir>

Flags

Flag
Description
Default

-i, --in

Input model directory (relative to go.mod)

(none, omit for init mode)

-o, --out

Output directory

gen/som

-v, --verbose

Show detailed generation progress

false

--dry

Simulate generation without writing files

false

--no-check

Skip version compatibility checks

false

--no-count-index

Disable automatic COUNT index generation

false

--wire

Override wire generation: "no", "google", or "goforj"

auto-detect

Example

# Generate from ./model to ./gen/som (default output)
go run github.com/go-surreal/som@latest -i ./model

# Generate to custom output directory
go run github.com/go-surreal/som@latest -i ./model -o ./db/generated

Initialization Mode

When --in is omitted, SOM generates only static base files. This is useful for bootstrapping a new project so types like som.Node are available before you define models:

Installing the Binary

For faster execution, install the binary:

Then run directly:

Output Structure

The generator creates the following structure:

Generated Packages

som (root)

Core types and client:

  • Node[T] - Base type for database records (generic over ID type)

  • Edge - Base type for relationships

  • Timestamps - Auto-managed timestamp fields

  • OptimisticLock - Version-based conflict detection

  • SoftDelete - Non-destructive deletion

  • Email, Password[A], SemVer - Special types

  • ULID, UUID, Rand - ID types

  • ArrayID, ObjectID - Complex ID markers

repo

Repository implementations with CRUD operations:

query

Fluent query builders:

filter

Type-safe filter conditions:

by

Sort definitions:

with

Fetch paths for eager loading:

conv

Internal converters between model and database representations. Not typically used directly.

relate

Edge relationship builders:

Version Pinning

Pin to a specific version for reproducible builds:

Regenerating Code

Run the generator whenever you:

  • Add or remove model fields

  • Create new Node or Edge types

  • Rename models or fields

  • Change field types

  • Add or remove struct tags

Troubleshooting

Models not detected

Ensure structs embed som.Node[T] or som.Edge:

Import errors

After model changes, regenerate and run:

Conflicting types

If you get type conflicts, ensure your model package has a unique name and doesn't shadow generated packages.

Last updated