rest

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package rest provides a PostgreSQL REST API server similar to PostgREST.

The server automatically exposes database tables and views as REST endpoints. Each endpoint supports standard HTTP methods: GET, POST, PATCH, DELETE.

Tables and views are exposed at /table_or_view_name or `/schema_name/table_or_view_name` path. If schema_name is skipped `schema_name=public` is used

Query parameters control filtering, pagination, and ordering:

Parameter           | Description
--------------------|------------------------------------------------
?select=col1,col2   | Select specific columns
?order=col.desc     | Order results (supports nullsfirst/nullslast)
?order=similarity(col, 'search string') | Order by similarity (requires pg_trgm extension)
?limit=100           | Limit number of results (default: 100)
?offset=0            | Pagination offset (default: 0)
?col=eq.val          | Filter by column equality
?col=gt.val          | Filter with greater than comparison
?col=lt.val          | Filter with less than comparison
?col=gte.val         | Filter with greater than or equal comparison
?col=lte.val         | Filter with less than or equal comparison
?col=like.val        | Filter with pattern matching
?col=in.a,b,c        | Filter with value lists
?col=is.null         | Filter for null values
?or=(a.eq.x,b.lt.y)  | Combine filters with logical operators
?col=cs.example,new  | contains        (@>)	The column contains all these values. JSON object can be passed with urlencoded string
?col=cd.1,2,3.       | is contained in (<@)	The column’s values are all within this set

HTTP headers control response format for POST/PATCH/DELETE operations:

Header                         | Description
-------------------------------|----------------------------------------
Prefer: return=minimal         | Return status code only (default)
Prefer: return=representation  | Return modified rows in response body
Prefer: count=exact            | Add exact total count of rows for the query in Content-Range header
Prefer: return=headers-only    | Return headers with metadata only

API is compatible with PostgREST. For more details, see: https://docs.postgrest.org/en/stable/references/api/tables_views.html

Example usage:

server, err := rest.NewServer("postgres://user:pass@localhost/db", "")
if err != nil {
	log.Fatal(err)
}
defer server.Shutdown()
log.Fatal(server.Start(":8080"))

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FilterParam

type FilterParam struct {
	Operator string
	Value    any
}

type Function added in v0.0.3

type Function struct {
	Schema      string
	Name        string
	Parameters  []FunctionParam
	ReturnType  string
	IsSetof     bool // Returns a set of records
	Language    string
	IsAggregate bool
}

Function represents a PostgreSQL function

type FunctionParam added in v0.0.3

type FunctionParam struct {
	Name         string
	DataType     string
	DefaultValue *string
	Mode         string // IN, OUT, INOUT, VARIADIC
	Position     int
}

FunctionParam represents a PostgreSQL function parameter

type Headers

type Headers struct {
	Prefer *Prefer
}

Headers holds all parsed HTTP headers relevant to REST actions.

type JoinParam

type JoinParam struct {
	Table    string
	JoinType string
	On       string
}

type OrderParam

type OrderParam struct {
	Column        string
	Direction     string // asc or desc
	NullsPosition string // first or last
	Similarity    string // https://www.postgresql.org/docs/current/pgtrgm.html
}

type Prefer

type Prefer struct {
	Return string // "minimal", "representation", "headers-only"
	Count  string // "exact", "planned", "estimated"
}

Prefer holds preferences from the Prefer header (RFC 7240).

func (*Prefer) WantsCountEstimated

func (p *Prefer) WantsCountEstimated() bool

WantsCountEstimated reports whether the client wants an estimated count in the response.

func (*Prefer) WantsCountExact

func (p *Prefer) WantsCountExact() bool

WantsCountExact reports whether the client wants an exact count in the response.

func (*Prefer) WantsCountPlanned

func (p *Prefer) WantsCountPlanned() bool

WantsCountPlanned reports whether the client wants a planned count in the response.

func (*Prefer) WantsHeadersOnly

func (p *Prefer) WantsHeadersOnly() bool

WantsHeadersOnly reports whether the client prefers only headers with no response body for mutation operations.

func (*Prefer) WantsRepresentation

func (p *Prefer) WantsRepresentation() bool

WantsRepresentation reports whether the client prefers full representation in the response body for mutation operations.

type QueryParams

type QueryParams struct {
	Select     []string                 // Columns to select
	Order      []OrderParam             // Order by columns
	Limit      int                      // Limit results
	Offset     int                      // Offset results
	Filters    map[string][]FilterParam // Column filters
	EmbedJoins []JoinParam              // Embedded resource joins (foreign keys)
}

QueryParams holds parsed query parameters in a structured way

type Server

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

func NewServer

func NewServer(connString, basePath string, omitempty ...bool) (*Server, error)

func (*Server) AddMiddleware

func (s *Server) AddMiddleware(middleware ...httputil.Middleware)

func (*Server) Shutdown

func (s *Server) Shutdown(ctx context.Context) error

func (*Server) Start

func (s *Server) Start(addr string) error

Jump to

Keyboard shortcuts

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