-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.go
More file actions
120 lines (102 loc) · 2.7 KB
/
plugin.go
File metadata and controls
120 lines (102 loc) · 2.7 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"context"
"errors"
"github.com/nori-io/sql-gorm/internal/hook"
"github.com/jinzhu/gorm"
"github.com/nori-io/common/v3/config"
"github.com/nori-io/common/v3/logger"
"github.com/nori-io/common/v3/meta"
"github.com/nori-io/common/v3/plugin"
i "github.com/nori-io/interfaces/public/sql/gorm"
_ "github.com/jinzhu/gorm/dialects/mssql"
_ "github.com/jinzhu/gorm/dialects/mysql"
_ "github.com/jinzhu/gorm/dialects/postgres"
_ "github.com/jinzhu/gorm/dialects/sqlite"
)
type service struct {
db *gorm.DB
config *pluginConfig
logger logger.FieldLogger
}
type pluginConfig struct {
dsn string
dialect string
logMode bool
}
var (
Plugin plugin.Plugin = &service{}
dialects = [4]string{"mssql", "mysql", "postgres", "sqlite"}
)
func (p *service) Init(ctx context.Context, config config.Config, log logger.FieldLogger) error {
var isValidDialect bool
p.logger = log
p.config.logMode = config.Bool("sql.gorm.logMode", "log mode: true or false")()
p.config.dsn = config.String("sql.gorm.dsn", "database connection string")()
p.config.dialect = config.String("sql.gorm.dialect", "sql dialect: mssql, mysql, postgres, sqlite")()
for _, v := range dialects {
if v == p.config.dialect {
isValidDialect = true
}
}
if !isValidDialect {
return errors.New("Dialect is wrong. You should use on of sql dialects: mssql, mysql, postgres, sqlite")
}
return nil
}
func (p *service) Instance() interface{} {
return p.db
}
func (p *service) Meta() meta.Meta {
return meta.Data{
ID: meta.ID{
ID: "sql/gorm",
Version: "1.9.15",
},
Author: meta.Author{
Name: "Nori.io",
URI: "https://nori.io/",
},
Dependencies: []meta.Dependency{},
Description: meta.Description{
Name: "Nori: ORM GORM",
Description: "This plugin implements instance of ORM GORM",
},
Core: meta.Core{
VersionConstraint: "^0.2.0",
},
Interface: i.GormInterface,
License: []meta.License{
{
Title: "GPLv3",
Type: "GPLv3",
URI: "https://www.gnu.org/licenses/"},
},
Links: []meta.Link{},
Repository: meta.Repository{
Type: "git",
URI: "https://github.com/nori-io/sql-gorm",
},
Tags: []string{"orm", "gorm", "sql", "database", "db"},
}
}
func (p *service) Start(ctx context.Context, registry plugin.Registry) error {
var err error
p.db, err = gorm.Open(p.config.dialect, p.config.dsn)
if err != nil {
p.logger.Error(err.Error())
} else {
p.db.LogMode(p.config.logMode)
if p.config.logMode == true {
p.db.SetLogger(&hook.Logger{Origin: p.logger})
}
}
return err
}
func (p *service) Stop(ctx context.Context, registry plugin.Registry) error {
err := p.db.Close()
if err != nil {
p.logger.Error(err.Error())
}
return err
}