sqlStorage

package module
v0.0.0-...-d9fca83 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2022 License: MIT Imports: 10 Imported by: 3

README

Golang REST Layer SQL Storage Handler

This REST Layer resource storage backend stores data in a SQL Database using database/sql.

Usage

import "github.com/apuigsech/rest-layer-sql"

Create a resource storage handler with a given SQL driver, source and table:

h := sqlStorage.NewHandler(DB_DRIVER, DB_SOURCE, DB_TABLE)

Bind this resource storage handled to a resource:

index.Bind("resourceName", resourceSchema, h, resource.DefaultConf)

Supported SQL Drivers

All supported SQL Drivers are listed here

Examples

Run the example:

$ go run example/example.go
2018/12/07 18:29:53 Serving API on http://localhost:8080

You can perform requests using HTTPie.

$ http GET :8080/units
HTTP/1.1 200 OK
Content-Length: 2
Content-Type: application/json
Date: Fri, 07 Dec 2018 17:31:23 GMT
Etag: W/"d41d8cd98f00b204e9800998ecf8427e"
X-Total: 0

[]

$ http POST :8080/units str="foo" int:=0
HTTP/1.1 201 Created
Content-Length: 139
Content-Location: /units/bg5at5bmvban389193a0
Content-Type: application/json
Date: Fri, 07 Dec 2018 17:32:05 GMT
Etag: W/"80b0d96e674f761e87950ccb19bdc279"
Last-Modified: Fri, 07 Dec 2018 17:32:05 GMT

{
    "created": "2018-12-07T18:32:05.290925+01:00",
    "id": "bg5at5bmvban389193a0",
    "int": 0,
    "str": "foo",
    "updated": "2018-12-07T18:32:05.290925+01:00"
}

$ http POST :8080/units str="bar" int:=1
HTTP/1.1 201 Created
Content-Length: 137
Content-Location: /units/bg5at73mvban389193ag
Content-Type: application/json
Date: Fri, 07 Dec 2018 17:32:12 GMT
Etag: W/"8bb72222f5979bc9f9259c0262807667"
Last-Modified: Fri, 07 Dec 2018 17:32:12 GMT

{
    "created": "2018-12-07T18:32:12.08373+01:00",
    "id": "bg5at73mvban389193ag",
    "int": 1,
    "str": "bar",
    "updated": "2018-12-07T18:32:12.08373+01:00"
}

$ http GET :8080/units
HTTP/1.1 200 OK
Content-Length: 365
Content-Type: application/json
Date: Fri, 07 Dec 2018 17:32:44 GMT
Etag: W/"0a320d03fee8e1fb268d3cc48fac391d"
X-Total: 0

[
    {
        "_etag": "80b0d96e674f761e87950ccb19bdc279",
        "created": "2018-12-07T18:32:05.290925+01:00",
        "id": "bg5at5bmvban389193a0",
        "int": 0,
        "str": "foo",
        "updated": "2018-12-07T18:32:05.290925+01:00"
    },
    {
        "_etag": "8bb72222f5979bc9f9259c0262807667",
        "created": "2018-12-07T18:32:12.08373+01:00",
        "id": "bg5at73mvban389193ag",
        "int": 1,
        "str": "bar",
        "updated": "2018-12-07T18:32:12.08373+01:00"
    }
]

Documentation

Index

Constants

View Source
const (
	NONE = iota
	ERR
	WARN
	DEBUG
)

Variables

This section is empty.

Functions

This section is empty.

Types

type AutoIncrementingInteger

type AutoIncrementingInteger int

type Config

type Config struct {
	VerboseLevel   int
	QueryTemplates map[string]string
}

type SQLHandler

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

func NewHandler

func NewHandler(driverName string, dataSourceName string, tableName string, config *Config) (h *SQLHandler, err error)

func NewHandlerWithDB

func NewHandlerWithDB(driverName string, db *sql.DB, tableName string, config *Config) *SQLHandler

func (*SQLHandler) ApplyQueryTemplate

func (h *SQLHandler) ApplyQueryTemplate(queryTemplateName string, sqlQuery string) string

func (*SQLHandler) Clear

func (h *SQLHandler) Clear(ctx context.Context, q *query.Query) (total int, err error)

func (*SQLHandler) Create

func (h *SQLHandler) Create(ctx context.Context, s *schema.Schema) (err error)

func (*SQLHandler) Delete

func (h *SQLHandler) Delete(ctx context.Context, item *resource.Item) (err error)

func (*SQLHandler) ExecContext

func (h *SQLHandler) ExecContext(ctx context.Context, sqlQuery string, sqlParams ...interface{}) (sql.Result, error)

func (*SQLHandler) Find

func (h *SQLHandler) Find(ctx context.Context, q *query.Query) (list *resource.ItemList, err error)

func (*SQLHandler) Insert

func (h *SQLHandler) Insert(ctx context.Context, items []*resource.Item) (err error)

func (*SQLHandler) QueryContext

func (h *SQLHandler) QueryContext(ctx context.Context, sqlQuery string, sqlParams ...interface{}) (*sql.Rows, error)

func (*SQLHandler) Update

func (h *SQLHandler) Update(ctx context.Context, item *resource.Item, original *resource.Item) (err error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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