datahub

package module
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2023 License: Apache-2.0 Imports: 8 Imported by: 12

README

Datahub

Datahub is wrapper of dbflex. Datahub automates some process to dealing with data

  • Connection Pooling Management
  • Open, Release and Close Connection

Setup

To work with datahub, we have to prepare a datahub object.

h := datahub.New(func()(dbflex.IConnection, error){
    //-- code here

    return conn, nil
})
defer h.Close()

Once the h object is created, we can using it right away to handle our data processing within the solution.

ORM-ish data

Get
obj := new(MyDataRecord)
e := h.GetByID(obj, "myid")
Get by Attribute
e := h.GetByAtrr(obj, "Name", "Paijo")
Get by filter
e := h.GetByFilter(obj, dbflex.Eqs("Grade",10,"Location","Indonesia"))
Gets

Reading multiple rows of data. We need to pass 3 parameters

  • model, orm object that will be read
  • parm, a dbflex QueryParam object
  • target, target of data to be stored
items := []Item{}
qp := dbflex.NewQueryParam().SetWhere(f).SetTake(5) // filter by 5 and take 5 records only
e := h.Gets(new(item), parm, &items)
Count
e := h.Count(new(item), parm)
Write to database
e := h.Insert(model)
e := h.Update(model)
e := h.Save(model)
e := h.Delete(model)

Non ORM

To access non-orm data, we can use method GetAny*, Populate* and Execute

Get data
e := h.GetAnyByFilter("myTable", dbflex.Eq("_id","dataid"), target)
e := h.GetAnyByParm("myTable", dbflex.NewQueryParam().SetWhere("Grade",20).Aggr(dbflex.Avg("Salary")), target)
Read multi records
e := h.Populate(dbflex.From("myTable").Select().Take(1), target)
e := h.PopulateByFilter("mytable", myFilter)
e := h.PopulateByParm("mytable", myParm)
e := h.PopulateSQL(sqlTxt)  // only for rdbms
Write Data
e := h.InsertAny("mytable", record)
e := h.UpdateAny("mytable", dbflex.Eq("_id","myid"), record, "Status")
e := h.SaveAny("mytable", dbflex.Eq("_id", "myid"), record)
e := h.DeleteAny("mytable", nil)    // delete all record
e := h.DeleteAny("mytable", dbflex.Eq("Grade",0))

Sync/Migrate Database

// ORM
e := h.EnsureDb(ormModel)

// Non ORM
e := h.EnsureTable("mytable", myTableRecord)
e := h.EnsureIndex("myTable", "Email_Index", true, "Email")

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Count added in v0.3.0

func Count[T orm.DataModel](h *Hub, data T, qp *dbflex.QueryParam) (int, error)

Count returns number of data based on model and filter, generic mode

func Find added in v0.3.0

func Find[T orm.DataModel](h *Hub, model T, param *dbflex.QueryParam) ([]T, error)

Find return all data based on model and filter, generic mode

func FindAnyByCommand added in v0.3.1

func FindAnyByCommand[T any](h *Hub, model T, cmd dbflex.ICommand) ([]T, error)

FindAnyByCommand returns all data using dbflex command

func FindAnyByFilter added in v0.3.1

func FindAnyByFilter[T any](h *Hub, model T, tablename string, filter *dbflex.Filter, n int) ([]T, error)

func FindAnyByParm added in v0.3.1

func FindAnyByParm[T any](h *Hub, model T, tablename string, qp *dbflex.QueryParam) ([]T, error)

func FindAnyBySQL added in v0.3.1

func FindAnyBySQL[T any](h *Hub, model T, sql string) ([]T, error)

func FindByCommand added in v0.3.0

func FindByCommand[T orm.DataModel](h *Hub, data T, cmd dbflex.ICommand) ([]T, error)

FindByCommand returns all data using dbflex command, generic mode

func FindByFilter added in v0.3.0

func FindByFilter[T orm.DataModel](h *Hub, model T, filter *dbflex.Filter) ([]T, error)

FindByFilter like find but require only filter, generic mode

func GeneralDbConnBuilder added in v0.1.14

func GeneralDbConnBuilder(txt string) func() (dbflex.IConnection, error)

func GeneralDbConnBuilderWithTx added in v0.2.7

func GeneralDbConnBuilderWithTx(txt string, useTx bool) func() (dbflex.IConnection, error)

func Get added in v0.3.0

func Get[T orm.DataModel](h *Hub, model T) (T, error)

Get return single data, generic mode

func GetByFilter added in v0.3.0

func GetByFilter[T orm.DataModel](h *Hub, model T, filter *dbflex.Filter) (T, error)

GetByFilter return first record by filter

func GetByID added in v0.3.0

func GetByID[T orm.DataModel](h *Hub, model T, ids ...interface{}) (T, error)

GetByID return first record by theirs ID

func GetByParm added in v0.3.0

func GetByParm[T orm.DataModel](h *Hub, model T, qp *dbflex.QueryParam) (T, error)

