Documentation ¶
Index ¶
- Constants
- Variables
- func ConnectionURL(c *Config) string
- type Config
- type Dialect
- type Gorm
- func (g *Gorm) Create(ctx context.Context, model interface{}) (any, error)
- func (g *Gorm) DB(ctx context.Context) (*sql.DB, error)
- func (g *Gorm) Delete(ctx context.Context, model interface{}) (any, error)
- func (g *Gorm) DeleteWithWhere(ctx context.Context, models interface{}, condition map[string]interface{}) (any, error)
- func (g *Gorm) ExecuteTxn(ctx context.Context, fn func(ctx context.Context) error) error
- func (g *Gorm) FindByID(ctx context.Context, dest interface{}, id string) (any, error)
- func (g *Gorm) FindByIds(ctx context.Context, dest interface{}, ids []string) (any, error)
- func (g *Gorm) FindInBatches(ctx context.Context, dest interface{}, page int, limit int) (any, error)
- func (g *Gorm) FindWithQuery(ctx context.Context, dest interface{}, qb *query.Query) (any, error)
- func (g *Gorm) FindWithWhere(ctx context.Context, dest interface{}, condition map[string]interface{}) (any, error)
- func (g *Gorm) FindWithWhereInBatches(ctx context.Context, dest interface{}, condition map[string]interface{}, ...) (any, error)
- func (g *Gorm) Instance() (*sql.DB, error)
- func (g *Gorm) Update(ctx context.Context, model interface{}, values interface{}) (any, error)
- func (g *Gorm) UpdateWithUpdateables(ctx context.Context, updateables []string, model interface{}, ...) (any, error)
- func (g *Gorm) UpdateWithWhere(ctx context.Context, model interface{}, values interface{}, ...) (any, error)
- func (g *Gorm) Upsert(ctx context.Context, model interface{}) (any, error)
- type Migrator
- type MigratorConfig
Constants ¶
const ( // PostgresDNS is postgres connection path format for gorm. // host=localhost:3306 dbname=app sslmode=require user=svc password=pw PostgresDNS = "host=%s port=%d dbname=%s sslmode=%s user=%s password=%s" // MysqlDNS is mysql connection path format for gorm. // app:password@tcp(localhost:3306)/app?charset=utf8&parseTime=True&loc=Local // Default charset is utf8 MysqlDNS = "%s:%s@%s(%s:%d)/%s?charset=%s&parseTime=True&loc=Local" )
const ( // DBContextKey ... DBContextKey contextKey = iota )
const ( // DefaultMySQLCharset is the default mysql character set DefaultMySQLCharset string = "utf8" )
Variables ¶
var ErrBadRequestRecordNotFound = errors.New("BAD_REQUEST_RECORD_NOT_FOUND")
ErrBadRequestRecordNotFound ...
var ErrorUnsupportedDialect = errors.New(
"dialect for the db is not supported",
)
ErrorUnsupportedDialect tells the SQL dialect is not supported
Functions ¶
func ConnectionURL ¶
ConnectionURL makes the connection URL from the config
Types ¶
type Config ¶
type Config struct { // Dialect of SQL Dialect Dialect // Protocol ... Protocol string // URL ... URL string // Port ... Port int // Username ... Username string // Password ... Password string // SslMode ... SslMode string // Name ... Name string // Schema ... Schema string // Charset for client side interaction // only MYSQL uses it at present and uses utf8 as default CharSet string // MaxOpenConnections of the database allowed MaxOpenConnections int // MaxIdleConnections for the database before closing conn MaxIdleConnections int // ConnectionMaxLifeTime ... ConnectionMaxLifetime int // Debug turns on the debug logs for the ORM Debug bool // SQLiteDSN is used for testing purpose SQLiteDSN string }
Config this is to be used in application toml for loading the data into a storage config struct
type Gorm ¶
type Gorm struct {
// contains filtered or unexported fields
}
Gorm implements store.Storage for all SQL stores
func NewGorm ¶
NewGorm creates a new instance of gorm for all sql based interactions with the database, it wraps gorm to prevent direct dependency with gorm
func (*Gorm) DeleteWithWhere ¶
func (g *Gorm) DeleteWithWhere( ctx context.Context, models interface{}, condition map[string]interface{}) (any, error)
DeleteWithWhere deletes all record which matches where clause
func (*Gorm) ExecuteTxn ¶
ExecuteTxn runs multiple commands within a transaction, committing changes upon success and rolling them back if an error occurs.
func (*Gorm) FindByID ¶
FindByID finds using the primary key of the model and sets in the destination. Assumes that primary key is id column. Pass the dest as pointer
func (*Gorm) FindByIds ¶
FindByIds finds using the primary key of the model and sets in the destination. Assumes that primary key is id column. Pass the dest as pointer
func (*Gorm) FindInBatches ¶
func (g *Gorm) FindInBatches( ctx context.Context, dest interface{}, page int, limit int) (any, error)
FindInBatches retrieves a paginated set of results from the database.
func (*Gorm) FindWithQuery ¶
FindWithQuery allows general search criteria and sets in the destination
func (*Gorm) FindWithWhere ¶
func (g *Gorm) FindWithWhere(ctx context.Context, dest interface{}, condition map[string]interface{}) (any, error)
FindWithWhere finds using the conditions passed in map and sets in the destination
func (*Gorm) FindWithWhereInBatches ¶
func (g *Gorm) FindWithWhereInBatches( ctx context.Context, dest interface{}, condition map[string]interface{}, page int, limit int) (any, error)
FindWithWhereInBatches retrieves a paginated set of results from the database with conditions.
func (*Gorm) Instance ¶
Instance is used for performing direct operations on gorm One use case: database migrations
func (*Gorm) Update ¶
Update updated the model attributes that are part of values into the database using gorm. Returns the rows affected with the update and error. Uses primary key in the where clause returns error if primary key of the model is null.
func (*Gorm) UpdateWithUpdateables ¶
func (g *Gorm) UpdateWithUpdateables(ctx context.Context, updateables []string, model interface{}, values interface{}, query interface{}, args ...interface{}) (any, error)
UpdateWithUpdateables Update updated the model attributes that are part of values into the database using gorm. Returns the rows affected with the update and error Uses query and args in the where clause, returns error if args length is less than 1.
func (*Gorm) UpdateWithWhere ¶
func (g *Gorm) UpdateWithWhere( ctx context.Context, model interface{}, values interface{}, condition map[string]interface{}) (any, error)
UpdateWithWhere Update updated the model attributes that are part of values into the database using gorm. Returns the rows affected with the update and error Uses query and args in the where clause returns error if args length is less than 1.
type Migrator ¶
type Migrator struct { Gorm *Gorm // contains filtered or unexported fields }
Migrator is responsible for running database migrations.
func NewMigrator ¶
func NewMigrator(ctx context.Context, conf MigratorConfig) (*Migrator, error)
NewMigrator creates a new Migrator with the provided database connection and configuration.
type MigratorConfig ¶
MigratorConfig holds the configuration options for the migrator.