Documentation
¶
Overview ¶
Package schema resolves the database mapping of proto resources: tables, identifier columns, parent joins and singleton children. It is the single source of truth shared by the model, postgres and rpc generators.
Index ¶
- Constants
- func Columns(bindings []ColumnBinding) []string
- func SearchFieldExpression(message *protogen.Message, path, columnPrefix string) (string, error)
- func SetFilesRegistry(registry *protoregistry.Files)
- type ColumnBinding
- type Join
- type JoinCondition
- type JoinField
- type JoinQuery
- type JoinTarget
- type SearchDoc
- type SingletonChild
- type SnippetField
- type Table
Constants ¶
const SearchArrayToStringFunction = "core_array_to_string"
SearchArrayToStringFunction is an IMMUTABLE wrapper around array_to_string (which is only STABLE and therefore unusable in generated columns) that migrations must declare alongside array-bearing search documents:
CREATE OR REPLACE FUNCTION core_array_to_string(text[], text) RETURNS text LANGUAGE sql IMMUTABLE PARALLEL SAFE AS $$ SELECT array_to_string($1, $2) $$;
const SearchDocumentColumn = "search_document"
SearchDocumentColumn is the database column holding a searchable resource's tsvector document. Migrations must declare it as a stored generated column whose expression matches the codegen-emitted SearchDocumentExpression.
const SearchPhoneNumberTokensFunction = "core_phone_number_tokens"
SearchPhoneNumberTokensFunction is an IMMUTABLE function emitting, for each space-separated phone number, every suffix (length >= 3) of its digits-only form — so any fragment of a number prefix-matches a token. Migrations using SPLIT_PHONE_NUMBER search fields must declare it:
CREATE OR REPLACE FUNCTION core_phone_number_tokens(text) RETURNS text LANGUAGE sql IMMUTABLE PARALLEL SAFE AS $$ SELECT string_agg(substr(d.digits, s.pos), ' ') FROM unnest(string_to_array($1, ' ')) AS e(elem) CROSS JOIN LATERAL (SELECT regexp_replace(e.elem, '[^0-9]', '', 'g') AS digits) d CROSS JOIN LATERAL generate_series(1, length(d.digits) - 2) AS s(pos) $$;
Variables ¶
This section is empty.
Functions ¶
func Columns ¶
func Columns(bindings []ColumnBinding) []string
Columns returns the column of each binding.
func SearchFieldExpression ¶
SearchFieldExpression resolves a search field path to the raw text SQL expression headlined into snippets, with every column reference qualified by the given prefix (e.g. "contact_activity_event."), for use inside queries that join other tables where bare column names would be ambiguous. Unlike the search document expression, JSONB message/array terminals extract only their string leaf values (space-joined) rather than raw JSON text — ts_headline fragments must be human-readable. jsonb_path_query is a set-returning function, unusable in the stored generated column, but snippets are computed per query so it is fine here.
func SetFilesRegistry ¶
func SetFilesRegistry(registry *protoregistry.Files)
SetFilesRegistry provides the file registry backing query-join filter transpilation. It must be set once before joins are resolved.
Types ¶
type ColumnBinding ¶
type ColumnBinding struct {
// Variable is the snake_case pattern variable, e.g. "shelf".
Variable string
// Column is the database column holding the variable's ID, e.g. "shelf_id".
Column string
// resource; non-shared variables are stored as nullable columns.
Shared bool
}
ColumnBinding maps one resource name pattern variable to its database column.
func ColumnBindings ¶
func ColumnBindings(pattern *resource.ParsedPattern, modelOpts *modelpb.ModelOpts) ([]ColumnBinding, error)
ColumnBindings returns the database column bound to each variable of the pattern. The id_column_name override applies to the resource's own identifier — the final pattern variable — and is invalid on singletons, which have no identifier of their own.
func UnionColumnBindings ¶
func UnionColumnBindings(parsedResource *resource.ParsedResource, modelOpts *modelpb.ModelOpts) ([]ColumnBinding, error)
UnionColumnBindings returns the database column bound to each variable of the union of the resource's patterns. Variables shared by every pattern map to non-nullable columns; pattern-specific variables map to nullable columns.
func (ColumnBinding) GoFieldName ¶
func (b ColumnBinding) GoFieldName() string
GoFieldName returns the Go struct field name of the binding, e.g. "ShelfID".
type Join ¶
type Join struct {
// Key identifies the join source: the ancestor resource type, or the
// reference/anchor field name.
Key string
Table Table
// Alias is the table alias the join is emitted under.
Alias string
// Left is true when the join is emitted as a LEFT JOIN: reference joins
// on nullable fields, and every query join.
Left bool
Conditions []JoinCondition
Fields []JoinField
// Query marks a descendant join: emitted as a LEFT JOIN LATERAL
// selecting at most one correlated child row.
Query *JoinQuery
}
Join groups all joined fields of a message against one join source.
type JoinCondition ¶
type JoinCondition struct {
// SourceColumn is the column on the joined table.
SourceColumn string
// ChildExpr is a printf template with a single %s placeholder for the
// child table qualifier, e.g. "%s.organization_id" or
// "split_part(%s.active_revision, '/', 12)".
ChildExpr string
}
JoinCondition equates a column on the joined table with an expression on the child table.
func (JoinCondition) Render ¶
func (c JoinCondition) Render(alias, childTable string) string
Render returns the SQL condition, qualifying both sides.
type JoinField ¶
type JoinField struct {
// Alias is the column alias on the child, i.e. the proto field name.
Alias string
// Column is the backing column on the joined table.
Column string
}
JoinField is a child column populated from a joined table column.
type JoinQuery ¶
JoinQuery is the resolved SQL of a query join's row selection over the child table. Filter, OrderBy and NameExpr are qualified by Inner, the alias the child table is emitted under inside the lateral subquery: its own bare table name.
type JoinTarget ¶
type JoinTarget struct {
Table Table
// Alias is the table alias the join is emitted under. Ancestor joins use
// the table name; reference joins use the reference field name and query
// joins their anchor field name, so two joins against the same table
// cannot collide.
Alias string
Column string
Nullable bool
// Ancestor is true for ancestor joins, whose fields must mirror the
// source column's nullability.
Ancestor bool
}
JoinTarget is the joined table and column backing a single joined field.
func ResolveJoin ¶
func ResolveJoin(message *protogen.Message, field *protogen.Field, join *modelpb.Join) (*JoinTarget, error)
ResolveJoin resolves the joined table, alias and column of a join-annotated field.
type SearchDoc ¶
type SearchDoc struct {
// Expression is the SQL expression composing the tsvector document.
Expression string
// SnippetFields are the fields producing highlighted snippets in Search responses.
SnippetFields []SnippetField
}
SearchDoc holds the resolved search document of a resource.
type SingletonChild ¶
type SingletonChild struct {
Resource *resource.ParsedResource
// Pattern is the child's singleton pattern parented by the resource it was
// resolved against. Its variables are exactly the parent's identifiers.
Pattern *resource.ParsedPattern
Message *protogen.Message
ModelOpts *modelpb.ModelOpts
}
SingletonChild is a persisted singleton child resource, created and deleted alongside its parent.
func SingletonChildren ¶
func SingletonChildren(parentMessage *protogen.Message, parent *resource.ParsedResource) ([]SingletonChild, error)
SingletonChildren returns the persisted singleton children of a resource. Children without model options are not persisted and are skipped; any other resolution failure — or a soft-deletability mismatch with the parent — is an error.
func (SingletonChild) Table ¶
func (c SingletonChild) Table() Table
Table resolves the child's table.
type SnippetField ¶
type SnippetField struct {
// Path is the resource field path, e.g. "biography".
Path string
// Expression is the raw text SQL expression to headline.
Expression string
// Weight is the field's tsvector weight letter (A-D).
Weight string
}
SnippetField is one field headlined into a search-result snippet.
type Table ¶
Table identifies a database table, optionally schema-qualified.
func TableOf ¶
func TableOf(parsedResource *resource.ParsedResource, modelOpts *modelpb.ModelOpts) Table
TableOf resolves the table of a resource from its model options.
func (Table) Qualified ¶
Qualified returns the schema-qualified table name, or the bare name when no schema is set.
func (Table) SchemaOrPublic ¶
SchemaOrPublic returns the table's schema, defaulting to "public".