GetByParm return first record by param

func GetByQuery added in v0.3.0

func GetByQuery[T orm.DataModel](h *Hub, model T, queryName string, payload codekit.M) (T, error)

GetByQuery return first record by query

Types

type Hub

type Hub struct {
	// contains filtered or unexported fields
}

Hub main datahub object. This object need to be initiated to work with datahub

func NewHub

func NewHub(fn func() (dbflex.IConnection, error), usePool bool, poolsize int) *Hub

NewHub function to create new hub

func NewHubWithOpts added in v0.1.17

func NewHubWithOpts(fn func() (dbflex.IConnection, error), opts *HubOptions) *Hub

NewHub function to create new hub

func (*Hub) BeginTx added in v0.1.4

func (h *Hub) BeginTx() (*Hub, error)

BeginTx create a hub with Transaction. Commit and/or Rollback need to call later on to close the transaction

func (*Hub) Close

func (h *Hub) Close()

func (*Hub) CloseConnection

func (h *Hub) CloseConnection(idx string, conn dbflex.IConnection)

CloseConnection to close connection

func (*Hub) Commit added in v0.1.4

func (h *Hub) Commit() error

Commit commits all change into database

func (*Hub) Count

func (h *Hub) Count(data orm.DataModel, qp *dbflex.QueryParam) (int, error)

Count returns number of data based on model and filter

func (*Hub) CountAny added in v0.1.17

func (h *Hub) CountAny(name string, qp *dbflex.QueryParam) (int, error)

CountAny returns number of data based on tablename and filter

func (*Hub) CountAnyByFilter added in v0.2.8

func (h *Hub) CountAnyByFilter(name string, f *dbflex.Filter) (int, error)

CountByAnyFilter returns number of data based on table name and filter

func (*Hub) CountByFilter added in v0.2.8

func (h *Hub) CountByFilter(data orm.DataModel, f *dbflex.Filter) (int, error)

CountByFilter returns number of data based on model and filter

func (*Hub) Delete

func (h *Hub) Delete(data orm.DataModel) error

Delete delete the active record on database

func (*Hub) DeleteAny added in v0.2.3

func (h *Hub) DeleteAny(name string, filter *dbflex.Filter) error

DeleteAny delete record on database table. Normally used with no-datamodel object

func (*Hub) DeleteByFilter added in v0.2.8

func (h *Hub) DeleteByFilter(model orm.DataModel, where *dbflex.Filter) error

DeleteByFilter delete object in database based on specific model and filter

func (*Hub) DeleteQuery

func (h *Hub) DeleteQuery(model orm.DataModel, where *dbflex.Filter) error

DeleteQuery delete object in database based on specific model and filter, will be DEPRECATED use DeleteByFilter instead

func (*Hub) EnsureDb added in v0.2.3

func (h *Hub) EnsureDb(obj orm.DataModel) error

EnsureDb

func (*Hub) EnsureIndex added in v0.2.3

func (h *Hub) EnsureIndex(tableName, indexName string, unique bool, fields ...string) error

EnsureIndex ensure index existence

func (*Hub) EnsureTable added in v0.1.7

func (h *Hub) EnsureTable(name string, keys []string, object interface{}) error

EnsureTable will ensure existense of table according to given object

func (*Hub) Execute

func (h *Hub) Execute(cmd dbflex.ICommand, object interface{}) (interface{}, error)

Execute will execute command. Normally used with no-datamodel object

func (*Hub) Get

func (h *Hub) Get(data orm.DataModel) error

Get return single data based on model. It will find record based on releant ID field

func (*Hub) GetAnyByFilter added in v0.1.16

func (h *Hub) GetAnyByFilter(tableName string, filter *dbflex.Filter, dest interface{}) error
GetAnyByFilter returns single data based on filter enteered. Data need to be comply with orm.DataModel.

Because no sort is defined, it will only return 1st row by any given resultset If sort is needed pls use by ByParm

func (*Hub) GetAnyByParm added in v0.1.16

func (h *Hub) GetAnyByParm(tableName string, parm *dbflex.QueryParam, dest interface{}) error

GetAnyByParm return single data based on filter and table name

func (*Hub) GetByAttr added in v0.1.13

func (h *Hub) GetByAttr(data orm.DataModel, attr string, value interface{}) error

GetByAttr returns single data based on value on single field. Data need to be comply with orm.DataModel

func (*Hub) GetByFilter added in v0.1.15

func (h *Hub) GetByFilter(data orm.DataModel, filter *dbflex.Filter) error
GetByFilter returns single data based on filter enteered. Data need to be comply with orm.DataModel.

Because no sort is defined, it will only 1st row by any given sort If sort is needed pls use by ByParm

func (*Hub) GetByID

func (h *Hub) GetByID(data orm.DataModel, ids ...interface{}) error

GetByID returns single data based on its ID. Data need to be comply with orm.DataModel

func (*Hub) GetByParm

