Add refs to DAL models constructed from compose modules
This commit is contained in:
@@ -1223,7 +1223,7 @@ func modulesToModelSet(dmm dalModelManager, ns *types.Namespace, mm ...*types.Mo
|
||||
}
|
||||
|
||||
// convert each module to model
|
||||
model, err = ModuleToModel(mod, conn.Config.ModelIdent)
|
||||
model, err = ModuleToModel(ns, mod, conn.Config.ModelIdent)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -1284,7 +1284,7 @@ func modelBaseConstraints(model *dal.Model, mod *types.Module) (out map[string][
|
||||
// ModuleToModel converts a module with fields to DAL model and attributes
|
||||
//
|
||||
// note: this function does not do any partition placeholder replacements
|
||||
func ModuleToModel(mod *types.Module, inhIdent string) (model *dal.Model, err error) {
|
||||
func ModuleToModel(ns *types.Namespace, mod *types.Module, inhIdent string) (model *dal.Model, err error) {
|
||||
var (
|
||||
attrAux dal.AttributeSet
|
||||
)
|
||||
@@ -1304,6 +1304,22 @@ func ModuleToModel(mod *types.Module, inhIdent string) (model *dal.Model, err er
|
||||
model.Ident = inhIdent
|
||||
}
|
||||
|
||||
// Refs for lookups
|
||||
var (
|
||||
nsSlug = ""
|
||||
nsID = uint64(0)
|
||||
)
|
||||
if ns != nil {
|
||||
nsSlug = ns.Slug
|
||||
nsID = ns.ID
|
||||
}
|
||||
model.Refs = map[string]any{
|
||||
"module": mod.Handle,
|
||||
"moduleID": mod.ID,
|
||||
"namespace": nsSlug,
|
||||
"namespaceID": nsID,
|
||||
}
|
||||
|
||||
// Convert user-defined fields to attributes
|
||||
attrAux, err = moduleFieldsToAttributes(mod)
|
||||
if err != nil {
|
||||
|
||||
@@ -306,13 +306,13 @@ func TestModuleToModel(t *testing.T) {
|
||||
)
|
||||
|
||||
t.Log("ident on DAL config not set, use ident from connection config")
|
||||
model, err = ModuleToModel(m, "ident-from-conn-config")
|
||||
model, err = ModuleToModel(nil, m, "ident-from-conn-config")
|
||||
req.NoError(err)
|
||||
req.Equal("ident-from-conn-config", model.Ident)
|
||||
|
||||
t.Log("explicit ident in module's DAL config should override the handle")
|
||||
m.Config.DAL.Ident = "explicit-ident"
|
||||
model, err = ModuleToModel(m, "ident-from-conn-config")
|
||||
model, err = ModuleToModel(nil, m, "ident-from-conn-config")
|
||||
req.NoError(err)
|
||||
req.Equal("explicit-ident", model.Ident)
|
||||
}
|
||||
|
||||
@@ -2,9 +2,10 @@ package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
|
||||
"github.com/cortezaproject/corteza-server/compose/service"
|
||||
"github.com/cortezaproject/corteza-server/pkg/dal"
|
||||
"strconv"
|
||||
|
||||
"github.com/cortezaproject/corteza-server/compose/types"
|
||||
"github.com/cortezaproject/corteza-server/pkg/envoy/resource"
|
||||
@@ -126,9 +127,11 @@ func (n *composeModule) Encode(ctx context.Context, pl *payload) (err error) {
|
||||
res.ID = NextID()
|
||||
}
|
||||
|
||||
res.NamespaceID = n.relNS.ID
|
||||
ns := n.relNS
|
||||
|
||||
res.NamespaceID = ns.ID
|
||||
if res.NamespaceID <= 0 {
|
||||
ns := resource.FindComposeNamespace(pl.state.ParentResources, n.res.RefNs.Identifiers)
|
||||
ns = resource.FindComposeNamespace(pl.state.ParentResources, n.res.RefNs.Identifiers)
|
||||
res.NamespaceID = ns.ID
|
||||
}
|
||||
if res.NamespaceID <= 0 {
|
||||
@@ -296,7 +299,7 @@ func (n *composeModule) Encode(ctx context.Context, pl *payload) (err error) {
|
||||
|
||||
var model *dal.Model
|
||||
// convert module to model and assume compose_record for default ident
|
||||
if model, err = service.ModuleToModel(res, "compose_record"); err != nil {
|
||||
if model, err = service.ModuleToModel(ns, res, "compose_record"); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user