dataservicex

package module
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 27, 2022 License: MIT Imports: 4 Imported by: 0

README ¶

dataservicex

Go Reference build_pr Go Report Card Built with WeBuild

dataservicex is a go library that wrapup common CRUD functions of the data service layer to access a database. As a common application, it frequently re-implements the following functions with its data model:

  • Create model
  • Update full model
  • Update specific columns
  • Get a model by ID
  • Get list model
  • Delete by ID

The dataservicex uses generic, goqu, and sqlx to implement the common functions.

Quickstart

Install library

Import library to your project

go get -u github.com/vnteamopen/godebouncer

Usage

import (
	"github.com/jmoiron/sqlx"
	_ "github.com/go-sql-driver/mysql"


)

type Person struct {
	ID        int64          `db:"person_id" goqu:"skipupdate"`
	Name      string         `db:"name"`
	Age       int64          `db:"age"`
}

func (Person) TableName() string {
	return "person"
}

func (Person) IDColumnName() string {
	return "person_id"
}

func main() {
	db, _ := sqlx.Connect("mysql", connStr)
	dataService := dataservicex.NewDataServices[Person](db)
}

You can specific Dialect of database query generating

	dialect := goqu.Dialect("mysql")
	dataService = dataservicex.NewDataServices(db, dataservicex.WithDialect[Person](dialect))

Examples

Create model

func main() {
	dataService := dataservicex.NewDataServices[Person](...)
	createdPerson, err := dataService.Create(context.Background(), Person{
		Name: "Thuc Le",
		Age: 25,
	})
}

Update full model

func main() {
	dataService := dataservicex.NewDataServices[Person](...)
	updatingID := 1
	updatedPerson, err := dataService.Update(context.Background(), updatingID, Person{
		Name: "Thuc Le",
		Age: 25,
	})
}

Update specific columns

func main() {
	dataService := dataservicex.NewDataServices[Person](...)
	updatingID := 1
	updatedPerson, err := dataService.UpdateColumns(context.Background(),
		updatingID,
		goqu.Record{
			"name": "Thuc Le",
		})
}

Get a model by ID

func main() {
	dataService := dataservicex.NewDataServices[Person](...)
	person, err := dataService.GetByID(context.Background(), 1)
}

Get list model

func main() {
	dataService := dataservicex.NewDataServices[Person](...)
	personList, err := dataService.GetList(context.Background())
}

Delete by ID

func main() {
	dataService := dataservicex.NewDataServices[Person](...)
	err := dataService.Delete(context.Background(), 1)
}

License

MIT

Contribution

All your contributions to project and make it better, they are welcome. Feel free to start an issue.

Thanks! 🙌

Documentation ¶

Index ¶

Constants ¶

This section is empty.

Variables ¶

This section is empty.

Functions ¶

func WithDialect ¶

func WithDialect[T model](dialect goqu.DialectWrapper) func(*DataServices[T])

Types ¶

type DataServices ¶

type DataServices[T model] struct {
	// contains filtered or unexported fields
}

func NewDataServices ¶

func NewDataServices[T model](db *sqlx.DB, options ...func(*DataServices[T])) DataServices[T]

func (*DataServices[T]) Create ¶

func (ds *DataServices[T]) Create(ctx context.Context, m T) (T, error)

func (*DataServices[T]) Delete ¶

func (ds *DataServices[T]) Delete(ctx context.Context, id interface{}) error

func (*DataServices[T]) GetByID ¶

func (ds *DataServices[T]) GetByID(ctx context.Context, id interface{}) (T, error)

func (*DataServices[T]) GetDBx ¶ added in v0.1.2

func (ds *DataServices[T]) GetDBx() *sqlx.DB

func (*DataServices[T]) GetDialect ¶ added in v0.1.1

func (ds *DataServices[T]) GetDialect() goqu.DialectWrapper

func (*DataServices[T]) GetList ¶

func (ds *DataServices[T]) GetList(ctx context.Context) ([]T, error)

func (*DataServices[T]) Update ¶

func (ds *DataServices[T]) Update(ctx context.Context, id interface{}, updating T) (T, error)

func (*DataServices[T]) UpdateColumns ¶

func (ds *DataServices[T]) UpdateColumns(ctx context.Context, id interface{}, record goqu.Record) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL