Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use read-only DB replica for read-only GORM methods and criteria

I have a read-only MySql DB replica and want to use it for all select-based GORM requests and criteria all over the project (Grails 3.1.16) Is there any beautiful way to override the default DataSource only for the part of GORM methods (e.g. get, find)?

According to the documentation, we can use a particular dataSource for domain/service or use namespace with each method call. But I am looking for a more generic mechanism.

like image 949
Eugene Hoza Avatar asked Jan 29 '26 20:01

Eugene Hoza


1 Answers

You can use dbresolver. For example:

db.Use(dbresolver.Register(dbresolver.Config{
    Replicas: []gorm.Dialector{mysql.Open("read_only_replica")},
}))
like image 57
zwtop Avatar answered Jan 31 '26 12:01

zwtop



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!