Documentation
¶
Overview ¶
Package entity defines the entity abstraction layer: configured tables, views, and procedures exposed to MCP tools, with aliases, field projection, masking, descriptions, role permissions, row-level security, and keys.
An Entity is a complete description of a Relation (Codd): attributes with domains and constraints, candidate keys, foreign keys, and role access. Registry holds an immutable set of entities keyed by logical name. RowPolicies attach a relalg.Predicate per role for row-level security, applied by rbac.
Index ¶
- Constants
- Variables
- func ActionNames() []string
- func ID(datasource, schema, name string) string
- func ReferencesOf(datasource, schema, local string) []string
- func ReferentialWrite(action string) bool
- type Action
- type Attribute
- type Cascade
- type Domain
- type Entity
- func (e Entity) AttributeByName(name string) (Attribute, bool)
- func (e Entity) CursorKey() []string
- func (e Entity) DatasourceName() string
- func (e Entity) IdentityKeys(enforced, inTransaction bool) [][]string
- func (e Entity) OrderedNames(set map[string]bool) []string
- func (e Entity) PrimaryKey() []string
- func (e Entity) Ref() relalg.RelationRef
- func (e Entity) References() []string
- func (e Entity) RelationKey() string
- type FieldAccess
- type FieldPermissions
- type ForeignKey
- type Index
- type Key
- type Kind
- type MCPFlags
- type Registry
- type Relationship
- type Resolved
- type RoleAccess
- type RowPolicies
Constants ¶
const ( KeyPartial = "partial" // a partial unique index (WHERE ...) KeyExpression = "expression" // an index on expressions, such as lower(email) KeyPrefix = "prefix" // a MySQL prefix index, such as email(10) KeyNullable = "nullable" // a nullable column: several rows may hold NULL )
Reasons a unique key does not identify a row.
const ( FKCascade = "cascade" FKSetNull = "set_null" FKSetDefault = "set_default" )
Referential actions of a foreign key that write the referencing rows.
Variables ¶
var Actions = []Action{ActionRead, ActionCreate, ActionUpdate, ActionDelete, ActionExecute, ActionAggregate}
Actions lists every action, in the order configuration names them.
var Cardinalities = []string{"one", "one-to-one", "belongs-to", "many", "one-to-many", "has-many"}
Cardinalities a relationship may declare: the first three name one target row, the others a list of them.
var ErrDuplicateEntity = errors.New("entity: duplicate name")
ErrDuplicateEntity is returned by NewRegistry when two entities share a name.
var ErrEmptyName = errors.New("entity: empty name")
ErrEmptyName is returned by NewRegistry when an entity has an empty name.
Functions ¶
func ActionNames ¶ added in v0.1.13
func ActionNames() []string
ActionNames lists the names of Actions, as configuration writes them.
func ID ¶ added in v0.1.12
ID is the canonical identity of a configured entity, its namespace path: datasource.schema.name, or datasource.name without a schema. Entity names, datasource names and schemas contain no dots.
func ReferencesOf ¶ added in v0.1.12
ReferencesOf lists the ways to refer to the entity named local in a datasource and schema, shortest first, like partially qualified SQL names: local, schema.local, datasource.local and datasource.schema.local, the last being its ID.
func ReferentialWrite ¶ added in v0.1.11
ReferentialWrite reports whether a referential action changes the referencing rows (cascade, set null or set default) rather than only checking them.
Types ¶
type Action ¶
type Action uint8
Action names a DML operation for role permissions.
const ( // ActionRead selects records. ActionRead Action = iota // ActionCreate inserts records. ActionCreate // ActionUpdate updates records. ActionUpdate // ActionDelete deletes records. ActionDelete // ActionExecute calls a stored procedure. ActionExecute // ActionAggregate groups/aggregate-queries records. ActionAggregate )
func ActionsFor ¶ added in v0.1.11
ActionsFor lists the actions applicable to an entity kind: execute for a procedure, otherwise the record actions.
func ParseAction ¶ added in v0.1.13
ParseAction is the action named name (read, create, update, delete, execute or aggregate); false when none is.
type Attribute ¶
type Attribute struct {
Name string
Alias string
Description string
Domain Domain
Excluded bool // true removes the column from all responses (field projection)
Mask string // optional mask rule name (see mask package)
}
Attribute is one column of a relation, with projection and masking controls.
type Cascade ¶ added in v0.1.11
type Cascade struct {
// Schema and Table name the referencing relation; Relation is its
// relation key and Entity the entity exposing it ("" when none does),
// both set at assembly.
Schema string
Table string
Relation string
Entity string
// Columns are the referenced columns of this relation; ForeignColumns
// the referencing columns of the other, in the same order.
Columns []string
ForeignColumns []string
OnDelete string
OnUpdate string
}
Cascade is a foreign key of another relation that writes its rows when rows of the referenced relation change.
type Domain ¶
type Domain struct {
Type string
Nullable bool
// Default reports that the database supplies a value when an insert
// omits the column (a default expression, a sequence, an identity BY
// DEFAULT or auto_increment).
Default bool
// Generated reports that the database computes the value (a generated
// column or an identity ALWAYS): the column cannot be written.
Generated bool
// AutoIncrement reports a value taken from a sequence on insert.
AutoIncrement bool
}
Domain is a column's value domain as introspected from the database.
type Entity ¶
type Entity struct {
// Name identifies the entity: its canonical ID (see ID) when Local is
// set, which every reference inside the gateway uses.
Name string
// Local is the configured name, unique within its datasource and schema;
// empty when Name is not namespaced.
Local string
Source string
DataSource string
Schema string
Description string
Kind Kind
Attributes []Attribute
Keys []Key
ForeignKeys []ForeignKey
// Indexes are the table's indexes as introspection reports them, for
// administrators to see; the database plans access paths, not the
// gateway. The unique ones are also Keys.
Indexes []Index
Role RoleAccess
FieldAccess FieldAccess
MCP MCPFlags
RowPolicies RowPolicies
// TenantPolicy is ANDed for every principal and never merged across
// grants; nil means the entity has no tenant boundary.
TenantPolicy relalg.Predicate
Relations []Relationship
// Derived marks a relation whose data comes from other relations (a view,
// a materialized view or a foreign table): a write to any table of its
// datasource may change it.
Derived bool
// Relation identifies the physical relation (set at assembly, see
// RelationKey); at most one entity exposes a relation.
Relation string
// Affects lists the entities a procedure writes; empty means unknown.
Affects []string
// Cascades are the foreign keys of other relations whose referential
// actions write their rows when this entity's rows are deleted or their
// referenced columns updated (I-6).
Cascades []Cascade
// AllowCascade admits such writes without checking the caller's
// permissions on the cascaded entities.
AllowCascade bool
// SideEffects reports triggers or rules on the relation: a write may
// change other relations of the datasource.
SideEffects bool
// Params is the ordered list of formal parameter names for a KindProcedure
// entity. execute_entity binds a caller's named args to positional CALL
// placeholders in this exact order; a stored procedure whose params are not
// declared cannot be executed (fail-closed) since positional binding would
// otherwise be guesswork.
Params []string
}
Entity is a complete description of an exposed relation.
func (Entity) AttributeByName ¶
AttributeByName returns the attribute with the given logical name and a found flag. It matches Name or Alias.
func (Entity) CursorKey ¶ added in v0.1.11
CursorKey returns the key keyset pagination orders by: the primary key, otherwise the first identity key; nil when the entity has none.
func (Entity) DatasourceName ¶ added in v0.1.11
DatasourceName is the entity's datasource, "default" when unset.
func (Entity) IdentityKeys ¶ added in v0.1.11
IdentityKeys returns the column sets that identify at most one row, primary key first. enforced drops declared keys, as a write needs; inTransaction drops deferrable keys.
func (Entity) OrderedNames ¶ added in v0.1.11
OrderedNames returns the attribute names in set, in attribute order.
func (Entity) PrimaryKey ¶
PrimaryKey returns the columns of the primary key, or nil if none is declared.
func (Entity) Ref ¶ added in v0.1.13
func (e Entity) Ref() relalg.RelationRef
Ref names the relation the entity reads or writes, for the IR.
func (Entity) References ¶ added in v0.1.12
References lists the ways to refer to the entity, shortest first (see ReferencesOf); an entity without a local name is referred to by Name only.
func (Entity) RelationKey ¶ added in v0.1.11
RelationKey identifies the physical relation the entity exposes: Relation when assembly resolved it, otherwise its configured datasource, schema and source.
type FieldAccess ¶
type FieldAccess map[string]FieldPermissions
FieldAccess maps role names to field-level permissions. Absence of a role preserves entity-level authorization behavior for backwards compatibility.
type FieldPermissions ¶
FieldPermissions is one role's field-level access. Read applies to projections, filters, grouping, and aggregate inputs; Write applies to create values and update assignments.
type ForeignKey ¶
type ForeignKey struct {
Name string
Columns []string
// RefSchema is the referenced relation's schema; it may differ from the
// referencing table's.
RefSchema string
RefRelation string
RefColumns []string
// OnDelete and OnUpdate are the referential actions: one of the FK*
// constants, empty when the database reports none.
OnDelete string
OnUpdate string
}
ForeignKey declares referential integrity to another relation.
type Index ¶ added in v0.1.12
type Index struct {
Name string
// Method is the database's name for the index structure, lower-cased:
// for example btree, hash, gin, gist, brin or spgist on PostgreSQL, and
// btree, hash, fulltext or spatial on MySQL and OceanBase.
Method string
// Parts are the key parts in order: a column, a column prefix such as
// title(20), or an expression as the database prints it. Parts the
// database does not report (OceanBase's full-text and spatial indexes)
// are left out.
Parts []string
Unique bool
Primary bool
// Where is the predicate of a partial index.
Where string
}
Index is an index of a table in the database's own terms: databases support different structures and forms of index.
type Key ¶
type Key struct {
Name string
Columns []string
Primary bool
// Declared marks a key the database does not guarantee, configured on a
// view or foreign table: it serves reads, never the write-safety check.
Declared bool
// Deferrable reports a constraint checked at commit: inside a
// transaction it may be violated, so it identifies no row there.
Deferrable bool
// Reason is empty for a key that identifies at most one row.
Reason string
}
Key is a primary or unique key. A unique key that cannot identify a row is kept for display, with Reason saying why.
type MCPFlags ¶
type MCPFlags struct {
DMLTools bool // expose the seven DML tools for this entity
CustomTool bool // register a stored procedure as a named tool
TrustedProcedure bool // procedure passed explicit DBA cost/safety review
}
MCPFlags controls how an entity participates in MCP.
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry is an immutable set of entities keyed by Name, also found by any of their References. It holds no per-request state and is safe for concurrent use (invariant I9).
func NewRegistry ¶
NewRegistry builds a Registry from entities, rejecting empty names and duplicates. It copies the slice so callers cannot mutate the registry.
func (*Registry) Match ¶ added in v0.1.12
Match returns the entities ref may name: the entity whose Name it is, else every entity it is a reference of, like a partially qualified SQL name.
type Relationship ¶
type Relationship struct {
Name string
Target string
// Cardinality is one of Cardinalities: how many target rows a row has.
Cardinality string
JoinOn map[string]string // this field -> target field
}
Relationship describes a link to another entity for nested expansion.
func (Relationship) Single ¶ added in v0.1.13
func (r Relationship) Single() bool
Single reports whether the relationship names at most one target row, expanded as one object rather than a list.
type Resolved ¶
Resolved is a request-time view of an entity with field projection applied (excluded attributes removed). RBAC further restricts Attributes per role.
type RoleAccess ¶
RoleAccess maps each action to the roles allowed to perform it.
type RowPolicies ¶
RowPolicies maps a role name to a row-level filter predicate. The rbac package ANDs the role's predicate with the request predicate.