inline

package
v0.6.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 3, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package inline generates VALUES clauses for inlining metadata into SQL functions.

Overview

Rather than querying database tables for closure and userset metadata during permission checks, Melange inlines this data directly into generated functions as VALUES clauses. This eliminates JOINs and improves query performance.

Inline Data Types

The package generates two types of inline data:

  1. Closure data: Maps (object_type, relation) to satisfying relations
  2. Userset data: Maps (object_type, relation) to allowed (subject_type, subject_relation) patterns

Example

For a schema with:

type document
  define owner: [user]
  define editor: [user] or owner
  define viewer: [user] or editor

The closure data for viewer would be:

(VALUES
  ('document', 'viewer', 'viewer'),
  ('document', 'viewer', 'editor'),
  ('document', 'viewer', 'owner')
) AS c(object_type, relation, satisfying_relation)

Integration with SQL Generation

The generated VALUES clauses are embedded in check functions as CTEs:

WITH closure AS (
  <inline closure data>
)
SELECT 1 FROM melange_tuples t
INNER JOIN closure c ON c.satisfying_relation = t.relation
WHERE c.object_type = 'document' AND c.relation = 'viewer' ...

This approach keeps authorization metadata versioned with the schema rather than stored in separate tables that could become inconsistent.

Package inline provides inline SQL model data and typed VALUES rows.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildClosureTypedRows

func BuildClosureTypedRows(closureRows []analysis.ClosureRow) []sqldsl.ValuesRow

BuildClosureTypedRows builds typed ValuesRow slices for closure data. Returns nil for empty input (TypedValuesTable handles empty case).

func BuildUsersetTypedRows

func BuildUsersetTypedRows(analyses []analysis.RelationAnalysis) []sqldsl.ValuesRow

BuildUsersetTypedRows builds typed ValuesRow slices for userset data. Returns nil for empty input (TypedValuesTable handles empty case).

Types

type InlineSQLData

type InlineSQLData struct {
	// ClosureRows contains typed expression rows for closure data.
	// Each row has 3 columns: object_type, relation, satisfying_relation.
	ClosureRows []sqldsl.ValuesRow
	// UsersetRows contains typed expression rows for userset data.
	// Each row has 4 columns: object_type, relation, subject_type, subject_relation.
	UsersetRows []sqldsl.ValuesRow
}

InlineSQLData contains SQL VALUES payloads that replace database-backed model tables. Rationale: Model data is inlined into SQL VALUES clauses rather than querying database tables. This eliminates the need for persistent melange_model tables and ensures generated functions are self-contained. When the schema changes, migration regenerates all functions with updated inline data. This approach trades function size for runtime simplicity and removes a JOIN from every check.

func BuildInlineSQLData

func BuildInlineSQLData(closureRows []analysis.ClosureRow, analyses []analysis.RelationAnalysis) InlineSQLData

BuildInlineSQLData builds inline SQL data for tools and tests.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL