Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
tooilxui opened this issue Jul 22, 2023 · 0 comments
Assignees

Comments

@tooilxui
Copy link

tooilxui commented Jul 22, 2023

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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants