postgrest

package module
v0.0.10 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2024 License: Apache-2.0 Imports: 11 Imported by: 2

README

Postgrest GO

golangci-lint CodeFactor

Golang client for PostgREST. The goal of this library is to make an "ORM-like" restful interface.

Documentation

Full documentation can be found here.

Quick start

Install

go get github.com/supabase-community/postgrest-go

Usage

package main

import (
	"fmt"

	"github.com/supabase/postgrest-go"
)

func main() {
	client := postgrest.NewClient("http://localhost:3000/rest/v1", "", nil)
	if client.ClientError != nil {
		panic(client.ClientError)
	}

	result := client.Rpc("add_them", "", map[string]int{"a": 12, "b": 3})
	if client.ClientError != nil {
		panic(client.ClientError)
	}

	fmt.Println(result)
}

Testing

Some tests are implemented to run against mocked Postgrest endpoints. Optionally, tests can be run against an actual Postgrest instance by setting a POSTGREST_URL environment variable to the fully-qualified URL to a Postgrest instance, and, optionally, an API_KEY environment variable (if, for example, testing against a local Supabase instance).

A script is included in the test directory that can be used to seed the test database.

To run all tests:

go test ./...

License

This repo is licensed under the Apache License.

Sponsors

We are building the features of Firebase using enterprise-grade, open source products. We support existing communities wherever possible, and if the products don’t exist we build them and open source them ourselves. Thanks to these sponsors who are making the OSS ecosystem better for everyone.

New Sponsor

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var DefaultOrderOpts = OrderOpts{
	Ascending:    false,
	NullsFirst:   false,
	ForeignTable: "",
}

DefaultOrderOpts is the default set of options used by Order.

Functions

This section is empty.

Types

type Client

type Client struct {
	ClientError error

	Transport *transport
	// contains filtered or unexported fields
}

func NewClient

func NewClient(rawURL, schema string, headers map[string]string) *Client

NewClient constructs a new client given a URL to a Postgrest instance.

func (*Client) ChangeSchema

func (c *Client) ChangeSchema(schema string) *Client

ChangeSchema modifies the schema for subsequent requests.

func (*Client) From

func (c *Client) From(table string) *QueryBuilder

From sets the table to query from.

func (*Client) Ping

func (c *Client) Ping() bool

func (*Client) Rpc

func (c *Client) Rpc(name string, count string, rpcBody interface{}) string

Rpc executes a Postgres function (a.k.a., Remote Prodedure Call), given the function name and, optionally, a body, returning the result as a string.

func (*Client) TokenAuth

func (c *Client) TokenAuth(token string) *Client

TokenAuth sets authorization headers for subsequent requests.

type ExecuteError

type ExecuteError struct {
	Hint    string `json:"hint"`
	Details string `json:"details"`
	Code    string `json:"code"`
	Message string `json:"message"`
}

ExecuteError is the error response format from postgrest. We really only use Code and Message, but we'll keep it as a struct for now.

type FilterBuilder

type FilterBuilder struct {
	// contains filtered or unexported fields
}

FilterBuilder describes a builder for a filtered result set.

func (*FilterBuilder) And added in v0.0.10

func (f *FilterBuilder) And(filters, foreignTable string) *FilterBuilder

func (*FilterBuilder) ContainedBy

func (f *FilterBuilder) ContainedBy(column string, value []string) *FilterBuilder

func (*FilterBuilder) ContainedByObject

func (f *FilterBuilder) ContainedByObject(column string, value interface{}) *FilterBuilder

func (*FilterBuilder) Contains

func (f *FilterBuilder) Contains(column string, value []string) *FilterBuilder

func (*FilterBuilder) ContainsObject

func (f *FilterBuilder) ContainsObject(column string, value interface{}) *FilterBuilder

func (*FilterBuilder) Eq

func (f *FilterBuilder) Eq(column, value string) *FilterBuilder

func (*FilterBuilder) Execute

func (f *FilterBuilder) Execute() ([]byte, int64, error)

Execute runs the PostgREST query, returning the result as a byte slice.

func (*FilterBuilder) ExecuteString

func (f *FilterBuilder) ExecuteString() (string, int64, error)

ExecuteString runs the PostgREST query, returning the result as a JSON string.

func (*FilterBuilder) ExecuteTo

func (f *FilterBuilder) ExecuteTo(to interface{}) (countType, error)

ExecuteTo runs the PostgREST query, encoding the result to the supplied interface. Note that the argument for the to parameter should always be a reference to a slice.

Example
// Given a database with a "users" table containing "id", "name" and "email"
// columns:
var res []struct {
	ID    int64  `json:"id"`
	Name  string `json:"name"`
	Email string `json:"email"`
}

client := NewClient("http://localhost:3000", "", nil)
count, err := client.From("users").Select("*", "exact", false).ExecuteTo(&res)
if err == nil && count > 0 {
	// The value for res will contain all columns for all users, and count will
	// be the exact number of rows in the users table.
}
Output:

func (*FilterBuilder) Filter

func (f *FilterBuilder) Filter(column, operator, value string) *FilterBuilder

