feat: move db gen out, emit json field
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"github.com/jackc/pgx/v5"
|
||||
|
||||
"tutorial.sqlc.dev/app/internal/author"
|
||||
"tutorial.sqlc.dev/app/internal/db"
|
||||
)
|
||||
|
||||
func run() error {
|
||||
@@ -19,8 +20,9 @@ func run() error {
|
||||
}
|
||||
defer conn.Close(ctx)
|
||||
|
||||
querier := db.New(conn)
|
||||
r := gin.Default()
|
||||
authorCtl := author.NewController(conn)
|
||||
authorCtl := author.NewController(querier)
|
||||
author.Register(r, &authorCtl)
|
||||
|
||||
r.Run(":8000")
|
||||
|
||||
@@ -4,26 +4,26 @@ import (
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"tutorial.sqlc.dev/app/internal/author/gen"
|
||||
"tutorial.sqlc.dev/app/internal/db"
|
||||
)
|
||||
|
||||
type Controller struct {
|
||||
queries gen.Queries
|
||||
q db.Querier
|
||||
}
|
||||
|
||||
func NewController(conn gen.DBTX) Controller {
|
||||
func NewController(queries db.Querier) Controller {
|
||||
return Controller{
|
||||
queries: *gen.New(conn),
|
||||
q: queries,
|
||||
}
|
||||
}
|
||||
|
||||
func (u *Controller) ListAuthors(c *gin.Context) {
|
||||
authors, err := u.queries.ListAuthors(c.Request.Context())
|
||||
authors, err := u.q.ListAuthors(c.Request.Context())
|
||||
if err != nil {
|
||||
c.AbortWithError(500, err)
|
||||
return
|
||||
}
|
||||
c.JSON(200, toAuthorSlice(authors))
|
||||
c.JSON(200, authors)
|
||||
}
|
||||
|
||||
func (u *Controller) GetAuthorByID(c *gin.Context) {
|
||||
@@ -33,11 +33,11 @@ func (u *Controller) GetAuthorByID(c *gin.Context) {
|
||||
c.AbortWithError(400, err)
|
||||
return
|
||||
}
|
||||
author, err := u.queries.GetAuthor(c.Request.Context(), int64(id))
|
||||
author, err := u.q.GetAuthor(c.Request.Context(), int64(id))
|
||||
if err != nil {
|
||||
c.JSON(400, gin.H{"message": "Author not found"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, toAuthor(author))
|
||||
c.JSON(200, author)
|
||||
}
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.29.0
|
||||
|
||||
package gen
|
||||
|
||||
import (
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
type Author struct {
|
||||
ID int64
|
||||
Name string
|
||||
Bio pgtype.Text
|
||||
}
|
||||
@@ -1,10 +1,12 @@
|
||||
package author
|
||||
|
||||
import "tutorial.sqlc.dev/app/internal/author/gen"
|
||||
import (
|
||||
"tutorial.sqlc.dev/app/internal/db"
|
||||
)
|
||||
|
||||
func toAuthor(a gen.Author) Author {
|
||||
func toAuthor(a db.Author) Author {
|
||||
return Author{
|
||||
a.ID, a.Name, a.Bio.String,
|
||||
a.ID, a.Name, a.Bio,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,5 +3,5 @@ package author
|
||||
type Author struct {
|
||||
ID int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Bio string `json:"bio"`
|
||||
Bio *string `json:"bio"`
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// versions:
|
||||
// sqlc v1.29.0
|
||||
|
||||
package gen
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
11
internal/db/models.go
Normal file
11
internal/db/models.go
Normal file
@@ -0,0 +1,11 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.29.0
|
||||
|
||||
package db
|
||||
|
||||
type Author struct {
|
||||
ID int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Bio *string `json:"bio"`
|
||||
}
|
||||
19
internal/db/querier.go
Normal file
19
internal/db/querier.go
Normal file
@@ -0,0 +1,19 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.29.0
|
||||
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
type Querier interface {
|
||||
CreateAuthor(ctx context.Context, arg CreateAuthorParams) (Author, error)
|
||||
DeleteAuthor(ctx context.Context, id int64) error
|
||||
GetAuthor(ctx context.Context, id int64) (Author, error)
|
||||
ListAuthors(ctx context.Context) ([]Author, error)
|
||||
UpdateAuthor(ctx context.Context, arg UpdateAuthorParams) error
|
||||
}
|
||||
|
||||
var _ Querier = (*Queries)(nil)
|
||||
@@ -3,12 +3,10 @@
|
||||
// sqlc v1.29.0
|
||||
// source: query.sql
|
||||
|
||||
package gen
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
const createAuthor = `-- name: CreateAuthor :one
|
||||
@@ -21,8 +19,8 @@ RETURNING id, name, bio
|
||||
`
|
||||
|
||||
type CreateAuthorParams struct {
|
||||
Name string
|
||||
Bio pgtype.Text
|
||||
Name string `json:"name"`
|
||||
Bio *string `json:"bio"`
|
||||
}
|
||||
|
||||
func (q *Queries) CreateAuthor(ctx context.Context, arg CreateAuthorParams) (Author, error) {
|
||||
@@ -88,9 +86,9 @@ RETURNING id, name, bio
|
||||
`
|
||||
|
||||
type UpdateAuthorParams struct {
|
||||
ID int64
|
||||
Name string
|
||||
Bio pgtype.Text
|
||||
ID int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Bio *string `json:"bio"`
|
||||
}
|
||||
|
||||
func (q *Queries) UpdateAuthor(ctx context.Context, arg UpdateAuthorParams) error {
|
||||
11
sqlc.yaml
11
sqlc.yaml
@@ -1,10 +1,13 @@
|
||||
version: "2"
|
||||
sql:
|
||||
- engine: "postgresql"
|
||||
queries: "internal/author/query.sql"
|
||||
schema: "internal/author/schema.sql"
|
||||
queries: "db/query.sql"
|
||||
schema: "db/schema.sql"
|
||||
gen:
|
||||
go:
|
||||
package: "gen"
|
||||
out: "internal/author/gen"
|
||||
package: "db"
|
||||
out: "internal/db"
|
||||
sql_package: "pgx/v5"
|
||||
emit_json_tags: true
|
||||
emit_interface: true
|
||||
emit_pointers_for_null_types: true
|
||||
|
||||
@@ -1,32 +0,0 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.29.0
|
||||
|
||||
package tutorial
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
)
|
||||
|
||||
type DBTX interface {
|
||||
Exec(context.Context, string, ...interface{}) (pgconn.CommandTag, error)
|
||||
Query(context.Context, string, ...interface{}) (pgx.Rows, error)
|
||||
QueryRow(context.Context, string, ...interface{}) pgx.Row
|
||||
}
|
||||
|
||||
func New(db DBTX) *Queries {
|
||||
return &Queries{db: db}
|
||||
}
|
||||
|
||||
type Queries struct {
|
||||
db DBTX
|
||||
}
|
||||
|
||||
func (q *Queries) WithTx(tx pgx.Tx) *Queries {
|
||||
return &Queries{
|
||||
db: tx,
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.29.0
|
||||
|
||||
package tutorial
|
||||
|
||||
import (
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
type Author struct {
|
||||
ID int64
|
||||
Name string
|
||||
Bio pgtype.Text
|
||||
}
|
||||
Reference in New Issue
Block a user