-
Notifications
You must be signed in to change notification settings - Fork 3
/
models.go
77 lines (61 loc) · 1.89 KB
/
models.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package main
import (
"time"
"unicode/utf8"
"github.com/256dpi/fire"
"github.com/256dpi/fire/axe"
"github.com/256dpi/fire/coal"
"github.com/256dpi/fire/flame"
)
var catalog = coal.NewCatalog(&Item{})
var indexer = coal.NewIndexer()
func init() {
// add flame indexes
flame.AddApplicationIndexes(indexer)
flame.AddUserIndexes(indexer)
flame.AddTokenIndexes(indexer, true)
// add axe indexes
axe.AddJobIndexes(indexer, time.Minute)
// add item index
indexer.Add(&Item{}, false, 0, "Name")
// add background delete index
indexer.Add(&Item{}, false, time.Second, "Deleted")
// add unique create token index
indexer.Add(&Item{}, true, 0, "CreateToken")
}
// EnsureIndexes will ensure that the required indexes exist.
func EnsureIndexes(store *coal.Store) error {
// ensure model indexes
err := indexer.Ensure(store)
if err != nil {
return err
}
return nil
}
// Item represents a general item.
type Item struct {
coal.Base `json:"-" bson:",inline" coal:"items"`
Name string `json:"name"`
State bool `json:"state"`
Count int `json:"count"`
Created time.Time `json:"created-at" bson:"created_at" coal:"fire-created-timestamp"`
Updated time.Time `json:"updated-at" bson:"updated_at" coal:"fire-updated-timestamp"`
Deleted *time.Time `json:"deleted-at" bson:"deleted_at" coal:"fire-soft-delete"`
CreateToken string `json:"create-token" bson:"create_token" coal:"fire-idempotent-create"`
UpdateToken string `json:"update-token" bson:"update" coal:"fire-consistent-update"`
}
// Validate implements the fire.ValidatableModel interface.
func (i *Item) Validate() error {
// check name
if utf8.RuneCountInString(i.Name) < 1 {
return fire.E("missing name")
}
// check timestamps
if i.Created.IsZero() || i.Updated.IsZero() {
return fire.E("missing timestamp")
}
return nil
}
type count struct {
Item coal.ID `bson:"item_id"`
}