Skip to content

Is there a possible way to set multiple gorm.Config to each database in the database resolver? #108

Open
@tooilxui

Description

@tooilxui

I want to access informix, sql server, postres sql in the same database resolver.

But informix is an old server and doesn't enable trasaction, I have to enable SkipDefaultTransaction in gorm.Config to avoid errors.
on the other hand, other databases are working with transactions enabled, is there a possible way to set gorm.Config to every database registered in resolver?

it's seems all resolvers are share same gorm.Config from main DB, my current code here below:

func BuildDbResolver() (*gorm.DB, error) {
	mainDB, err := gorm.Open(
		postgres.Open(
			fmt.Sprintf(
				"host=%s port=%d user=%s dbname=%s password=%s sslmode=disable",
				PgErpConfig.Address,
				PgErpConfig.Port,
				PgErpConfig.UserName,
				PgErpConfig.Name,
				PgErpConfig.Password,
			),
		),
		&gorm.Config{Logger: NewLogger()},
	)
	if err != nil {
		return nil, fmt.Errorf("failed to connect to postgreSQL database: %v", err)
	}

	resolver := dbresolver.Register(dbresolver.Config{
		Sources:           []gorm.Dialector{ifx.Open(Ids12Config.DSN)},  // how to apply gorm.Config with SkipDefaultTransaction = 'true' to informix only?
		TraceResolverMode: true,
	}).Register(dbresolver.Config{
		Sources:           []gorm.Dialector{sqlserver.Open(EdisonConfig.DSN)},
		TraceResolverMode: true,
	})
	mainDB.Use(resolver)
	return mainDB, nil
}

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions