Skip to content

Instantly share code, notes, and snippets.

@manakuro
Created December 10, 2021 04:44
Show Gist options
  • Save manakuro/f3a8005f971fb1abafe216dc3377a02a to your computer and use it in GitHub Desktop.
Save manakuro/f3a8005f971fb1abafe216dc3377a02a to your computer and use it in GitHub Desktop.
package datastore
import (
"golang-clean-architecture-ent-gqlgen/config"
"golang-clean-architecture-ent-gqlgen/ent"
"entgo.io/ent/dialect"
"github.com/go-sql-driver/mysql"
)
// New returns data source name
func New() string {
mc := mysql.Config{
User: config.C.Database.User,
Passwd: config.C.Database.Password,
Net: config.C.Database.Net,
Addr: config.C.Database.Addr,
DBName: config.C.Database.DBName,
AllowNativePasswords: config.C.Database.AllowNativePasswords,
Params: map[string]string{
"parseTime": config.C.Database.Params.ParseTime,
"charset": config.C.Database.Params.Charset,
"loc": config.C.Database.Params.Loc,
},
}
return mc.FormatDSN()
}
// NewClient returns an orm client
func NewClient() (*ent.Client, error) {
var entOptions []ent.Option
entOptions = append(entOptions, ent.Debug())
d := New()
return ent.Open(dialect.MySQL, d, entOptions...)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment