-
Notifications
You must be signed in to change notification settings - Fork 23
/
datastore.go
54 lines (42 loc) · 1.33 KB
/
datastore.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
package datastore
import (
"xorm.io/xorm"
"github.com/mayswind/ezbookkeeping/pkg/core"
"github.com/mayswind/ezbookkeeping/pkg/errs"
)
// DataStore represents a data storage containing a series of database shards
type DataStore struct {
databases []*Database
}
// Choose returns a database instance by sharding key
func (s *DataStore) Choose(key int64) *Database {
return s.databases[0]
}
// Query returns a new database session in a specific database by sharding key
func (s *DataStore) Query(c *core.Context, key int64) *xorm.Session {
return s.Choose(key).NewSession(c)
}
// DoTransaction runs a new database transaction in a specific database by sharding key
func (s *DataStore) DoTransaction(key int64, c *core.Context, fn func(sess *xorm.Session) error) (err error) {
return s.Choose(key).DoTransaction(c, fn)
}
// SyncStructs updates database structs by database models
func (s *DataStore) SyncStructs(beans ...any) error {
var err error
for i := 0; i < len(s.databases); i++ {
err = s.databases[i].engineGroup.Sync2(beans...)
if err != nil {
return err
}
}
return err
}
// NewDataStore returns a new data storage by a series of database
func NewDataStore(databases ...*Database) (*DataStore, error) {
if len(databases) < 1 {
return nil, errs.ErrDatabaseIsNull
}
return &DataStore{
databases: databases,
}, nil
}