Skip to content

Commit

Permalink
SQLiREST
Browse files Browse the repository at this point in the history
  • Loading branch information
ushieru committed Nov 3, 2023
0 parents commit 8ed99b1
Show file tree
Hide file tree
Showing 13 changed files with 971 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Uziel Atlai Cocolan Flores

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# SQLiREST

SQLiREST es un servidor web que convierte su base de datos SQLite3 directamente en una API REST.
> Inspirado en PostgREST.
## Run
```bash
./sqlirest
```

## Config

### .env
```ini
DATABASE_URL="db.sqlite3" # default -> :memori:
SQL_FILE="init.sql"
PORT=8081 # default -> 8080
```

## Operadores
| REST | Operador |
| ---- | -------- |
| eq | = |
| gt | > |
| gte | >= |
| lt | < |
| lte | <= |
| neq | <> |
| like | like |

## Ejemplos

[Books](./example/README.md)
78 changes: 78 additions & 0 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# SQLiREST

### .env
```ini
SQL_FILE="init.sql"
```

### init.sql
```sql
CREATE TABLE IF NOT EXISTS "authors" (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL
);

CREATE TABLE IF NOT EXISTS "books" (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL CHECK(length(name) > 3),
author_id INTEGER NOT NULL,
FOREIGN KEY(author_id) REFERENCES authors(id)
);

INSERT INTO authors(name) VALUES
("Octavio Paz"),
("Juan Rulfo"),
("Rosario Castellanos");

INSERT INTO books(name, author_id) VALUES
("El laberinto de la soledad", 1),
("Piedra de sol", 1),
("Pedro Páramo", 2),
("El llano en llamas", 2),
("Balún Canán", 3),
("Poesía no eres tú", 3);
```

### Rest

```
GET http://localhost:8080/authors
```
> [{"id":1,"name":"Octavio Paz"},{"id":2,"name":"Juan Rulfo"},{"id":3,"name":"Rosario Castellanos"}]
```
POST http://localhost:8080/authors
{ "name": "Valeria Luiselli" }
```
> HTTP/1.1 201 Created
```
GET http://localhost:8080/authors?name=eq.Valeria%20Luiselli
```
> [{"id":4,"name":"Valeria Luiselli"}]
```
GET http://localhost:8080/books
```
> [{"author_id":1,"id":1,"name":"El laberinto de la soledad"},{"author_id":1,"id":2,"name":"Piedra de sol"},{"author_id":2,"id":3,"name":"Pedro Páramo"},{"author_id":2,"id":4,"name":"El llano en llamas"},{"author_id":3,"id":5,"name":"Balún Canán"},{"author_id":3,"id":6,"name":"Poesía no eres tú"}]
```
GET http://localhost:8080/books?select=id,name
```
> [{"id":1,"name":"El laberinto de la soledad"},{"id":2,"name":"Piedra de sol"},{"id":3,"name":"Pedro Páramo"},{"id":4,"name":"El llano en llamas"},{"id":5,"name":"Balún Canán"},{"id":6,"name":"Poesía no eres tú"}]
```
GET http://localhost:8080/books?select=name,id&id=gt.4
```
> [{"id":5,"name":"Balún Canán"},{"id":6,"name":"Poesía no eres tú"}]
```
PATCH http://localhost:8080/books?id=eq.5
{"name": "Oficio de tinieblas"}
```
> HTTP/1.1 200 OK
```
DELETE http://localhost:8080/books?id=eq.5
```
> HTTP/1.1 200 OK
49 changes: 49 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
module github.com/ushieru/sqlirest

go 1.20

require (
github.com/glebarez/go-sqlite v1.21.2
github.com/gofiber/fiber/v2 v2.50.0
github.com/huandu/go-sqlbuilder v1.22.0
github.com/spf13/viper v1.17.0
)

require (
github.com/andybalholm/brotli v1.0.5 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/google/uuid v1.3.1 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/huandu/xstrings v1.3.2 // indirect
github.com/klauspost/compress v1.17.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/sagikazarmark/locafero v0.3.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.10.0 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.50.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
modernc.org/libc v1.22.5 // indirect
modernc.org/mathutil v1.5.0 // indirect
modernc.org/memory v1.5.0 // indirect
modernc.org/sqlite v1.23.1 // indirect
)
Loading

0 comments on commit 8ed99b1

Please sign in to comment.