surrealdb_go_utils

package module
v0.0.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 4, 2022 License: MIT Imports: 6 Imported by: 0

README

SurrealDB Go Utils

Go Reference

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

Constants

This section is empty.

Variables

View Source
var ErrInvalidThing = fmt.Errorf("invalid thing")

Functions

This section is empty.

Types

type ListType

type ListType string
var (
	ListTypeIds     ListType = "ids"
	ListTypeObjects ListType = "objects"
)

type Record

type Record[T any] struct {
	// contains filtered or unexported fields
}

func NewRecord added in v0.0.5

func NewRecord[T any](value any) *Record[T]

NewRecord creates a new Record[T] instance. Either the id or the value must be set.

func (*Record[T]) Id

func (record *Record[T]) Id() string

Id returns the id of the record(used when we haven't fetched the record yet)

func (*Record[T]) IsId

func (record *Record[T]) IsId() bool

IsId returns true if the record is an id

func (*Record[T]) IsRecord

func (record *Record[T]) IsRecord() bool

IsRecord returns true if we're using record rather than id

func (*Record[T]) MarshalJSON

func (record *Record[T]) MarshalJSON() ([]byte, error)

func (*Record[T]) Record

func (record *Record[T]) Record() T

Record returns the record

func (*Record[T]) UnmarshalJSON

func (record *Record[T]) UnmarshalJSON(data []byte) error

type RecordList

type RecordList[T any | string] struct {
	// contains filtered or unexported fields
}

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

func NewThing(values ...string) *Thing

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

func (thing *Thing) AsTypeThing() string

AsTypeThing Get the value for the database, for example `type::thing('table','id')`

func (*Thing) AsTypeThingParam

func (thing *Thing) AsTypeThingParam(paramName string) string

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)

func (*Thing) Id

func (thing *Thing) Id() string

Id Get the id value

func (*Thing) MarshalJSON

func (thing *Thing) MarshalJSON() ([]byte, error)

func (*Thing) Table

func (thing *Thing) Table() string

Table Get the table value

func (*Thing) UnmarshalJSON

func (thing *Thing) UnmarshalJSON(data []byte) error

func (*Thing) Value

func (thing *Thing) Value() string

Value Get the full value, for example `table:id` or `user:one`

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL