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
Definitions
Range
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.
BoundIncluded
A bound whose value is in the range.
type BoundIncluded[T any] struct {
Value T
}CBOR tag: 50.
BoundExcluded
A bound whose value is not in the range.
type BoundExcluded[T any] struct {
Value T
}CBOR tag: 51.
Bound
The constraint for the two bound types.
type Bound[T any] interface {
BoundIncluded[T] | BoundExcluded[T]
} RecordRangeID
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
}Selecting a range of records
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().
Methods
.String()
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
.GetJoinString()
Returns the operator between the bounds: .., ..=, >.. or >..=.
Returns: string
See also
RecordID for record IDs whose ID is a range
Data manipulation for selecting a range of records
SurrealQL ranges for the underlying data model