feat: Integrate db infra

This commit is contained in:
Muyao CHEN
2024-10-05 23:51:11 +02:00
parent 1d753783ce
commit c00cbf35f1
10 changed files with 168 additions and 79 deletions

View File

@ -50,14 +50,15 @@ func defaultConfig() {
// db
viper.SetDefault("db.host", "localhost")
viper.SetDefault("db.port", 5432)
viper.SetDefault("db.username", "howmuch")
viper.SetDefault("db.password", "howmuch")
viper.SetDefault("db.username", "postgres")
viper.SetDefault("db.password", "example")
viper.SetDefault("db.database", "howmuch")
viper.SetDefault("db.sslmode", "disable")
}
// initConfig reads in config file and ENV variables if set.
func initConfig() {
defaultConfig()
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)

View File

@ -0,0 +1,32 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.27.0
package sqlc
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,
}
}

View File

@ -0,0 +1,72 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.27.0
// source: user.sql
package sqlc
import (
"context"
"github.com/jackc/pgx/v5/pgtype"
)
const insertUser = `-- name: InsertUser :one
INSERT INTO "user" (
email, first_name, last_name, password, created_at, updated_at
) VALUES ( $1, $2, $3, $4, $5, $6 )
RETURNING id, email, first_name, last_name, password, created_at, updated_at
`
type InsertUserParams struct {
Email string
FirstName string
LastName string
Password string
CreatedAt pgtype.Timestamp
UpdatedAt pgtype.Timestamp
}
// MIT License
//
// Copyright (c) 2024 vinchent <vinchent@vinchent.xyz>
//
// 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.
func (q *Queries) InsertUser(ctx context.Context, arg InsertUserParams) (User, error) {
row := q.db.QueryRow(ctx, insertUser,
arg.Email,
arg.FirstName,
arg.LastName,
arg.Password,
arg.CreatedAt,
arg.UpdatedAt,
)
var i User
err := row.Scan(
&i.ID,
&i.Email,
&i.FirstName,
&i.LastName,
&i.Password,
&i.CreatedAt,
&i.UpdatedAt,
)
return i, err
}

View File

@ -31,6 +31,7 @@ import (
"syscall"
"time"
"git.vinchent.xyz/vinchent/howmuch/internal/howmuch/infra/datastore"
"git.vinchent.xyz/vinchent/howmuch/internal/pkg/core"
"git.vinchent.xyz/vinchent/howmuch/internal/pkg/errno"
"git.vinchent.xyz/vinchent/howmuch/internal/pkg/log"
@ -105,6 +106,24 @@ func run() error {
gin.SetMode(gin.ReleaseMode)
}
// Init DB
dbPool, err := datastore.NewDB(
fmt.Sprintf(
"host=%s port=%d dbname=%s user=%s password=%s sslmode=%s",
viper.GetString("db.host"),
viper.GetInt("db.port"),
viper.GetString("db.database"),
viper.GetString("db.username"),
viper.GetString("db.password"),
viper.GetString("db.sslmode"),
),
)
if err != nil {
log.FatalLog("DB connection failure", "err", err)
}
defer dbPool.Close()
r := gin.Default()
// Middlewares

View File

@ -1,32 +1,44 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.27.0
// MIT License
//
// Copyright (c) 2024 vinchent <vinchent@vinchent.xyz>
//
// 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.
package sqlc
package datastore
import (
"context"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
"github.com/jackc/pgx/v5/pgxpool"
)
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,
// NewDB creates a new database for the application
func NewDB(dsn string) (*pgxpool.Pool, error) {
conn, err := pgxpool.New(context.Background(), dsn)
if err != nil {
return nil, err
}
// Ping test the conn
if err = conn.Ping(context.Background()); err != nil {
return nil, err
}
return conn, err
}

View File

@ -1,50 +0,0 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.27.0
// source: user.sql
package sqlc
import (
"context"
"github.com/jackc/pgx/v5/pgtype"
)
const insertUser = `-- name: InsertUser :one
INSERT INTO "user" (
email, first_name, last_name, password, created_at, updated_at
) VALUES ( $1, $2, $3, $4, $5, $6 )
RETURNING id, email, first_name, last_name, password, created_at, updated_at
`
type InsertUserParams struct {
Email string
FirstName string
LastName string
Password string
CreatedAt pgtype.Timestamp
UpdatedAt pgtype.Timestamp
}
func (q *Queries) InsertUser(ctx context.Context, arg InsertUserParams) (User, error) {
row := q.db.QueryRow(ctx, insertUser,
arg.Email,
arg.FirstName,
arg.LastName,
arg.Password,
arg.CreatedAt,
arg.UpdatedAt,
)
var i User
err := row.Scan(
&i.ID,
&i.Email,
&i.FirstName,
&i.LastName,
&i.Password,
&i.CreatedAt,
&i.UpdatedAt,
)
return i, err
}