Skip to content

Commit

Permalink
Merge pull request #62 from calvinmclean/example/sqlc
Browse files Browse the repository at this point in the history
Add example using sqlc + Turso
  • Loading branch information
calvinmclean committed Apr 28, 2024
2 parents 01d6ab1 + a69dde0 commit 35b2226
Show file tree
Hide file tree
Showing 12 changed files with 516 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,5 @@ jobs:
run: |
for dir in examples/*/; do
echo "Building $dir"
go build $dir/*.go
cd $dir && go build && cd -
done
2 changes: 2 additions & 0 deletions examples/sql/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
local.db
babyapi-sql
58 changes: 58 additions & 0 deletions examples/sql/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# SQL Example

This example starts with the [`sqlc` tutorial](https://docs.sqlc.dev/en/latest/tutorials/getting-started-sqlite.html) and adds `babyapi` functionality on top of it for the HTTP layer. It provides a custom implementation of the `babyapi.Storage` interface to wrap the queries generated by [`sqlc`](https://sqlc.dev). It also implements connectivity to [Turso](https://turso.tech) cloud SQLite.

In addition to the storage backend, this example demonstrates how to extend the built-in CLI to add custom arguments and custom setup for only initializing DB for the server and not the client.

## How To

First, build the project
```shell
git clone https://github.com/calvinmclean/babyapi.git
cd babyapi/examples/sql
go build -o babyapi-sql .
```

Then, run the server:

```shell
./babyapi-sql serve
```

And use the provided CLI client to create resources:

```shell
# Create
./babyapi-sql client authors post --data '{
"name": "J.R.R. Tolkien",
"bio": "English writer and philologist",
"genre": "Fantasy"
}'

# List
./babyapi-sql client authors list

# Search
./babyapi-sql client authors list -q "genre=Fantasy"
```

### Storage Options

By default, this will use an in-memory SQLite database. You can also use a local file storage or cloud-based Turso DB.

```shell
./babyapi-sql serve --db memory
./babyapi-sql serve --db file

# Optionally set filename using environement variable
SQLITE_FILE=my_file.db ./babyapi-sql serve --db file
```

If you want to use Turso, follow step one of [their tutorial](https://docs.turso.tech/sdk/go/quickstart) to create and use a new cloud database. Then, run it using the database URL and auth as env vars:

```shell
TURSO_DATABASE_URL=
TURSO_AUTH_TOKEN=

./babyapi-sql serve --db turso
```
31 changes: 31 additions & 0 deletions examples/sql/db/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions examples/sql/db/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

131 changes: 131 additions & 0 deletions examples/sql/db/query.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions examples/sql/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module github.com/calvinmclean/babyapi/examples/sql

go 1.22.1

replace github.com/calvinmclean/babyapi => ../../

require (
github.com/calvinmclean/babyapi v0.11.0
github.com/rs/xid v1.5.0
github.com/spf13/cobra v1.8.0
github.com/tursodatabase/go-libsql v0.0.0-20240418131656-29cbe90b12a1
)

require (
github.com/ajg/form v1.5.1 // indirect
github.com/antlr4-go/antlr/v4 v4.13.0 // indirect
github.com/go-chi/chi/v5 v5.0.10 // indirect
github.com/go-chi/render v1.0.3 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/libsql/sqlite-antlr4-parser v0.0.0-20240327125255-dbf53b6cbf06 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8 // indirect
)
41 changes: 41 additions & 0 deletions examples/sql/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
github.com/ajg/form v1.5.1 h1:t9c7v8JUKu/XxOGBU0yjNpaMloxGEJhUkqFRq0ibGeU=
github.com/ajg/form v1.5.1/go.mod h1:uL1WgH+h2mgNtvBq0339dVnzXdBETtL2LeUXaIv25UY=
github.com/antlr4-go/antlr/v4 v4.13.0 h1:lxCg3LAv+EUK6t1i0y1V6/SLeUi0eKEKdhQAlS8TVTI=
github.com/antlr4-go/antlr/v4 v4.13.0/go.mod h1:pfChB/xh/Unjila75QW7+VU4TSnWnnk9UTnmpPaOR2g=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-chi/chi/v5 v5.0.10 h1:rLz5avzKpjqxrYwXNfmjkrYYXOyLJd37pz53UFHC6vk=
github.com/go-chi/chi/v5 v5.0.10/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/go-chi/render v1.0.3 h1:AsXqd2a1/INaIfUSKq3G5uA8weYx20FOsM7uSoCyyt4=
github.com/go-chi/render v1.0.3/go.mod h1:/gr3hVkmYR0YlEy3LxCuVRFzEu9Ruok+gFqbIofjao0=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/libsql/sqlite-antlr4-parser v0.0.0-20240327125255-dbf53b6cbf06 h1:JLvn7D+wXjH9g4Jsjo+VqmzTUpl/LX7vfr6VOfSWTdM=
github.com/libsql/sqlite-antlr4-parser v0.0.0-20240327125255-dbf53b6cbf06/go.mod h1:FUkZ5OHjlGPjnM2UyGJz9TypXQFgYqw6AFNO1UiROTM=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rs/xid v1.5.0 h1:mKX4bl4iPYJtEIxp6CYiUuLQ/8DYMoz0PUdtGgMFRVc=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/tursodatabase/go-libsql v0.0.0-20240418131656-29cbe90b12a1 h1:Hi55olDHy1FmAgL6B5Ca/wFFZBbfEmF5tlgykPuZP20=
github.com/tursodatabase/go-libsql v0.0.0-20240418131656-29cbe90b12a1/go.mod h1:TjsB2miB8RW2Sse8sdxzVTdeGlx74GloD5zJYUC38d8=
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8 h1:aAcj0Da7eBAtrTp03QXWvm88pSyOt+UgdZw2BFZ+lEw=
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8/go.mod h1:CQ1k9gNrJ50XIzaKCRR2hssIjF07kZFEiieALBM/ARQ=
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=

0 comments on commit 35b2226

Please sign in to comment.