-
Notifications
You must be signed in to change notification settings - Fork 4
/
db.go
36 lines (32 loc) · 807 Bytes
/
db.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
/*
Various database (gorm) helpers
*/
package core
import (
"github.com/jinzhu/gorm"
"github.com/phonkee/go-paginator"
)
/*
Initial setup of database connection
*/
func setupDB(DB *gorm.DB) {
// remove original functions to update timestamps, we will maintain that by ourself in models in
// BeforeCreate, BeforeSave methods
DB.Callback().Create().Remove("gorm:update_time_stamp")
}
/*
LimitQueryset sets limits to queryset and returns it
*/
func LimitQueryset(db *gorm.DB, p paginator.Paginator) *gorm.DB {
limit, offset := p.GetLimitOffset()
return db.Limit(limit).Offset(offset)
}
/*
CountQueryset performs count query and sets paginator count
*/
func CountQueryset(db *gorm.DB, p paginator.Paginator) *gorm.DB {
count := 0
db.Limit(0).Offset(0).Count(&(count))
p.Count(count)
return db
}