-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollection.go
62 lines (54 loc) · 2.11 KB
/
collection.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
package condition
import (
"github.com/elliotchance/pie/v2"
"github.com/FrancoLiberali/cql/model"
)
type Collection[TObject model.Model, TAttribute model.Model] struct {
name string
t1Field string
t2Field string
}
// Preload collection of models
//
// nestedPreloads can be used to preload relations of the models inside the collection
func (collection Collection[TObject, TAttribute]) Preload(nestedPreloads ...JoinCondition[TAttribute]) Condition[TObject] {
return NewCollectionPreloadCondition[TObject, TAttribute](collection.name, nestedPreloads)
}
// Any generates a condition that is true if at least one model in the collection fulfills the conditions
func (collection Collection[TObject, TAttribute]) Any(
firstCondition WhereCondition[TAttribute],
conditions ...WhereCondition[TAttribute],
) WhereCondition[TObject] {
return newExistsCondition[TObject, TAttribute](firstCondition, conditions, collection.name, collection.t1Field, collection.t2Field)
}
// None generates a condition that is true if no model in the collection fulfills the conditions
func (collection Collection[TObject, TAttribute]) None(
firstCondition WhereCondition[TAttribute],
conditions ...WhereCondition[TAttribute],
) WhereCondition[TObject] {
return Not[TObject](
newExistsCondition[TObject, TAttribute](firstCondition, conditions, collection.name, collection.t1Field, collection.t2Field),
)
}
// All generates a condition that is true if all models in the collection fulfill the conditions (or is empty)
func (collection Collection[TObject, TAttribute]) All(
firstCondition WhereCondition[TAttribute],
conditions ...WhereCondition[TAttribute],
) WhereCondition[TObject] {
return Not[TObject](
newExistsCondition[TObject, TAttribute](
Not[TAttribute](
pie.Unshift(conditions, firstCondition)...,
),
[]WhereCondition[TAttribute]{},
collection.name, collection.t1Field, collection.t2Field,
),
)
}
func NewCollection[TObject model.Model, TAttribute model.Model](name, t1Field, t2Field string) Collection[TObject, TAttribute] {
return Collection[TObject, TAttribute]{
name: name,
t1Field: t1Field,
t2Field: t2Field,
}
}