Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
caiofsr committed Oct 5, 2024
1 parent 9c32938 commit 970e62f
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1 @@
golang 1.23.0
golang 1.23.2
15 changes: 12 additions & 3 deletions internal/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"strconv"
"time"

"mpf/internal/database/sqlc"

Check failure on line 13 in internal/database/database.go

View workflow job for this annotation

GitHub Actions / build

found packages sqlc (db.go) and database (store.go) in /home/runner/work/mpf/mpf/internal/database/sqlc

_ "github.com/jackc/pgx/v5/stdlib"
_ "github.com/joho/godotenv/autoload"
"github.com/pressly/goose/v3"
Expand All @@ -30,8 +32,14 @@ type Service interface {
RunMigrations() error
}

type ServiceQueries interface {
Service
sqlc.Querier
}

type service struct {
db *sql.DB
db *sql.DB
queries *sqlc.Queries
}

var (
Expand All @@ -46,7 +54,7 @@ var (
//go:embed migrations/*.sql
var embedMigrations embed.FS

func New() Service {
func New() ServiceQueries {
// Reuse Connection
if dbInstance != nil {
return dbInstance
Expand All @@ -57,7 +65,8 @@ func New() Service {
log.Fatal(err)
}
dbInstance = &service{
db: db,
db: db,
queries: sqlc.New(db),
}

return dbInstance
Expand Down
5 changes: 5 additions & 0 deletions internal/database/queries/users.sql
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
-- name: GetUsers :many
SELECT * FROM users;

-- name: CreateUser :one
INSERT INTO users (name, surname, email, password)
VALUES ($1, $2, $3, $4)
RETURNING *;
15 changes: 7 additions & 8 deletions internal/database/sqlc/db.go

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

23 changes: 12 additions & 11 deletions internal/database/sqlc/models.go

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

16 changes: 16 additions & 0 deletions internal/database/sqlc/querier.go

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

21 changes: 21 additions & 0 deletions internal/database/sqlc/store.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package database

import (
"database/sql"
)

type Store interface {
Querier
}

type SQLStore struct {
*Queries
db *sql.DB
}

func NewStore(db *sql.DB) Store {
return &SQLStore{
db: db,
Queries: New(db),
}
}
44 changes: 41 additions & 3 deletions internal/database/sqlc/users.sql.go

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

9 changes: 7 additions & 2 deletions sqlc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ sql:
schema: "internal/database/migrations"
gen:
go:
package: "database"
package: "sqlc"
out: "internal/database/sqlc"
sql_package: "pgx/v5"
sql_package: "database/sql"
emit_json_tags: true
emit_prepared_queries: false
emit_interface: true
emit_exact_table_names: false
emit_empty_slices: true

0 comments on commit 970e62f

Please sign in to comment.