/
doc.go
66 lines (47 loc) · 1.67 KB
/
doc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
Package otgorm provides gorm with opentracing.
For documentation about gorm usage, see https://gorm.io/index.html
Integration
package otgorm exports the configuration in the following format:
gorm:
default:
database: mysql
dsn: root@tcp(127.0.0.1:3306)/app?charset=utf8mb4&parseTime=True&loc=Local
Add the gorm dependency to core:
var c *core.C = core.New()
c.Provide(otgorm.Providers())
Then you can invoke gorm from the application.
c.Invoke(func(client *gorm.DB) {
// use client
})
The database types you can specify in configs are "mysql", "sqlite" and
"clickhouse". Other database types can be added by injecting the otgorm.Drivers
type to the dependency graph.
For example, if we want to use postgres:
var c *core.C = core.New()
c.Provide(otgorm.Providers())
c.Provide(di.Deps{func() otgorm.Drivers {
return otgorm.Drivers{
"mysql": mysql.Open,
"sqlite": sqlite.Open,
"clickhouse": clickhouse.Open,
"postgres": postgres.Open,
}
}}
Sometimes there are valid reasons to connect to more than one mysql server.
Inject otgorm.Maker to factory a *gorm.DB with a specific configuration entry.
c.Invoke(function(maker otgorm.Maker) {
client, err := maker.Make("default")
// do something with client
})
Migration and Seeding
package otgorm comes with migration and seeding support. Other modules can
register migration and seeding that are to be run by the command included in
this package.
To invoke the command, add the module to core first:
c.AddModuleFunc(otgorm.New)
Then you can migrate the database by running:
go run main.go database migrate
See examples to learn more.
*/
package otgorm