[Go] I want to separate endpoints by Read / Write to DB

Introduction

In order to support replication, I decided to divide the DB endpoint by Read / Write. I examined how to handle it with Go / Gorm2.

Usage environment

From the conclusion

Each Method of Gorm is wrapped in a Type that has multiple Connections for each Endpoint. By doing this, the side using the connection can operate the DB without being aware of the endpoint.

db/connect.go


package db

import (
	"os"

	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

type SqlHandler struct {
	ReadConn  *gorm.DB
	WriteConn *gorm.DB
}

func Connect() *SqlHandler {
	readConn, err := gorm.Open(GetDBConfig("read"))
	if err != nil {
		panic(err.Error())
	}

	writeConn, err := gorm.Open(GetDBConfig("write"))
	if err != nil {
		panic(err.Error())
	}

	sqlHandler := SqlHandler{
		ReadConn:  readConn,
		WriteConn: writeConn,
	}

	return &sqlHandler
}

func GetDBConfig(endpoint string) (gorm.Dialector, *gorm.Config) {
	USER := "myUser"
	PASS := "hoge1234"
	PROTOCOL := "tcp(" + endpoint + ":5050)"
	DBNAME := "myDB"
	OPTION := "charset=utf8&parseTime=True&loc=Local"

	mysqlConfig := mysql.Config{
		DSN: USER + ":" + PASS + "@" + PROTOCOL + "/" + DBNAME + "?" + OPTION,
	}

	config := &gorm.Config{}

	return mysql.New(mysqlConfig), config
}

func (handler *SqlHandler) Find(out interface{}, where ...interface{}) *gorm.DB {
	return handler.ReadConn.Find(out, where...)
}

func (handler *SqlHandler) Exec(sql string, values ...interface{}) *gorm.DB {
	return handler.WriteConn.Exec(sql, values...)
}

func (handler *SqlHandler) First(out interface{}, where ...interface{}) *gorm.DB {
	return handler.ReadConn.Find(out, where...)
}

func (handler *SqlHandler) Raw(sql string, values ...interface{}) *gorm.DB {
	return handler.WriteConn.Raw(sql, values...)
}

func (handler *SqlHandler) Create(value interface{}) *gorm.DB {
	return handler.WriteConn.Create(value)
}

func (handler *SqlHandler) Save(value interface{}) *gorm.DB {
	return handler.WriteConn.Save(value)
}

func (handler *SqlHandler) Delete(value interface{}) *gorm.DB {
	return handler.WriteConn.Delete(value)
}

func (handler *SqlHandler) Where(query interface{}, args ...interface{}) *gorm.DB {
	return handler.WriteConn.Where(query, args...)
}

Replication

is what

Replication (https://ja.wikipedia.org/wiki/%E3%83%AC%E3%83%97%E3%83%AA%E3%82%B1%E3%83%BC%E3%82 % B7% E3% 83% A7% E3% 83% B3)

Places of concern

For the parts that can be issued by query itself, such as func Exec and func Raw, there is a possibility of both read / write, and I am wondering which one to distribute. This time, I assigned it to write. As much as possible, users want to be able to use it without being aware of endpoints ...

in conclusion

I have decided to use CQRS, and I feel that SQL solid writing is likely to increase in the first place. The meaning of using Gorm is a little diminished, but I would like it when considering the mapping to Type as an ORM. I think it would be good to make only the mapping part by yourself! !!

reference

-Golang --Building Clean Architecture API with Echo and GORM

Recommended Posts

[Go] I want to separate endpoints by Read / Write to DB
I want to store DB information in list
I want to manage systemd by time zone! !!
I want to sell Mercari by scraping python
I want to write to a file with Python
I want to write in Python! (1) Code format check
I want to write in Python! (3) Utilize the mock
I want to save the photos sent by LINE to S3
I want to solve Sudoku (Sudoku)
I want Sphinx to be convenient and used by everyone
I didn't want to write the AWS key in the program
I want to read the html version of "OpenCV-Python Tutorials" OpenCV 3.1 version
I want to understand systemd roughly
I want to scrape images to learn
I want to do ○○ with Pandas
I want to copy yolo annotations
I want to debug with Python
I want to change the color by clicking the scatter point in matplotlib
I want to operate DB using Django's ORM from an external application
I want to be cursed by a pretty girl every time I sudo! !!
[Google Colab] I want to display multiple images side by side in tiles
[Question] I want to scrape a character string surrounded by unique tags!
I want to read CSV line by line while converting the field type (while displaying the progress bar) and process it.
I want to pin Spyder to the taskbar
I want to detect objects with OpenCV
I want to output to the console coolly
I want to print in a comprehension
I want to scrape them all together.
I want to handle the rhyme part1
I want to know how LINUX works!
I want to blog with Jupyter Notebook
Bind to class to read and write YAML
I want to handle the rhyme part3
I want to use jar from python
I want to build a Python environment
I want to use Linux on mac
I want to pip install with PythonAnywhere
I want to analyze logs with Python
I want to play with aws with python
I want to use IPython Qt Console
I want to display the progress bar
I want to make an automation program!
I want to embed Matplotlib in PySimpleGUI
I want to handle the rhyme part2
I want to develop Android apps on Android
I want CAPTCHA to say HIWAI words
I want to handle the rhyme part5
I want to handle the rhyme part4
Because I don't want to go out with people whose desktops are dirty
I want to write an element to a file with numpy and check it.