Skip to content

Commit

Permalink
feat(database): kick sqlboiler, cause too much boilerplate
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkness4 committed Nov 6, 2023
1 parent cd43563 commit ea6bcac
Show file tree
Hide file tree
Showing 16 changed files with 126 additions and 1,151 deletions.
21 changes: 10 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ ifeq ($(golint),)
golint := $(shell go env GOPATH)/bin/golangci-lint
endif

sqlboiler := $(shell which sqlboiler)
ifeq ($(sqlboiler),)
sqlboiler := $(shell go env GOPATH)/bin/sqlboiler
endif

migrate := $(shell which migrate)
ifeq ($(migrate),)
migrate := $(shell go env GOPATH)/bin/migrate
endif

sqlc := $(shell which sqlc)
ifeq ($(sqlc),)
sqlc := $(shell go env GOPATH)/bin/sqlc
endif

.PHONY: bin/auth-htmx
bin/auth-htmx: $(GO_SRCS)
go build -ldflags "-s -w -X main.version=${VERSION}" -o "$@" ./main.go
Expand All @@ -48,8 +48,8 @@ clean:
rm -rf bin/

.PHONY: sql
sql: $(sqlboiler)
$(sqlboiler) sqlite3
sql: $(sqlc)
$(sqlc) generate

.PHONY: migration
migration: $(migrate)
Expand All @@ -66,16 +66,15 @@ drop: $(migrate)
$(migrate):
go install -tags 'sqlite3' github.com/golang-migrate/migrate/v4/cmd/migrate

$(sqlboiler):
go install github.com/volatiletech/sqlboiler/v4
go install github.com/volatiletech/sqlboiler/v4/drivers/sqlboiler-sqlite3

$(gow):
go install github.com/mitranim/gow@latest

$(golint):
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest

$(sqlc):
go install github.com/sqlc-dev/sqlc/cmd/sqlc

.PHONY: version
version:
@echo VERSION_CORE=${VERSION_CORE}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ A very simple example HTMX with OAuth2 with:
- HTMX solution for SSR.
- OAuth2 with Github Auth and OIDC.
- (+CSRF protection measures for OAuth2 and all requests).
- SQLite3 with SQLBoiler and golang-migrate.
- SQLite3 with sqlc and golang-migrate.

## Motivation

Expand Down
32 changes: 11 additions & 21 deletions database/counter/counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import (
"database/sql"
"errors"

"github.com/Darkness4/auth-htmx/database/models"
"github.com/volatiletech/sqlboiler/v4/boil"
"github.com/Darkness4/auth-htmx/database"
)

type Repository interface {
Expand All @@ -15,37 +14,28 @@ type Repository interface {
}

func NewRepository(db *sql.DB) Repository {
return &repository{db}
return &repository{
Queries: database.New(db),
}
}

type repository struct {
*sql.DB
*database.Queries
}

func (r *repository) Inc(ctx context.Context, userID string) (new int64, err error) {
counter, err := models.Counters(models.CounterWhere.UserID.EQ(userID)).One(ctx, r.DB)
new, err = r.Queries.IncrementCounter(ctx, userID)
if err != nil && !errors.Is(err, sql.ErrNoRows) {
return 0, err
return new, err
}
if counter == nil {
counter = &models.Counter{
UserID: userID,
Count: 0,
}
if errors.Is(err, sql.ErrNoRows) {
return 1, r.Queries.CreateCounter(ctx, userID)
}
counter.Count++
return counter.Count, counter.Upsert(
ctx,
r.DB,
true,
[]string{models.CounterColumns.UserID},
boil.Whitelist(models.CounterColumns.Count),
boil.Infer(),
)
return new, err
}

func (r *repository) Get(ctx context.Context, userID string) (int64, error) {
counter, err := models.Counters(models.CounterWhere.UserID.EQ(userID)).One(ctx, r.DB)
counter, err := r.Queries.GetCounter(ctx, userID)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return 0, nil
Expand Down
31 changes: 31 additions & 0 deletions database/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 database/models.go

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

38 changes: 0 additions & 38 deletions database/models/boil_queries.go

This file was deleted.

10 changes: 0 additions & 10 deletions database/models/boil_table_names.go

This file was deleted.

52 changes: 0 additions & 52 deletions database/models/boil_types.go

This file was deleted.

7 changes: 0 additions & 7 deletions database/models/boil_view_names.go

This file was deleted.

Loading

0 comments on commit ea6bcac

Please sign in to comment.