Modules
When GoAdmin engine is building, some modules are built. Such as: database module, user authentication module. And this chapter tells you how to reuse the modules.

Database Module

Database module can be used afrer the engine set the global config. For example:
1
import (
2
...
3
"github.com/GoAdminGroup/go-admin/engine"
4
...
5
)
6
7
func main() {
8
9
...
10
11
eng := engine.Default()
12
13
cfg := config.Config{
14
...
15
Databases: config.DatabaseList{
16
"default": {
17
Host: "127.0.0.1",
18
Port: "3306",
19
User: "root",
20
Pwd: "root",
21
Name: "godmin",
22
MaxIdleCon: 50,
23
MaxOpenCon: 150,
24
Driver: config.DriverMysql,
25
},
26
},
27
...
28
}
29
30
_ = eng.AddConfig(cfg). // Here the database module built.
31
AddPlugins(adminPlugin).
32
Use(r)
33
34
// get mysql connection
35
conn := eng.MysqlConnection()
36
37
// get mssql connection
38
conn := eng.MssqlConnection()
39
40
// get postgresql connection
41
conn := eng.PostgresqlConnection()
42
43
// get sqlite connection
44
conn := eng.SqliteConnection()
45
46
// Note: you get a pointer here which is point to the global database connection object.
47
// If you want to reuse in you table models file, then you should call before the .Use(r) method.
48
// Or it will panic. Like:
49
//
50
// _ = eng.AddConfig(cfg).
51
// ResolveMysqlConnection(tables.SetConn)
52
// AddPlugins(adminPlugin).
53
// Use(r)
54
//
55
// In your tables.go:
56
//
57
// var conn db.Connection
58
//
59
// func SetConn(c db.Connection) {
60
// conn = c
61
// }
62
//
63
// And then you call the conn in your table model file.
64
65
// get connection by setter function
66
eng.ResolveMysqlConnection(SetConn)
67
68
...
69
}
70
71
var globalConn db.Connection
72
73
func SetConn(conn db.Connection) {
74
globalConn = conn
75
}
Copied!
When you receive connection object, you can call the built-in sql helper methods:
1
import (
2
...
3
"github.com/GoAdminGroup/go-admin/modules/db"
4
"github.com/GoAdminGroup/go-admin/modules/db/dialect"
5
...
6
)
7
8
func main() {
9
10
// pass the global connection object, then call the helper methods
11
12
// query
13
db.WithDriver(globalConn).Table("users").Select("id", "name").First()
14
15
// update
16
db.WithDriver(globalConn).Table("users").Where("id", "=", 10).
17
Update(dialect.H{
18
"name": "jack",
19
})
20
21
// insert
22
db.WithDriver(globalConn).Table("users").
23
Insert(dialect.H{
24
"name": "jack",
25
})
26
27
// delete
28
db.WithDriver(globalConn).Table("users").Where("id", "=", 10).Delete()
29
30
// and so on...
31
}
Copied!
You can use APIs of the Connection too.
1
// Connection is a connection handler of database.
2
type Connection interface {
3
4
// Initialize
5
InitDB(cfg map[string]config.Database) Connection
6
7
// Get the driver name
8
Name() string
9
10
// Close the connection
11
Close() []error
12
13
// Get the default delimiter
14
GetDelimiter() string
15
16
// Get the sql.DB
17
GetDB(key string) *sql.DB
18
19
// Query with the default connection
20
Query(query string, args ...interface{}) ([]map[string]interface{}, error)
21
22
// Exec with the default connection
23
Exec(query string, args ...interface{}) (sql.Result, error)
24
25
// Query with the specify connection which is the key of Databases of config.Config.
26
QueryWithConnection(conn, query string, args ...interface{}) ([]map[string]interface{}, error)
27
28
// Exec with the specify connection which is the key of Databases of config.Config.
29
ExecWithConnection(conn, query string, args ...interface{}) (sql.Result, error)
30
31
// Transaction operations:
32
33
// begin the transaction
34
BeginTx() *sql.Tx
35
QueryWithTx(tx *sql.Tx, query string, args ...interface{}) ([]map[string]interface{}, error)
36
ExecWithTx(tx *sql.Tx, query string, args ...interface{}) (sql.Result, error)
37
BeginTxWithReadUncommitted() *sql.Tx
38
BeginTxWithReadCommitted() *sql.Tx
39
BeginTxWithRepeatableRead() *sql.Tx
40
BeginTxWithLevel(level sql.IsolationLevel) *sql.Tx
41
BeginTxWithReadUncommittedAndConnection(conn string) *sql.Tx
42
BeginTxWithReadCommittedAndConnection(conn string) *sql.Tx
43
BeginTxWithRepeatableReadAndConnection(conn string) *sql.Tx
44
BeginTxAndConnection(conn string) *sql.Tx
45
BeginTxWithLevelAndConnection(conn string, level sql.IsolationLevel) *sql.Tx
46
}
Copied!
Also you can use the orm, for example, `gorm
1
package main
2
3
import (
4
...
5
"github.com/jinzhu/gorm"
6
...
7
)
8
9
func initORM() {
10
// conn is the global connection object
11
orm, _ := gorm.Open("mysql", conn.GetDB("default"))
12
// See more about gorm: https://gorm.io
13
}
Copied!

User Authentication Module

When we write the page content, need to get the corresponding login user, and to validate its information, need to use user authentication module.
1
import (
2
...
3
adapter "github.com/GoAdminGroup/go-admin/adapter/gin"
4
"github.com/GoAdminGroup/go-admin/engine"
5
...
6
)
7
8
func main() {
9
10
...
11
12
eng := engine.Default()
13
14
cfg := config.Config{
15
...
16
}
17
18
if err := eng.AddConfig(cfg).
19
AddPlugins(adminPlugin, examplePlugin).
20
Use(r); err != nil {
21
panic(err)
22
}
23
24
r.GET("/admin", adapter.Content(func(ctx *gin.Context) (types.Panel, error) {
25
// get the auth user
26
user, _ := engine.User(ctx)
27
28
// Verify the permissions
29
if !user.CheckPermission("dashboard") {
30
return types.Panel{}, errors.New("no permission")
31
}
32
33
// Verify the roles
34
if !user.CheckRole("operator") {
35
return types.Panel{}, errors.New("no permission")
36
}
37
})
38
...
39
}
Copied!
Retrieve the auth user model in the data model functions:
1
import (
2
...
3
"github.com/GoAdminGroup/go-admin/modules/auth"
4
"github.com/GoAdminGroup/go-admin/plugins/admin/models"
5
...
6
)
7
8
func GetUserTable(ctx *context.Context) table.Table {
9
// retrieve the auth user.
10
user = auth.Auth(ctx)
11
}
Copied!
Last modified 1yr ago