postgres

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2023 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ThingTable = postgres.Generate(postgres.Table[gorestapi.Thing]{
		Table: `"thing"`,
		Fields: []*postgres.Field[gorestapi.Thing]{
			{Name: "id", ID: true, Insert: "$#", Value: func(rec *gorestapi.Thing) (driver.Value, error) { return rec.ID, nil }},
			{Name: "created", Insert: "NOW()", NullVal: "0001-01-01 00:00:00 UTC"},
			{Name: "updated", Insert: "NOW()", Update: "NOW()", NullVal: "0001-01-01 00:00:00 UTC"},
			{Name: "name", Insert: "$#", Update: "$#", Value: func(rec *gorestapi.Thing) (driver.Value, error) { return rec.Name, nil }},
			{Name: "description", Insert: "$#", Update: "$#", Value: func(rec *gorestapi.Thing) (driver.Value, error) { return rec.Description, nil }},
		},
		Selector: postgres.Selector[gorestapi.Thing]{
			FilterFieldTypes: queryp.FilterFieldTypes{
				"thing.id":          queryp.FilterTypeSimple,
				"thing.created":     queryp.FilterTypeTime,
				"thing.updated":     queryp.FilterTypeTime,
				"thing.name":        queryp.FilterTypeString,
				"thing.description": queryp.FilterTypeString,
			},
			SortFields: queryp.SortFields{
				"thing.id":          "",
				"thing.created":     "",
				"thing.updated":     "",
				"thing.name":        "",
				"thing.description": "",
			},
			DefaultSort: queryp.Sort{
				&queryp.SortTerm{Field: "thing.name", Desc: false},
			},
		},
	})
)
View Source
var (
	WidgetTable = postgres.Generate(postgres.Table[gorestapi.Widget]{
		Table: `"widget"`,
		Fields: []*postgres.Field[gorestapi.Widget]{
			{Name: "id", ID: true, Insert: "$#", Value: func(rec *gorestapi.Widget) (driver.Value, error) { return rec.ID, nil }},
			{Name: "created", Insert: "NOW()", NullVal: "0001-01-01 00:00:00 UTC"},
			{Name: "updated", Insert: "NOW()", Update: "NOW()", NullVal: "0001-01-01 00:00:00 UTC"},
			{Name: "name", Insert: "$#", Update: "$#", Value: func(rec *gorestapi.Widget) (driver.Value, error) { return rec.Name, nil }},
			{Name: "description", Insert: "$#", Update: "$#", Value: func(rec *gorestapi.Widget) (driver.Value, error) { return rec.Description, nil }},
			{Name: "thing_id", Insert: "$#", Update: "$#", Value: func(rec *gorestapi.Widget) (driver.Value, error) { return rec.ThingID, nil }},
		},
		Joins: `
		LEFT JOIN thing ON widget.thing_id = thing.id
		`,
		Selector: postgres.Selector[gorestapi.Widget]{
			FilterFieldTypes: queryp.FilterFieldTypes{
				"widget.id":          queryp.FilterTypeSimple,
				"widget.created":     queryp.FilterTypeTime,
				"widget.updated":     queryp.FilterTypeTime,
				"widget.name":        queryp.FilterTypeString,
				"widget.description": queryp.FilterTypeString,
				"thing.name":         queryp.FilterTypeString,
				"thing.description":  queryp.FilterTypeString,
			},
			SortFields: queryp.SortFields{
				"widget.id":          "",
				"widget.created":     "",
				"widget.updated":     "",
				"widget.name":        "",
				"widget.description": "",
				"thing.name":         "",
				"thing.description":  "",
			},
			DefaultSort: queryp.Sort{
				&queryp.SortTerm{Field: "thing.name", Desc: false},
			},
			PostProcessRecord: func(rec *gorestapi.Widget) error {
				if rec.ThingID == nil || *rec.ThingID == "" {
					rec.Thing = nil
				}
				return nil
			},
		},
		PostProcessRecord: func(rec *gorestapi.Widget) error {
			if rec.ThingID == nil || *rec.ThingID == "" {
				rec.Thing = nil
			}
			return nil
		},
		SelectAdditionalFields: ThingTable.GenerateAdditionalFields(true),
	})
)

Functions

This section is empty.

Types

type Client

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

func New

func New(cfg *Config) (*Client, error)

New returns a new database client

func (*Client) ThingDeleteByID

func (c *Client) ThingDeleteByID(ctx context.Context, id string) error

ThingDeleteByID deletes a record by id

func (*Client) ThingGetByID

func (c *Client) ThingGetByID(ctx context.Context, id string) (*gorestapi.Thing, error)

ThingGetByID returns the the record by id

func (*Client) ThingSave

func (c *Client) ThingSave(ctx context.Context, record *gorestapi.Thing) error

ThingSave saves the record

func (*Client) ThingsFind

func (c *Client) ThingsFind(ctx context.Context, qp *queryp.QueryParameters) ([]*gorestapi.Thing, *int64, error)

ThingsFind fetches records with filter and pagination

func (*Client) WidgetDeleteByID

func (c *Client) WidgetDeleteByID(ctx context.Context, id string) error

WidgetDeleteByID deletes a record by id

func (*Client) WidgetGetByID

func (c *Client) WidgetGetByID(ctx context.Context, id string) (*gorestapi.Widget, error)

WidgetGetByID returns the the record by id

func (*Client) WidgetSave

func (c *Client) WidgetSave(ctx context.Context, record *gorestapi.Widget) error

WidgetSave saves the record

func (*Client) WidgetsFind

func (c *Client) WidgetsFind(ctx context.Context, qp *queryp.QueryParameters) ([]*gorestapi.Widget, *int64, error)

WidgetsFind fetches records with filter and pagination

type Config

type Config struct {
	postgres.Config `conf:",squash"`
}

Jump to

Keyboard shortcuts

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