Documentation ¶
Index ¶
- Constants
- Variables
- func ConnectToPostgres(config Config) (*gorm.DB, error)
- func ConnectToSqlite3(dbname string, walMode bool) *gorm.DB
- func IsPointer(v any) bool
- func MigrateViewsFunctionsAndTriggers(db *gorm.DB, database, user string) (output []byte, err error)
- func NewLogger(logLevel SqlLogLevel, w io.Writer) logger.Interface
- func ParseDSN(dsn string, params *DSNParamas)
- func Ping(db *gorm.DB) error
- func WriteDropFunctionsQueries(db *gorm.DB, w *bufio.Writer) error
- func WriteDropTriggerQueries(db *gorm.DB, w *bufio.Writer) error
- func WriteDropViewQueries(db *gorm.DB, w *bufio.Writer) error
- type Condition
- type Config
- type DSNParamas
- type Group
- type Join
- type Limit
- type ORM
- type Order
- type PaginatedResult
- type Preload
- type Select
- type SqlLogLevel
- type Where
Constants ¶
const ( SILENT = iota WARN INFO ERROR )
const MemorySQLiteDB = "file::memory:"
Variables ¶
var ( // The model passed in is not a pointer ErrNotPointer = errors.New("not a valid pointer") // An update query affected 0 rows ErrNoRecordsUpdated = errors.New("no records updated") )
Functions ¶
func ConnectToPostgres ¶
Connect to the postgres database with the data source name.
func ConnectToSqlite3 ¶
Connect to dbname. If dbname is nil, it connect to a memory sqlite database ForeignKey pragma is enabled by for all connections
func MigrateViewsFunctionsAndTriggers ¶
func MigrateViewsFunctionsAndTriggers(db *gorm.DB, database, user string) (output []byte, err error)
Drops all views, functions, triggers Works only for postgres
WARNING: DO NOT run on in tests on a production database unless if doing actual migrations like dropping and re-creating all views, functions and triggers.
func NewLogger ¶
func NewLogger(logLevel SqlLogLevel, w io.Writer) logger.Interface
Creates a new SQL logger for gorm and that will write to w
func ParseDSN ¶
func ParseDSN(dsn string, params *DSNParamas)
parse postgres DSN into DSNParams struct
func WriteDropFunctionsQueries ¶
writes sql statements to drop all postgres functions/procedures to w Works only for postgres
func WriteDropTriggerQueries ¶
writes sql statements to drop all views to w.
Execute with psql since the postgres driver does not support multiple statements.
this is important for migrations
Types ¶
type Condition ¶
Interface that applies some operation to the GORM DB definition Multiple conditions are applied in the order specified. Select, Join, Where, Group, Having
type Config ¶
type Config struct { // Connection data source name DSN string // gorm's sql logger.Interface. Create one with helper gowrap.NewLogger Logger logger.Interface // Use a connection pool. // SetMaxIdleConns(20), SetMaxOpenConns(200) UseConnPool bool }
Configuration struct for connecting to postgres database
type DSNParamas ¶
type Group ¶
type Group struct {
Name string // grouping condition e.g "category"
}
Add grouping. Group should apear after Join but before Where conditions
type ORM ¶
type ORM interface { Insert(v any) error Update(v any) error PartialUpdate(model any, updates any, where Where) error Delete(v any, conditions ...Condition) error First(v any, id uint, conditions ...Condition) error FindOne(v any, where Where, conditions ...Condition) error FindAll(slicePtr any, conditions ...Condition) error DB() *gorm.DB }
type Order ¶
type Order struct {
Name string // grouping condition e.g "category DESC"
}
Add grouping. Group should apear after Join but before Where conditions
type PaginatedResult ¶
type PaginatedResult[T any] struct { Page int // Current page Limit int // Page size HasNext bool // if there is a next page HasPrev bool // is there is a previous page Count int // total number of records in the database. TotalPages int // total number of pages (based on Count) Results []T // Slice of the query results }
Represents a paginated query result based of limit/offset pagination
type Preload ¶
type Preload struct { // Main reload string e.g "Orders.Products" or "Users" Query string // condition for preloading e.g []any{"state NOT IN (?)", "cancelled"} Args []any }
Preload relationships.
if your struct has no nested relationships, you can simply pass clause.Associations as the query.
type SqlLogLevel ¶
type SqlLogLevel int