This repository has been archived by the owner on Nov 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collection_items.go
106 lines (93 loc) · 2.72 KB
/
collection_items.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
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
package core
import (
"github.com/datatogether/sql_datastore"
"github.com/ipfs/go-datastore"
"github.com/ipfs/go-datastore/query"
)
// ItemCount gets the number of items in the collection
func (c *Collection) ItemCount(store datastore.Datastore) (count int, err error) {
if sqls, ok := store.(*sql_datastore.Datastore); ok {
row := sqls.DB.QueryRow(qCollectionLength, c.Id)
err = row.Scan(&count)
return
}
// TODO - untested code :(
res, err := store.Query(query.Query{
Prefix: c.Key().String(),
KeysOnly: true,
})
if err != nil {
return 0, err
}
for r := range res.Next() {
if r.Error != nil {
return 0, err
}
if _, ok := r.Value.(*CollectionItem); ok {
count++
}
}
return
}
// SaveItems saves a slice of items to the collection.
// It's up to you to ensure that the "index" param doesn't get all messed up.
// TODO - validate / automate the Index param?
func (c *Collection) SaveItems(store datastore.Datastore, items []*CollectionItem) error {
for _, item := range items {
item.collectionId = c.Id
if err := item.Save(store); err != nil {
return err
}
}
return nil
}
// DeleteItems removes a given list of items from the collection
func (c *Collection) DeleteItems(store datastore.Datastore, items []*CollectionItem) error {
for _, item := range items {
item.collectionId = c.Id
if err := item.Delete(store); err != nil {
return err
}
}
return nil
}
// ReadItems reads a bounded set of items from the collection
// the orderby param currently only supports SQL-style input of a single proprty, eg: "index" or "index DESC"
func (c *Collection) ReadItems(store datastore.Datastore, orderby string, limit, offset int) (items []*CollectionItem, err error) {
items = make([]*CollectionItem, limit)
res, err := store.Query(query.Query{
Limit: limit,
Offset: offset,
// Keeping in mind that CollectionItem keys take the form /Collection:[id]/CollectionItem:[id]
// and Collections have the key /Collection:[id], the Collection key is the prefix for looking up keys
Prefix: c.Key().String(),
Filters: []query.Filter{
// Pass in a Filter Type to specify that results must be of type CollectionItem
// In abstract terms this combined with the Prefix query param amounts to querying:
// /Collection:[id]/CollectionItem:*
sql_datastore.FilterKeyTypeEq(CollectionItem{}.DatastoreType()),
},
Orders: []query.Order{
query.OrderByValue{
TypedOrder: sql_datastore.OrderBy(orderby),
},
},
})
if err != nil {
return nil, err
}
i := 0
for r := range res.Next() {
if r.Error != nil {
return nil, r.Error
}
c, ok := r.Value.(*CollectionItem)
if !ok {
return nil, ErrInvalidResponse
}
items[i] = c
i++
}
// fmt.Println(items)
return items[:i], nil
}