githubEdit

Architecture

This page describes how SOM works internally.

Generation Pipeline

SOM uses a code generation approach to provide type-safe database access. The generation workflow:

Input Models (Go structs)


┌───────────────────┐
│      Parser       │  Analyzes Go source using gotype
│   (core/parser)   │  Identifies: Nodes, Edges, Structs, Enums
└─────────┬─────────┘


┌───────────────────┐
│  Code Generator   │  Uses jennifer for Go code generation
│  (core/codegen)   │  Produces type-safe builders
└─────────┬─────────┘


┌───────────────────┐
│   Output Files    │  Complete database access layer
│    (gen/som/)     │  Ready to use in your application
└───────────────────┘

1. Parsing Phase

The parser analyzes your Go source files to identify:

  • Nodes: Structs embedding som.Node[T] (where T is an ID type)

  • Edges: Structs embedding som.Edge

  • Structs: Regular structs used as fields (for nested types)

  • Enums: Types implementing som.Enum interface

  • Fields: All fields with their types, tags, and constraints

2. Code Generation Phase

Using the jenniferarrow-up-right library, SOM generates idiomatic Go code:

  • Static library code (filters, builders, utilities)

  • Per-model repositories and query builders

  • Type converters for database serialization

  • Filter and sort definitions for each field type

3. Output Phase

Generated files are written to the output directory with their own go.mod, creating a self-contained package.

Generated Code Structure

Key Components

Nodes

Nodes represent database records. Any struct embedding som.Node[T] becomes a SurrealDB table:

The type parameter T determines the ID format (som.ULID, som.UUID, som.Rand, or a custom struct with som.ArrayID/som.ObjectID).

Edges

Edges represent graph relationships between nodes:

Repositories

Generated for each model with full CRUD operations:

Query Builder

Fluent API with method chaining:

Note: Methods like Order, Limit, Start, etc. return BuilderNoLive, which does not expose Live(). This prevents constructing invalid live queries with ordering or pagination.

Type Converters

Handle transformation between Go types and SurrealDB format:

Database Communication

CBOR Protocol

SOM uses CBOR (Concise Binary Object Representation) for SurrealDB communication. Custom types use CBOR tags:

Type
CBOR Tag
Format

DateTime

12

[unix_seconds, nanoseconds]

Duration

14

Nanoseconds as int64

UUID

37

16-byte binary

Last updated