Filter adds a filtering operator to the query. For a list of available operators, see: https://postgrest.org/en/stable/api.html#operators

func (*FilterBuilder) Gt

func (f *FilterBuilder) Gt(column, value string) *FilterBuilder

func (*FilterBuilder) Gte

func (f *FilterBuilder) Gte(column, value string) *FilterBuilder

func (*FilterBuilder) Ilike

func (f *FilterBuilder) Ilike(column, value string) *FilterBuilder

func (*FilterBuilder) In

func (f *FilterBuilder) In(column string, values []string) *FilterBuilder

func (*FilterBuilder) Is

func (f *FilterBuilder) Is(column, value string) *FilterBuilder

func (*FilterBuilder) Like

func (f *FilterBuilder) Like(column, value string) *FilterBuilder

func (*FilterBuilder) Limit

func (f *FilterBuilder) Limit(count int, foreignTable string) *FilterBuilder

Limit the result to the specified count.

func (*FilterBuilder) Lt

func (f *FilterBuilder) Lt(column, value string) *FilterBuilder

func (*FilterBuilder) Lte

func (f *FilterBuilder) Lte(column, value string) *FilterBuilder

func (*FilterBuilder) Match

func (f *FilterBuilder) Match(userQuery map[string]string) *FilterBuilder

func (*FilterBuilder) Neq

func (f *FilterBuilder) Neq(column, value string) *FilterBuilder

func (*FilterBuilder) Not

func (f *FilterBuilder) Not(column, operator, value string) *FilterBuilder

func (*FilterBuilder) Or

func (f *FilterBuilder) Or(filters, foreignTable string) *FilterBuilder

func (*FilterBuilder) Order

func (f *FilterBuilder) Order(column string, opts *OrderOpts) *FilterBuilder

Order the result with the specified column. A pointer to an OrderOpts object can be supplied to specify ordering options.

func (*FilterBuilder) Overlaps

func (f *FilterBuilder) Overlaps(column string, value []string) *FilterBuilder

func (*FilterBuilder) Range

func (f *FilterBuilder) Range(from, to int, foreignTable string) *FilterBuilder

Range Limits the result to rows within the specified range, inclusive.

func (*FilterBuilder) RangeAdjacent

func (f *FilterBuilder) RangeAdjacent(column, value string) *FilterBuilder

func (*FilterBuilder) RangeGt

func (f *FilterBuilder) RangeGt(column, value string) *FilterBuilder

func (*FilterBuilder) RangeGte

func (f *FilterBuilder) RangeGte(column, value string) *FilterBuilder

func (*FilterBuilder) RangeLt

func (f *FilterBuilder) RangeLt(column, value string) *FilterBuilder

func (*FilterBuilder) RangeLte

func (f *FilterBuilder) RangeLte(column, value string) *FilterBuilder

func (*FilterBuilder) Single

func (f *FilterBuilder) Single() *FilterBuilder

Single Retrieves only one row from the result. The total result set must be one row (e.g., by using Limit). Otherwise, this will result in an error.

func (*FilterBuilder) TextSearch

func (f *FilterBuilder) TextSearch(column, userQuery, config, tsType string) *FilterBuilder

TextSearch performs a full-text search filter. For more information, see https://postgrest.org/en/stable/api.html#fts.

type OrderOpts

type OrderOpts struct {
	Ascending    bool
	NullsFirst   bool
	ForeignTable string
}

OrderOpts describes the options to be provided to Order.

type QueryBuilder

type QueryBuilder struct {
	// contains filtered or unexported fields
}

QueryBuilder describes a builder for a query.

func (*QueryBuilder) Delete

func (q *QueryBuilder) Delete(returning, count string) *FilterBuilder

Delete performs a deletion from the table.

func (*QueryBuilder) Execute

func (q *QueryBuilder) Execute() ([]byte, int64, error)

Execute runs the Postgrest query, returning the result as a byte slice.

func (*QueryBuilder) ExecuteString

func (q *QueryBuilder) ExecuteString() (string, int64, error)

ExecuteString runs the Postgrest query, returning the result as a JSON string.

func (*QueryBuilder) ExecuteTo

func (q *QueryBuilder) ExecuteTo(to interface{}) (int64, error)

ExecuteTo runs the Postgrest query, encoding the result to the supplied interface. Note that the argument for the to parameter should always be a reference to a slice.

func (*QueryBuilder) Insert

func (q *QueryBuilder) Insert(value interface{}, upsert bool, onConflict, returning, count string) *FilterBuilder

Insert performs an insertion into the table.

func (*QueryBuilder) Select

func (q *QueryBuilder) Select(columns, count string, head bool) *FilterBuilder

Select performs vertical filtering.

func (*QueryBuilder) Update

func (q *QueryBuilder) Update(value interface{}, returning, count string) *FilterBuilder

Update performs an update on the table.

func (*QueryBuilder) Upsert

func (q *QueryBuilder) Upsert(value interface{}, onConflict, returning, count string) *FilterBuilder

Upsert performs an upsert into the table.

Directories

Path Synopsis
test
rpc

Jump to

Keyboard shortcuts

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