fxorm

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2024 License: MIT Imports: 9 Imported by: 0

README

Fx ORM Module

ci go report codecov Deps PkgGoDev

Fx module for orm.

Installation

go get github.com/ankorstore/yokai/fxorm

Documentation

Dependencies

This module is intended to be used alongside:

Loading

To load the module in your Fx application:

package main

import (
	"github.com/ankorstore/yokai/fxconfig"
	"github.com/ankorstore/yokai/fxlog"
	"github.com/ankorstore/yokai/fxorm"
	"github.com/ankorstore/yokai/fxtrace"
	"go.uber.org/fx"
	"gorm.io/gorm"
)

type Model struct {
	Name string
}

func main() {
	fx.New(
		fxconfig.FxConfigModule,      // load the module dependencies
		fxlog.FxLogModule,
		fxtrace.FxTraceModule,
		fxorm.FxOrmModule,            // load the module
		fx.Invoke(func(db *gorm.DB) { // invoke the orm
			db.Create(&Model{Name: "some name"})
		}),
	).Run()
}
Configuration

This module provides the possibility to configure the ORM driver:

  • sqlite for SQLite databases
  • mysql for MySQL databases
  • postgres for PostgreSQL databases
  • sqlserver for SQL Server databases

You can also provide to the ORM the databasedsn, some config, and configure SQL queries logging and tracing.

# ./configs/config.yaml
app:
  name: app
  env: dev
  version: 0.1.0
  debug: false
modules:
  orm:
    driver: mysql                                               # driver to use
    dsn: "user:pass@tcp(dbhost:3306)/dbname?parseTime=True"     # database DSN to connect to
    config:
      dry_run: false                                            # disabled by default
      skip_default_transaction: false                           # disabled by default
      full_save_associations: false                             # disabled by default
      prepare_stmt: false                                       # disabled by default
      disable_automatic_ping: false                             # disabled by default
      disable_foreign_key_constraint_when_migrating: false      # disabled by default
      ignore_relationships_when_migrating: false                # disabled by default
      disable_nested_transaction: false                         # disabled by default
      allow_global_update: false                                # disabled by default
      query_fields: false                                       # disabled by default
      translate_error: false                                    # disabled by default
    log:
      enabled: true  # to log SQL queries, disabled by default
      level: info    # with a minimal level
      values: true   # by adding or not clear SQL queries parameters values in logs, disabled by default
    trace:
      enabled: true  # to trace SQL queries, disabled by default
      values: true   # by adding or not clear SQL queries parameters values in trace spans, disabled by default

See GORM Config for more details about the configuration.

Auto migrations

This module provides the possibility to run automatically your schemas migrations with RunFxOrmAutoMigrate():

package main

import (
	"github.com/ankorstore/yokai/fxconfig"
	"github.com/ankorstore/yokai/fxlog"
	"github.com/ankorstore/yokai/fxorm"
	"github.com/ankorstore/yokai/fxtrace"
	"go.uber.org/fx"
	"gorm.io/gorm"
)

type Model struct {
	Name string
}

func main() {
	fx.New(
		fxconfig.FxConfigModule,             // load the module dependencies
		fxlog.FxLogModule,
		fxtrace.FxTraceModule,
		fxorm.FxOrmModule,                   // load the module
		fxorm.RunFxOrmAutoMigrate(&Model{}), // run auto migration for Model
		fx.Invoke(func(db *gorm.DB) { // invoke the orm
			db.Create(&Model{Name: "some name"})
		}),
	).Run()
}
Performance

See GORM performance recommendations.

Disable Default Transaction

Gorm performs write (create/update/delete) operations by default inside a transaction to ensure data consistency, which is not optimized for performance.

You can disable it in the configuration:

# ./configs/config.yaml
app:
  name: app
  env: dev
  version: 0.1.0
  debug: false
modules:
  orm:
    driver: mysql                                               # driver to use
    dsn: user:pass@tcp(127.0.0.1:3306)/dbname?parseTime=True"   # database DSN to connect to
    config:
      skip_default_transaction: true                            # disable default transaction
Cache Prepared Statement

To create a prepared statement when executing any SQL (and cache them to speed up future calls):

# ./configs/config.yaml
app:
  name: app
  env: dev
  version: 0.1.0
  debug: false
modules:
  orm:
    driver: mysql                                               # driver to use
    dsn: user:pass@tcp(127.0.0.1:3306)/dbname?parseTime=True"   # database DSN to connect to
    config:
      prepare_stmt: true                                        # enable prepared statements
Override

By default, the gorm.DB is created by the DefaultOrmFactory.

If needed, you can provide your own factory and override the module:

package main

import (
	"github.com/ankorstore/yokai/fxconfig"
	"github.com/ankorstore/yokai/fxlog"
	"github.com/ankorstore/yokai/fxorm"
	"github.com/ankorstore/yokai/fxtrace"
	"github.com/ankorstore/yokai/orm"
	"go.uber.org/fx"
	"gorm.io/gorm"
)

type CustomOrmFactory struct{}

func NewCustomOrmFactory() orm.OrmFactory {
	return &CustomOrmFactory{}
}

func (f *CustomOrmFactory) Create(options ...orm.OrmOption) (*gorm.DB, error) {
	return &gorm.DB{...}, nil
}

type Model struct {
	Name string
}

func main() {
	fx.New(
		fxconfig.FxConfigModule,            // load the module dependencies
		fxlog.FxLogModule,
		fxtrace.FxTraceModule,
		fxorm.FxOrmModule,                  // load the module
		fx.Decorate(NewCustomOrmFactory),   // override the module with a custom factory
		fx.Invoke(func(customDb *gorm.DB) { // invoke the custom ORM
			customDb.Create(&Model{Name: "custom"})
		}),
	).Run()
}

Documentation

Index

Constants

View Source
const ModuleName = "orm"

ModuleName is the module name.

Variables

FxOrmModule is the Fx orm module.

Functions

func NewFxOrm

func NewFxOrm(p FxOrmParam) (*gorm.DB, error)

NewFxOrm returns a gorm.DB.

func RunFxOrmAutoMigrate

func RunFxOrmAutoMigrate(models ...any) fx.Option

RunFxOrmAutoMigrate performs auto migrations for a provided list of models.

Types

type FxOrmParam

type FxOrmParam struct {
	fx.In
	LifeCycle      fx.Lifecycle
	Factory        orm.OrmFactory
	Config         *config.Config
	TracerProvider trace.TracerProvider
}

FxOrmParam allows injection of the required dependencies in NewFxOrm.

Jump to

Keyboard shortcuts

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