Skip to content

API Reference

/

Values

Range

Range is a generic type for a SurrealDB range, such as 1..=10. Each of its two bounds can include its value in the range or exclude it. Its main use is as the ID of a RecordID, to work on a range of records.

Package: github.com/surrealdb/surrealdb.go/v2/pkg/models

Source: pkg/models/range.go

type Range[T any, TBeg Bound[T], TEnd Bound[T]] struct {
	Begin *TBeg
	End   *TEnd
}

A nil Begin or End leaves that side of the range open. CBOR tag: 49.

A bound whose value is in the range.

type BoundIncluded[T any] struct {
	Value T
}

CBOR tag: 50.

A bound whose value is not in the range.

type BoundExcluded[T any] struct {
	Value T
}

CBOR tag: 51.

The constraint for the two bound types.

type Bound[T any] interface {
	BoundIncluded[T] | BoundExcluded[T]
}

A range of record IDs in one table.

type RecordRangeID[T any, TBeg Bound[T], TEnd Bound[T]] struct {
	Range[T, TBeg, TEnd]
	Table Table
}

Use a Range as the ID of a RecordID to select, update or delete the records in the range. This example selects reading:2 and reading:3:

readings := models.RecordID{
	Table: "reading",
	ID: models.Range[int, models.BoundIncluded[int], models.BoundExcluded[int]]{
		Begin: &models.BoundIncluded[int]{Value: 2},
		End:   &models.BoundExcluded[int]{Value: 4},
	},
}

results, err := surrealdb.Select[[]map[string]any](ctx, db, readings)

In a record ID range, the value of a bound cannot be nil or models.None. Encoding such a range returns an error. For an array ID, use models.None inside the array as the lowest value of a part, as in []any{"London", models.None}.

The query builder has helpers that build a range, such as surrealql.RangeBeginInclusiveEndExclusive(2, 4), surrealql.RangeOpenBeginInclusive(2) and surrealql.RangeOpen().

Returns the range as text for display, such as 2..4 or 1>..=10. An open side is empty, so a range open on both sides is ... The text is not always valid SurrealQL, so do not send it in a query.

Returns: string

Returns the operator between the bounds: .., ..=, >.. or >..=.

Returns: string

Was this page helpful?

Last updated