githubEdit

Features

Core Features

Code Generation

Generate type-safe database access code from Go struct models. The generator produces:

  • Repositories with Create, Read, Update, Delete, Insert, and Query methods

  • Query builders with fluent, chainable API

  • Filter builders with 50+ operations per field type

  • Sort builders for ordering results

  • Converters for model-to-database transformation

Query Builder

Fluent API for building complex queries with compile-time type checking:

users, _ := client.UserRepo().Query().
    Where(filter.User.Email.Contains("@example.com")).
    Where(filter.User.CreatedAt.After(lastMonth)).
    Order(by.User.Name.Asc()).
    Limit(10).
    All(ctx)

Real-Time Queries

Subscribe to database changes with type-safe live queries:

Async Operations

All query methods have async variants for concurrent execution:

Eager Loading (Fetch)

Load related records in a single query:

Automatic Timestamps

Embed som.Timestamps for auto-managed CreatedAt and UpdatedAt fields:

Optimistic Locking

Embed som.OptimisticLock to prevent concurrent update conflicts:

Soft Delete

Embed som.SoftDelete for non-destructive deletion with automatic query filtering:

Iterator Methods

Process large result sets efficiently with Go range-over-func:

Bulk Insert

Insert multiple records efficiently in a single operation:

Lifecycle Hooks

Register hooks for pre/post CRUD operations:

BM25-based relevance searching with highlighting and score sorting:

Context Cache

Optional request-scoped caching for Read operations:

Complex ID Types

Support for array and object-based record IDs for range-efficient queries:

Supported Data Types

Primitive Types

Type
Filter Operations
Sort
Notes

string

30+ operations

Yes

Contains, StartsWith, IsEmail, Slug, etc.

int, int8, int16, int32, int64

Numeric + comparison

Yes

Add, Sub, Mul, Div, Abs

uint8, uint16, uint32

Numeric + comparison

Yes

float32, float64

Numeric + comparison

Yes

bool

Equal, IsTrue, IsFalse

Yes

rune

Numeric operations

Yes

Treated as int32

byte, []byte

Basic comparison

No

Binary data

Time Types

Type
Filter Operations
Sort
CBOR Tag

time.Time

Before, After, Add, Sub, Floor, Round, Format

Yes

12

time.Duration

Before, After, Add, Sub

Yes

14

time.Month

Comparable operations

Yes

-

time.Weekday

Comparable operations

Yes

-

Special Types

Type
Filter Operations
Sort
Notes

uuid.UUID (google)

Equal, NotEqual, In, NotIn

Yes

CBOR Tag 37

uuid.UUID (gofrs)

Equal, NotEqual, In, NotIn

Yes

CBOR Tag 37

url.URL

Equal, NotEqual

Yes

-

som.Email

Equal, In, User(), Host()

Yes

-

som.Password[A]

Zero, IsNil (auto-hashed)

No

-

som.SemVer

Equal, Compare, Major, Minor, Patch

Yes

-

Geometry Types

SOM supports geometry types from three popular Go libraries:

  • github.com/paulmach/orb - Point, LineString, Polygon, MultiPoint, etc.

  • github.com/peterstace/simplefeatures/geom - Same geometry types

  • github.com/twpayne/go-geom - Same geometry types

Custom Types

  • som.Enum - String-based enumerations with type-safe constants

Collections

  • Slices of any supported type with special operations:

    • Length(), Contains(), ContainsAll(), ContainsAny()

    • Empty(), NotEmpty(), Intersects(), Inside()

  • Pointers to any type with IsNil() / IsNotNil() checks

Embedded Structs

Nest structs within models with full filter support on nested fields:

Query Features

Execution Methods

Method
Returns
Description

All(ctx)

([]*Model, error)

All matching records

First(ctx)

(*Model, error)

First match or ErrNotFound

Count(ctx)

(int, error)

Count of matches

Exists(ctx)

(bool, error)

Whether any match exists

Live(ctx)

(<-chan LiveResult, error)

Stream of changes

Query Modifiers

Method
Description

Where(...)

Add WHERE conditions (AND)

Order(...)

Sort results

OrderRandom()

Random ordering

Limit(n)

Max results

Start(n)

Skip results

Fetch(...)

Eager load relations

Timeout(d)

Query timeout

Parallel(bool)

Parallel execution

TempFiles(bool)

Disk-based processing for large result sets

WithDeleted()

Include soft-deleted records

Between(from, to)

Range filter with configurable bounds

Range(from, to)

Range query for string and complex IDs

Filter Operations

String Operations (30+)

  • Comparison: Equal, NotEqual, In, NotIn

  • Pattern: Contains, StartsWith, EndsWith, FuzzyMatch

  • Validation: IsEmail, IsURL, IsDomain, IsIP, IsUUID, IsSemVer

  • Transform: Lowercase, Uppercase, Trim, Slug, Reverse

  • Utility: Len, Split, Words, Join, Concat, Replace, Slice

Numeric Operations

  • Comparison: Equal, NotEqual, LessThan, GreaterThan, etc.

  • Arithmetic: Add, Sub, Mul, Div, Raise, Abs

Combining Filters

Raw Queries

Execute arbitrary SurrealQL when the query builder doesn't cover your use case:

Transactions

Group multiple operations into an atomic unit with client-side transactions:

Between Filter

Range filter with configurable bound inclusivity:

Version Verification

SOM automatically verifies the SurrealDB server version on connect, ensuring compatibility with the minimum required version.

Structured Server Errors

SurrealDB v3 structured errors are exposed as som.ServerError for detailed programmatic error handling.

Current Limitations

Unsupported Go Types

  • uint, uint64, uintptr - SurrealDB limitations with large integers

  • complex64, complex128 - No SurrealDB equivalent

  • map types - Planned for future release

Other Limitations

  • No automatic migrations (schema changes require manual handling)

Last updated