/
collections.go
77 lines (66 loc) · 2.74 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
70
71
72
73
74
75
76
77
package storagecollections
import (
"context"
storagepostgres "github.com/sdinsure/agent/pkg/storage/postgres"
"gorm.io/gorm"
"gorm.io/gorm/clause"
appmodelcollections "github.com/footprintai/restcol/pkg/models/collections"
appmodelprojects "github.com/footprintai/restcol/pkg/models/projects"
"github.com/footprintai/restcol/pkg/storage"
)
type CollectionCURD struct {
*storagepostgres.PostgresDb
}
func NewCollectionCURD(db *storagepostgres.PostgresDb) *CollectionCURD {
return &CollectionCURD{
PostgresDb: db,
}
}
func (c *CollectionCURD) AutoMigrate() error {
tables := []interface{}{
&appmodelcollections.ModelCollection{},
&appmodelcollections.ModelSchema{},
&appmodelcollections.ModelFieldSchema{},
}
return storage.WrapStorageError(c.PostgresDb.AutoMigrate(tables))
}
func (c *CollectionCURD) Write(ctx context.Context, tableName string, record *appmodelcollections.ModelCollection) error {
err := c.With(ctx, tableName).Clauses(clause.OnConflict{UpdateAll: true}).Create(record).Error
return storage.WrapStorageError(err)
}
func (c *CollectionCURD) Update(ctx context.Context, tableName string, record *appmodelcollections.ModelCollection) error {
err := c.With(ctx, tableName).Session(&gorm.Session{FullSaveAssociations: true}).Updates(record).Error
return storage.WrapStorageError(err)
}
func (c *CollectionCURD) GetLatestSchema(ctx context.Context, tableName string, cid appmodelcollections.CollectionID) (*appmodelcollections.ModelCollection, error) {
s := &appmodelcollections.ModelSchema{}
err := c.With(ctx, tableName).Where("model_collection_id = ?", cid.String()).Order("id desc").First(s).Error
if err != nil {
return nil, storage.WrapStorageError(err)
}
return c.Get(ctx, tableName, cid, s.ID)
}
func (c *CollectionCURD) ListByProjectID(ctx context.Context, tableName string, pid appmodelprojects.ProjectID) ([]*appmodelcollections.ModelCollection, error) {
var cs []*appmodelcollections.ModelCollection
err := c.With(ctx, tableName).
Preload("Schemas", func(db *gorm.DB) *gorm.DB {
return db.Order("id desc").Limit(1)
}).
Preload("Schemas.Fields").
Where("model_project_id = ?", pid.String()).Order("id desc").Find(&cs).Error
return cs, storage.WrapStorageError(err)
}
func (c *CollectionCURD) Get(ctx context.Context, tableName string, cid appmodelcollections.CollectionID, sid appmodelcollections.SchemaID) (*appmodelcollections.ModelCollection, error) {
record := &appmodelcollections.ModelCollection{}
err := c.With(ctx, tableName).
Preload("Schemas", func(db *gorm.DB) *gorm.DB {
return db.Where(&appmodelcollections.ModelSchema{ID: sid})
}).
Preload("Schemas.Fields").
Where("id = ?", cid.String()).
Find(record).Error
if err != nil {
return nil, storage.WrapStorageError(err)
}
return record, nil
}