Skip to content
/ pgsq Public

Wrapper that glues pgx, pgscan, & squirrel together

License

Notifications You must be signed in to change notification settings

vaardan/pgsq

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pgsq

About

Wrapper for pgx/v5 pool.

scany/v2 is used to provide Select & Get convenience methods.

All basic (i.e. not raw) methods take sqlizer interface as a query, which is provided by squirrel query builder.

Usage example

Actual data-access methods should take pgsq.Queryable as an argument–this way CreateEntity can be called using a connection pool or a transaction depending on logical requirements.

package database

import (
    "context"
    "fmt"

    "github.com/vaardan/pgsq"
    "github.com/Masterminds/squirrel"
)


// CreateEntity creates new entity with the given name and returns its ID.
func CreateEntity(ctx context.Context, q pgsq.Queryable, name string) (int, error) {
	query := squirrel.StatementBuilder.
		PlaceholderFormat(squirrel.Dollar).
		Insert("entity_table").
		Columns("name").
		Values(name).
		Suffix("returning id")

	var id int
	err := q.Get(ctx, &id, query)
	if err != nil {
		return 0, fmt.Errorf("insert entity: %w", err)
	}

	return id, nil
}