-
Notifications
You must be signed in to change notification settings - Fork 5
/
document.go
47 lines (37 loc) · 866 Bytes
/
document.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
package colt
import (
"go.mongodb.org/mongo-driver/bson/primitive"
"time"
)
type Document interface {
SetID(id string)
GetID() string
NewID() string
//CastID(id interface{}) (interface{}, error)
}
type Doc struct {
ID string `bson:"_id,omitempty" json:"_id,omitempty"`
}
func (doc *Doc) NewID() string {
return primitive.NewObjectID().Hex()
}
func (doc *Doc) SetID(id string) {
doc.ID = id
}
func (doc *Doc) GetID() string {
return doc.ID
}
type DocWithTimestamps struct {
Doc `bson:",inline"`
CreatedAt time.Time `json:"created_at" bson:"created_at"`
UpdatedAt *time.Time `json:"updated_at,omitempty" bson:"updated_at,omitempty"`
}
func (doc *DocWithTimestamps) BeforeInsert() error {
doc.CreatedAt = time.Now()
return nil
}
func (doc *DocWithTimestamps) BeforeUpdate() error {
now := time.Now()
doc.UpdatedAt = &now
return nil
}