https://github.com/apuigsech/rest-layer-sql
Provide SQL Database using database/sql storage backend for rest-layer.
https://github.com/apuigsech/rest-layer-sql
golang rest-api sql
Last synced: about 2 months ago
JSON representation
Provide SQL Database using database/sql storage backend for rest-layer.
- Host: GitHub
- URL: https://github.com/apuigsech/rest-layer-sql
- Owner: apuigsech
- License: mit
- Created: 2018-12-07T17:54:23.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2022-08-17T23:45:32.000Z (over 3 years ago)
- Last Synced: 2024-06-19T00:26:03.479Z (almost 2 years ago)
- Topics: golang, rest-api, sql
- Language: Go
- Homepage:
- Size: 23.4 KB
- Stars: 12
- Watchers: 2
- Forks: 9
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Golang REST Layer SQL Storage Handler
This [REST Layer](https://github.com/rs/rest-layer) resource storage backend stores data in a SQL Database using [database/sql](https://golang.org/pkg/database/sql/).
## Usage
```go
import "github.com/apuigsech/rest-layer-sql"
```
Create a resource storage handler with a given SQL driver, source and table:
```go
h := sqlStorage.NewHandler(DB_DRIVER, DB_SOURCE, DB_TABLE)
```
Bind this resource storage handled to a resource:
```go
index.Bind("resourceName", resourceSchema, h, resource.DefaultConf)
```
## Supported SQL Drivers
All supported SQL Drivers are listed [here](https://github.com/golang/go/wiki/SQLDrivers)
## Examples
Run the example:
```bash
$ go run example/example.go
2018/12/07 18:29:53 Serving API on http://localhost:8080
```
You can perform requests using [HTTPie](https://httpie.org/).
```bash
$ 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"
}
]
```