README ¶
SurrealDB Go Utils
A basic little package with some useful structs for working with surreal
Thing:
package main
import (
"github.com/idevelopthings/surrealdb.go.utils"
)
type User struct {
ID surrealdb_go_utils.Thing `json:"id"`
}
Thing will allow you to the regular surrealdb id formatting, and take out the Table or Value with ease. We can also use this for situations in queries.
It will also unescape the value for you, so when it's passed via an api endpoint as a normal id, like "user: u4y127h7fh78h1", it will automatically be unescaped.
Record:
This is useful for when you have a type for your Table, which will have a pointer value, for example. A Book with an Author:
package main
import (
"github.com/idevelopthings/surrealdb_go_utils"
)
type Author struct {
ID surrealdb_go_utils.Thing `json:"id"`
}
type Book struct {
ID surrealdb_go_utils.Thing `json:"id"`
Author surrealdb_go_utils.Record[Author] `json:"author"`
}
When you pull the Book from the db as normal, it will only return an id string, something
like {"id": "book:u4y127h7fh78h1", "author": "author:jsdfhf8f2h7fh"}
.
When you also fetch "author", this usually will cause issues unmarsalling the json.
But this "Record" will handle both situations, and marshal to json as the correct type
var book *Book
book.Author.IsId() // Returns true if the value is an id(record pointer)
book.Author.Id() // Returns the ID of the record
book.Author.IsRecord() // Returns true if it's the Author value/record
book.Author.Record() // Returns the Author record(if the query included `fetch author` for example)
But when you use the Record, it will automatically unescape the id, and then pull the Author from the db, and return the Author struct.
RecordList
This works the same as "Record" above, but for an array instead.
Documentation ¶
Index ¶
- Variables
- type ListType
- type Record
- type RecordList
- type RecordType
- type Thing
- func (thing *Thing) AsTypeThing() string
- func (thing *Thing) AsTypeThingParam(paramName string) string
- func (thing *Thing) Id() string
- func (thing *Thing) MarshalJSON() ([]byte, error)
- func (thing *Thing) Table() string
- func (thing *Thing) UnmarshalJSON(data []byte) error
- func (thing *Thing) Value() string
Constants ¶
This section is empty.
Variables ¶
var ErrInvalidThing = fmt.Errorf("invalid thing")
Functions ¶
This section is empty.
Types ¶
type Record ¶
type Record[T any] struct { // contains filtered or unexported fields }
func (*Record[T]) MarshalJSON ¶
func (*Record[T]) UnmarshalJSON ¶
type RecordList ¶
func (*RecordList[T]) IdMap ¶
func (d *RecordList[T]) IdMap() map[string]*T
IdMap returns the ids in the list(this would be used when we haven't fetched the records)
func (*RecordList[T]) Ids ¶
func (d *RecordList[T]) Ids() []string
Ids return an array of all ids in the list
func (*RecordList[T]) IsResolved ¶
func (d *RecordList[T]) IsResolved() bool
IsResolved check if the list has been resolved/initiated
func (*RecordList[T]) Items ¶
func (d *RecordList[T]) Items() []T
Items returns the items in the list
func (*RecordList[T]) MarshalJSON ¶
func (d *RecordList[T]) MarshalJSON() ([]byte, error)
func (*RecordList[T]) UnmarshalJSON ¶
func (d *RecordList[T]) UnmarshalJSON(data []byte) error
type RecordType ¶
type RecordType string
var ( RecordTypeId RecordType = "id" RecordTypeRecord RecordType = "record" )
type Thing ¶
type Thing struct {
// contains filtered or unexported fields
}
func NewThing ¶
NewThing Creates a new thing If we pass a single value, it will split it and validate. For example:
// Single thing value string thing := NewThing("table:id") // Table and id separately thing := NewThing("table", "id")
func (*Thing) AsTypeThing ¶
AsTypeThing Get the value for the database, for example `type::thing('table','id')`
func (*Thing) AsTypeThingParam ¶
AsTypeThingParam Get the value for the database, like AsTypeThing, but it will use a param for the value instead. This will automatically add the $ to the param, for example:
thing := NewThing("user", "31367126") thing.AsTypeThingParam("id") // type::thing('user', $id) db.Query(fmt.Sprintf("select * from %s", thing.AsTypeThingParam("id")), map[string]any{ "id": thing.Id(), })
Will output the query:
select * from type::thing('user', $id)