gormconnector

package module
v0.2.4 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: MIT Imports: 22 Imported by: 1

README

lemmego/gormconnector

Wires GORM into a Lemmego application's lifecycle: it opens the connection from your configured database block, registers it in the container, and shuts it down cleanly.

Use it when you want GORM as your data layer. For the Lemmego ORM instead, use ormconnector.

// bootstrap/providers.go
func LoadProviders() []app.Provider {
    return []app.Provider{
        &gormconnector.Provider{UseGPA: true},
        // … everything that needs a database goes after this
    }
}

Install

go get github.com/lemmego/gormconnector

Then import the driver you actually use — the connector does not pick one for you, so a deployment links only the database it talks to.

With or without GPA

&gormconnector.Provider{UseGPA: true}   // type-safe repositories
&gormconnector.Provider{UseGPA: false}  // a raw *gorm.DB

With UseGPA: true you also get a gpagorm provider registered, so you can reach a typed repository:

db := gormconnector.Get(c.App())
repo := gpagorm.GetRepository[User](db)

user, err := repo.FindByID(c.RequestContext(), 123)   // *User, not interface{}

With UseGPA: false you get the *gorm.DB itself and write GORM as you normally would.

Where the connection comes from

From your application's own configuration, not from this module. That is the point of a connector: the DSN your migrations run against and the DSN your handlers query are the same one, so they cannot drift.

Supported drivers: postgres, postgresql, mysql, sqlite, sqlite3, sqlserver, mssql.

To reach past the connector when you need something GORM-specific:

conn := &gormconnector.Provider{UseGPA: true}
conn.WithGORMConfig(&gorm.Config{PrepareStmt: true}).WithGPAConfig(cfg)

return []app.Provider{conn, …}

SQLDB() gives you the underlying *sql.DB for anything that wants the standard library — a session store, a health check, a migration runner.

Ordering

List this provider before anything that needs a database. A provider that calls db.Resolve(a) with nothing registered yet does not fail loudly; it quietly derives its own connection, and then you have two pools against one server and one of them ignoring your configuration.

Generators

Two commands come with it:

lemmego gorm:model Post    # a GORM model with gorm: struct tags
lemmego gorm:repo Post     # a repository, with GPA integration

Testing

go test ./...
GOWORK=off go test ./...   # as a published module

Documentation

Index

Constants

View Source
const (
	RelationOneToOne   = "one_to_one"
	RelationOneToMany  = "one_to_many"
	RelationManyToOne  = "many_to_one"
	RelationManyToMany = "many_to_many"
)

Variables

View Source
var CommonModelFields = []*ModelField{
	{
		Name:     "id",
		Type:     "uint64",
		Required: true,
		Primary:  true,
	},
	{
		Name:     "created_at",
		Type:     "time.Time",
		Required: true,
	},
	{
		Name:     "updated_at",
		Type:     "time.Time",
		Required: true,
	},
	{
		Name:     "deleted_at",
		Type:     "time.Time",
		Required: true,
	},
}

Functions

func Get

func Get(a app.App) *gpagorm.Provider

func NewGormConnection added in v0.1.1

func NewGormConnection(config gpa.Config) (*gorm.DB, error)

func SupportedDrivers added in v0.1.1

func SupportedDrivers() []string

SupportedDrivers returns the list of supported database drivers

Types

type DBTag added in v0.1.1

type DBTag struct {
	Name     string
	Argument string
}

type DBTagBuilder added in v0.1.1

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

func NewDBTagBuilder added in v0.1.1

func NewDBTagBuilder(tags []*DBTag, driverName string) *DBTagBuilder

func (*DBTagBuilder) Add added in v0.1.1

func (mtb *DBTagBuilder) Add(name, argument string) *DBTagBuilder

func (*DBTagBuilder) Build added in v0.1.1

func (mtb *DBTagBuilder) Build() string

type ModelConfig added in v0.1.1

type ModelConfig struct {
	Name   string
	Fields []*ModelField
}

type ModelField added in v0.1.1

type ModelField struct {
	Name               string
	Type               string
	Required           bool
	Unique             bool
	Primary            bool
	ForeignConstrained bool
	Relation           string
}

type ModelGenerator added in v0.1.1

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

func NewModelGenerator added in v0.1.1

func NewModelGenerator(mc *ModelConfig) *ModelGenerator

func (*ModelGenerator) Generate added in v0.1.1

func (mg *ModelGenerator) Generate(appendable ...[]byte) error

func (*ModelGenerator) GetPackagePath added in v0.1.1

func (mg *ModelGenerator) GetPackagePath() string

func (*ModelGenerator) GetStub added in v0.1.1

func (mg *ModelGenerator) GetStub() string

type Provider

type Provider struct {
	UseGPA bool
	// contains filtered or unexported fields
}

func (*Provider) AddCommands added in v0.1.1

func (g *Provider) AddCommands() []app.Command

func (*Provider) Dialect added in v0.1.5

func (g *Provider) Dialect() db.Dialect

Dialect reports the SQL flavour of the open connection.

func (*Provider) Name added in v0.1.5

func (g *Provider) Name() string

Name reports which sql.connections key this connection was built from.

func (*Provider) Provide

func (g *Provider) Provide(a app.App) error

func (*Provider) SQLDB added in v0.1.5

func (g *Provider) SQLDB() *sql.DB

SQLDB returns the underlying connection. It is half of db.Connection, and is spelled the same way in every connector so the seam has one name.

func (*Provider) Shutdown added in v0.1.1

func (g *Provider) Shutdown(ctx context.Context) error

func (*Provider) WithGORMConfig added in v0.1.1

func (g *Provider) WithGORMConfig(config *gorm.Config) *Provider

func (*Provider) WithGPAConfig added in v0.1.1

func (g *Provider) WithGPAConfig(config gpa.Config) *Provider

type RepoConfig added in v0.1.1

type RepoConfig struct {
	Name string
}

type RepoGenerator added in v0.1.1

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

func NewRepoGenerator added in v0.1.1

func NewRepoGenerator(rc *RepoConfig) *RepoGenerator

func (*RepoGenerator) Generate added in v0.1.1

func (rg *RepoGenerator) Generate(appendable ...[]byte) error

func (*RepoGenerator) GetPackagePath added in v0.1.1

func (rg *RepoGenerator) GetPackagePath() string

func (*RepoGenerator) GetStub added in v0.1.1

func (rg *RepoGenerator) GetStub() string

Jump to

Keyboard shortcuts

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