Documentation
¶
Index ¶
- func GeneralChange[T any](d *DB, id string, data map[string]interface{}) (*T, error)
- func GeneralCreate[T any](d *DB, thing string, data map[string]interface{}) (*T, error)
- func GeneralDelete[T any](d *DB, id string) (*T, error)
- func GeneralSelect[T any](d *DB, s SelectQuery) ([]T, error)
- func GeneralSelectAny(d *DB, s SelectQuery) ([]map[string]any, error)
- func GeneralUpdate[T any](d *DB, id string, data map[string]interface{}) (*T, error)
- func UseFilter(f interface{}, q string) string
- type CacheKey
- type DB
- type SelectQuery
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GeneralChange ¶
GeneralChange is a function that changes an existing entry in the SurrealDB. Similar to GeneralUpdate, it takes the DB instance, the id of the object, and a map of the data to be changed. If successful, it returns a pointer to the changed object; otherwise, it returns an error. After changing, it clears related entries from the cache.
func GeneralCreate ¶
GeneralCreate is a generic function that handles the creation of a new record in the database. It takes a thing string which represents the table name, and a map of data for the record. After successfully creating the record, it clears the relevant cache. d: Pointer to DB instance thing: table name in the database data: map containing field-value pairs for the new record Returns a pointer to the created record of type T or an error.
func GeneralDelete ¶
GeneralDelete is a function that deletes an existing entry from the SurrealDB. It takes the DB instance, and the id of the object. If successful, it returns a pointer to the deleted object; otherwise, it returns an error. After deleting, it clears related entries from the cache.
func GeneralSelect ¶
func GeneralSelect[T any](d *DB, s SelectQuery) ([]T, error)
GeneralSelect is a generic function that handles querying of records from the database. It first checks if the query results are present in the cache. If not, it executes the query and stores the result in cache. d: Pointer to DB instance s: SelectQuery structure which encapsulates the SELECT query details Returns a slice of records of type T or an error.
func GeneralSelectAny ¶ added in v1.2.2
func GeneralSelectAny(d *DB, s SelectQuery) ([]map[string]any, error)
GeneralSelectAny is a function that handles querying of records from the database in a generic way, allowing for various data types in the result. Unlike GeneralSelect, which returns a slice of records of a specific type T, GeneralSelectAny returns a slice of maps with string keys and values of any type. The function first checks if the query results are already present in the cache. If not, it executes the query and stores the result in the cache.
d: Pointer to the DB instance. s: SelectQuery structure which encapsulates the SELECT query details.
Returns a slice of map[string]any records or an error.
func GeneralUpdate ¶
GeneralUpdate is a function that updates an existing entry in the SurrealDB. It takes the DB instance, the id of the object, and a map of the data to be updated. If successful, it returns a pointer to the updated object; otherwise, it returns an error. After updating, it clears related entries from the cache.
func UseFilter ¶
UseFilter takes an interface and a query string as input and adds WHERE and LIMIT clauses to the query based on the non-nil fields of the interface. It ignores the "limit" field while constructing WHERE clauses. f: Filter interface with optional fields q: Query string to which filters will be appended Returns the modified query string.
Types ¶
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
DB represents a wrapper over surrealdb.DB that includes a concurrent map for caching purposes.
func New ¶
New is a function that creates a new instance of DB. It establishes a connection to the SurrealDB with the provided URL and credentials, then switches to the specified namespace and database. If successful, it returns a pointer to the DB instance; otherwise, it returns an error.
func (*DB) GetSurrealDB ¶ added in v1.3.3
type SelectQuery ¶
SelectQuery represents a structure for generating SQL SELECT queries. TableName is the name of the table in the database. Fields is an optional slice of fields (columns) to be selected. If empty, all fields (*) are selected. Filter is an optional filter to be applied to the query as a WHERE clause.
func NewSelect ¶
func NewSelect(t string, f ...string) SelectQuery
NewSelect is a function that generates a new SelectQuery for selecting specific fields from a specific table. It accepts the table name as the first argument and a variable number of string arguments for the fields.
func NewSelectAll ¶
func NewSelectAll(t string) SelectQuery
NewSelectAll is a function that generates a new SelectQuery for selecting all fields from a specific table. It accepts the table name as an argument.
func (SelectQuery) String ¶
func (s SelectQuery) String() string
String method generates a SQL SELECT query string based on the SelectQuery values. It uses the UseFilter function to add any filter conditions to the query.
func (SelectQuery) WithFilter ¶
func (s SelectQuery) WithFilter(f any) SelectQuery
WithFilter is a method that adds a filter to the SelectQuery and returns the updated SelectQuery. It accepts an interface{} as a filter which can be any type that is acceptable by the UseFilter function.