githubEdit

Struct

The struct type handles embedded Go structs as nested objects within your models.

Overview

Property
Value

Go Type

Embedded struct / *EmbeddedStruct

Database Schema

object / option<object>

CBOR Encoding

Direct

Sortable

No (use nested field sorting)

Definition

Define structs in your model package and embed them:

package model

// Embedded struct (not a Node, no ID)
type Address struct {
    Street  string
    City    string
    Country string
    ZipCode string
}

type Dimensions struct {
    Width  float64
    Height float64
    Depth  float64
}

type User struct {
    som.Node

    Name    string
    Address Address    // Required embedded struct
    Billing *Address   // Optional embedded struct
}

type Product struct {
    som.Node

    Name       string
    Dimensions Dimensions
}

Note: Embedded structs do NOT embed som.Node - they are plain Go structs.

Schema

Generated SurrealDB schema:

Creating Struct Values

Filter Operations

Nested Field Filtering

Access nested fields using dot notation in the where clause:

Complex Nested Filters

Optional Struct Nil Checks

Optional Struct Field Access

Sorting

Structs themselves are not sortable, but nested fields are:

Anonymous Embedding

Go's anonymous embedding is supported:

Deeply Nested Structs

Structs can contain other structs:

Common Patterns

Filter by Location

Filter by Country

Users with Billing Address

Dimension Filtering

Complete Example

Filter Reference Table

Struct Access

Operation
Description
Returns

<FieldName>

Access nested field

Field's filter type

IsNil()

Struct is null (ptr)

Bool filter

IsNotNil()

Struct is not null (ptr)

Bool filter

Nested Field Operations

All filter operations available for the nested field's type:

Best Practices

Keep Structs Simple

Embedded structs work best for simple value objects:

Avoid Deep Nesting

Limit nesting depth for maintainability:

Use Pointers for Optional

Optional embedded structs should be pointers:

Last updated