Introduce base model constraints and rework record filter constraints
This commit is contained in:
@@ -86,6 +86,11 @@ const (
|
||||
moduleChanged moduleChanges = 1
|
||||
moduleLabelsChanged moduleChanges = 2
|
||||
moduleFieldsChanged moduleChanges = 4
|
||||
|
||||
recordTable = "compose_record"
|
||||
recordFieldID = "ID"
|
||||
recordFieldModuleID = "moduleID"
|
||||
recordFieldNamespaceID = "namespaceID"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -1232,6 +1237,8 @@ func modulesToModelSet(dmm dalModelManager, ns *types.Namespace, mm ...*types.Mo
|
||||
|
||||
// @todo validate ident with connection's ident validator
|
||||
|
||||
model.Constraints = modelBaseConstraints(model, mod)
|
||||
|
||||
model.ConnectionID = connectionID
|
||||
out = append(out, model)
|
||||
|
||||
@@ -1259,6 +1266,21 @@ func modulesToModelSet(dmm dalModelManager, ns *types.Namespace, mm ...*types.Mo
|
||||
return
|
||||
}
|
||||
|
||||
func modelBaseConstraints(model *dal.Model, mod *types.Module) (out map[string][]any) {
|
||||
|
||||
// If we're writting to the default table apply additional constraints
|
||||
// @todo there should be more logic here, but for now this is what we had
|
||||
// elsewhere.
|
||||
if model.Ident == recordTable {
|
||||
out = map[string][]any{
|
||||
recordFieldModuleID: {mod.ID},
|
||||
recordFieldNamespaceID: {mod.NamespaceID},
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// ModuleToModel converts a module with fields to DAL model and attributes
|
||||
//
|
||||
// note: this function does not do any partition placeholder replacements
|
||||
|
||||
+1
-19
@@ -96,30 +96,12 @@ const (
|
||||
OperationTypeCreate OperationType = "create"
|
||||
OperationTypeUpdate OperationType = "update"
|
||||
OperationTypeDelete OperationType = "delete"
|
||||
|
||||
recordFieldID = "ID"
|
||||
recordFieldModuleID = "moduleID"
|
||||
recordFieldNamespaceID = "namespaceID"
|
||||
)
|
||||
|
||||
func (f RecordFilter) ToConstraintedFilter(c map[string][]any) filter.Filter {
|
||||
return filter.Generic(
|
||||
// combine constraints with namespace and module
|
||||
filter.WithConstraints(func() map[string][]any {
|
||||
if c == nil {
|
||||
c = make(map[string][]any)
|
||||
}
|
||||
|
||||
if f.ModuleID > 0 {
|
||||
c[recordFieldModuleID] = []any{f.ModuleID}
|
||||
}
|
||||
|
||||
if f.NamespaceID > 0 {
|
||||
c[recordFieldNamespaceID] = []any{f.NamespaceID}
|
||||
}
|
||||
|
||||
return c
|
||||
}()),
|
||||
filter.WithConstraints(c),
|
||||
filter.WithExpression(f.Query),
|
||||
filter.WithOrderBy(f.Sort),
|
||||
filter.WithLimit(f.Limit),
|
||||
|
||||
@@ -34,6 +34,8 @@ type (
|
||||
Attributes AttributeSet
|
||||
|
||||
Operations OperationSet
|
||||
|
||||
Constraints map[string][]any
|
||||
}
|
||||
ModelSet []*Model
|
||||
|
||||
|
||||
@@ -4,9 +4,10 @@ import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/cortezaproject/corteza-server/pkg/errors"
|
||||
"github.com/modern-go/reflect2"
|
||||
"strings"
|
||||
|
||||
"github.com/cortezaproject/corteza-server/pkg/dal"
|
||||
"github.com/cortezaproject/corteza-server/pkg/filter"
|
||||
@@ -252,7 +253,15 @@ func (d *model) searchSql(f filter.Filter) *goqu.SelectDataset {
|
||||
//}
|
||||
}
|
||||
|
||||
for ident, vv := range f.Constraints() {
|
||||
cc := f.Constraints()
|
||||
if d.model.Constraints != nil {
|
||||
for k, c := range d.model.Constraints {
|
||||
// Overwrite user-provided constraints as the system ones are more important
|
||||
cc[k] = c
|
||||
}
|
||||
}
|
||||
|
||||
for ident, vv := range cc {
|
||||
attr := d.model.Attributes.FindByIdent(ident)
|
||||
if attr == nil {
|
||||
return base.SetError(fmt.Errorf("unknown attribute %q used for constrant", ident))
|
||||
|
||||
Reference in New Issue
Block a user