func (h *Hub) GetByParm(data orm.DataModel, parm *dbflex.QueryParam) error

GetByParm return single data based on filter

func (*Hub) GetByQuery added in v0.2.4

func (h *Hub) GetByQuery(obj orm.DataModel, queryName string, param codekit.M) error

func (*Hub) GetClassicConnection

func (h *Hub) GetClassicConnection() (dbflex.IConnection, error)

GetClassicConnection get connection without using pool. CleanUp operation need to be done manually

func (*Hub) GetConnection

func (h *Hub) GetConnection() (string, dbflex.IConnection, error)

GetConnection to generate connection. It will return index, connection and error. Index and connection need to be retain for purpose of closing the connection. It is advised to use other DB operation related of Hub rather than build manual connection

func (*Hub) Gets

func (h *Hub) Gets(data orm.DataModel, parm *dbflex.QueryParam, dest interface{}) error

Gets return all data based on model and filter

func (*Hub) GetsByFilter added in v0.2.7

func (h *Hub) GetsByFilter(data orm.DataModel, filter *dbflex.Filter, dest interface{}) error

GetsByFilter like gets but require only filter

func (*Hub) GetsByQuery added in v0.2.4

func (h *Hub) GetsByQuery(obj orm.DataModel, queryName string, param codekit.M, target interface{}) error

func (*Hub) Insert

func (h *Hub) Insert(data orm.DataModel) error

Insert will create data into database

func (*Hub) InsertAny added in v0.2.3

func (h *Hub) InsertAny(name string, object interface{}) error

InsertAny insert any object into database table

func (*Hub) IsTx added in v0.1.10

func (h *Hub) IsTx() bool

func (*Hub) Log

func (h *Hub) Log() *logger.LogEngine

Log get logger object

func (*Hub) PoolSize

func (h *Hub) PoolSize() int

PoolSize returns size of the pool

func (*Hub) Populate

func (h *Hub) Populate(cmd dbflex.ICommand, result interface{}) (int, error)

Populate will return all data based on command. Normally used with no-datamodel object

func (*Hub) PopulateByFilter added in v0.1.15

func (h *Hub) PopulateByFilter(tableName string, filter *dbflex.Filter, n int, dest interface{}) error

PopulateByFilter returns all data based on filter and n count. No sort is given, hence it will take n data for resp filter If n-count is 0, it will load all

func (*Hub) PopulateByParm

func (h *Hub) PopulateByParm(tableName string, parm *dbflex.QueryParam, dest interface{}) error

PopulateByParm returns all data based on table name and QueryParm. Normally used with no-datamodel object

func (*Hub) PopulateSQL added in v0.1.3

func (h *Hub) PopulateSQL(sql string, dest interface{}) error

PopulateSQL returns data based on SQL Query

func (*Hub) Rollback added in v0.1.4

func (h *Hub) Rollback() error

Rollback to reverts back all change into database

func (*Hub) Save

func (h *Hub) Save(data orm.DataModel, fields ...string) error

Save will save data into database, if fields is activated it will save the mentioned fields only, fields will only work with update hence it will assume record already exist

func (*Hub) SaveAny

func (h *Hub) SaveAny(name string, filter *dbflex.Filter, object interface{}) error

SaveAny save any object into database table. Normally used with no-datamodel object

func (*Hub) SetAutoCloseDuration

func (h *Hub) SetAutoCloseDuration(d time.Duration) *Hub

SetAutoCloseDuration set duration for a connection inside Hub Pool to be closed if it is not being used

func (*Hub) SetAutoReleaseDuration

func (h *Hub) SetAutoReleaseDuration(d time.Duration) *Hub

SetAutoReleaseDuration set duration for a connection in pool to be released for a process

func (*Hub) SetLog

func (h *Hub) SetLog(l *logger.LogEngine) *Hub

SetLog set logger

func (*Hub) SetOptions added in v0.2.0

func (h *Hub) SetOptions(opts *HubOptions) *Hub

Set Hub Opts

func (*Hub) Update

func (h *Hub) Update(data orm.DataModel, fields ...string) error

Update will update single data in database based on specific model

func (*Hub) UpdateAny added in v0.1.2

func (h *Hub) UpdateAny(name string, filter *dbflex.Filter, object interface{}, fields ...string) error

UpdateAny update specific fields on database table. Normally used with no-datamodel object

func (*Hub) UpdateField

func (h *Hub) UpdateField(data orm.DataModel, where *dbflex.Filter, fields ...string) error

UpdateField update relevant fields in data based on specific filter

func (*Hub) UsePool

func (h *Hub) UsePool() bool

UsePool is a hub using pool

func (*Hub) Validate added in v0.1.8

func (h *Hub) Validate() error

Validate validate if a connection can be established

type HubOptions added in v0.1.17

type HubOptions struct {
	AutoClose   time.Duration
	AutoRelease time.Duration
	Timeout     time.Duration
	UsePool     bool
	PoolSize    int
}

Jump to

Keyboard shortcuts

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