-
Notifications
You must be signed in to change notification settings - Fork 0
/
collections.go
69 lines (56 loc) · 1.78 KB
/
collections.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
package modelcollections
import (
"database/sql/driver"
"errors"
"time"
"gorm.io/gorm"
apppb "github.com/footprintai/restcol/api/pb"
modelprojects "github.com/footprintai/restcol/pkg/models/projects"
)
type ModelCollection struct {
ID CollectionID `gorm:"column:id;primarykey;type:string;"`
Type ModelCollectionType `gorm:"column:type;type:int;"`
// Summary is a summary for the collections
Summary string `gorm:"column:summary"`
CreatedAt time.Time `gorm:"column:created_at"`
UpdatedAt time.Time `gorm:"column:updated_at"`
DeletedAt gorm.DeletedAt `gorm:"column:deleted_at"`
Schemas []*ModelSchema `gorm:"constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"` // associated to many schemes
ModelProjectID modelprojects.ProjectID // foreign key to model project
ModelProject modelprojects.ModelProject
}
func NewModelCollection(
projectId modelprojects.ProjectID,
id CollectionID,
t apppb.CollectionType,
summary string,
schemas []*ModelSchema,
) ModelCollection {
return ModelCollection{
ID: id,
Type: ModelCollectionType(t),
Summary: summary,
Schemas: schemas,
ModelProjectID: projectId,
}
}
func (m ModelCollection) TableName() string {
return "restcol-collections"
}
type ModelCollectionType apppb.CollectionType
func (m ModelCollectionType) Proto() apppb.CollectionType {
return apppb.CollectionType(m)
}
func (m ModelCollectionType) Number() int {
return int(apppb.CollectionType(m).Number())
}
func (m ModelCollectionType) Value() (driver.Value, error) {
return m.Number(), nil
}
func (m *ModelCollectionType) Scan(in any) error {
if int64Val, isInt64 := in.(int64); isInt64 {
(*m) = ModelCollectionType(apppb.CollectionType(int64Val))
return nil
}
return errors.New("shema: require int64")
}