schema

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Column

type Column struct {
	Name             string
	Type             ColumnType
	Length           int
	Precision        int
	Scale            int
	IsPrimaryKey     bool
	IsNullable       bool
	IsUnique         bool
	DefaultValue     any
	HasDefault       bool
	ForeignKeyTable  string
	ForeignKeyColumn string
	IsPublic         bool
	IsOwnerSees      bool
	IsOwnerColumn    bool
	IsEncrypted      bool
	IsSealed         bool
	IsUnsafePublic   bool
	FKMetadataOnly   bool            // FK is for ORM relationship metadata only; no SQL REFERENCES constraint
	VisibleTo        map[string]bool // role slugs that can see this column
}

Column represents a database column definition.

func (*Column) Default

func (c *Column) Default(value any) *Column

func (*Column) Encrypted

func (c *Column) Encrypted() *Column

Encrypted marks this column as requiring encryption at rest.

func (*Column) ForeignKey

func (c *Column) ForeignKey(table, column string) *Column

func (*Column) IsOwner

func (c *Column) IsOwner() *Column

IsOwner marks this column as the ownership column for the table. The value of this column is compared against the authenticated user's ID to determine ownership. Only one column per table may be marked as owner.

func (*Column) NotNull

func (c *Column) NotNull() *Column

func (*Column) Nullable

func (c *Column) Nullable() *Column

func (*Column) OwnerSees

func (c *Column) OwnerSees() *Column

OwnerSees marks this column as visible only to the row's owner.

func (*Column) PrimaryKey

func (c *Column) PrimaryKey() *Column

func (*Column) Public

func (c *Column) Public() *Column

Public marks this column as visible to anyone (no auth required).

func (*Column) RoleSees

func (c *Column) RoleSees(slug string) *Column

RoleSees marks this column as visible to the specified role slug.

func (*Column) Sealed

func (c *Column) Sealed() *Column

Sealed marks this column as requiring non-deterministic encryption at rest (AES-256-GCM). Sealed columns cannot be searched — no WHERE scopes are generated.

func (*Column) Unique

func (c *Column) Unique() *Column

func (*Column) UnsafePublic

func (c *Column) UnsafePublic() *Column

UnsafePublic explicitly acknowledges that a sensitive-named column is intentionally marked Public. Without this, Squeeze flags the Public/sensitive combination as an error.

type ColumnType

type ColumnType int

ColumnType represents the data type of a database column.

const (
	UUID ColumnType = iota
	String
	Text
	Integer
	BigInteger
	Decimal
	Boolean
	Timestamp
	JSONB
	Date
	Time
	Binary
	Float
	Double
)

func (ColumnType) String

func (t ColumnType) String() string

type ControllerActionDef

type ControllerActionDef struct {
	Name    string
	Handler interface{}
}

ControllerActionDef describes a custom controller action exposed as a GraphQL mutation.

type DriverCapabilities

type DriverCapabilities struct {
	TransactionalDDL   bool // Can run DDL inside a transaction (Postgres: yes, MySQL: no)
	ConcurrentIndexing bool // Supports CREATE INDEX CONCURRENTLY (Postgres)
	JSONBSupport       bool // Native JSONB type (Postgres)
	UUIDNativeType     bool // Native UUID type (Postgres)
	AdvisoryLocks      bool // Advisory lock support (Postgres)
	ForeignKeys        bool // Foreign key constraints (SQL: yes, doc stores: no)
	EmbeddedDocs       bool // Embedded subdocuments (MongoDB, DynamoDB: yes)
	SecondaryIndex     bool // Secondary indexes
	UniqueIndex        bool // Unique index constraints
}

DriverCapabilities describes what a database driver supports. Used by the generator to make storage strategy decisions for nested schemas and query layer generation.

func DriverCaps

func DriverCaps(driver string) DriverCapabilities

DriverCaps returns capabilities for a given driver name.

type ExposeBuilder

type ExposeBuilder struct {
	// contains filtered or unexported fields
}

ExposeBuilder provides a fluent API for defining which operations to expose.

func (*ExposeBuilder) All

func (e *ExposeBuilder) All()

All adds all five CRUD operations (list, show, create, update, delete).

func (*ExposeBuilder) Create

func (e *ExposeBuilder) Create()

Create adds the create operation.

func (*ExposeBuilder) Delete

func (e *ExposeBuilder) Delete()

Delete adds the delete operation.

func (*ExposeBuilder) List

func (e *ExposeBuilder) List()

List adds the list operation.

func (*ExposeBuilder) RemoveCreate

func (e *ExposeBuilder) RemoveCreate()

RemoveCreate marks the create operation for removal (alter only).

func (*ExposeBuilder) RemoveDelete

func (e *ExposeBuilder) RemoveDelete()

RemoveDelete marks the delete operation for removal (alter only).

func (*ExposeBuilder) RemoveList

func (e *ExposeBuilder) RemoveList()

RemoveList marks the list operation for removal (alter only).

func (*ExposeBuilder) RemoveShow

func (e *ExposeBuilder) RemoveShow()

RemoveShow marks the show operation for removal (alter only).

func (*ExposeBuilder) RemoveUpdate

func (e *ExposeBuilder) RemoveUpdate()

RemoveUpdate marks the update operation for removal (alter only).

func (*ExposeBuilder) Show

func (e *ExposeBuilder) Show()

Show adds the show operation.

func (*ExposeBuilder) Update

func (e *ExposeBuilder) Update()

Update adds the update operation.

type ExposedOperation

type ExposedOperation struct {
	Type string // "list", "show", "create", "update", "delete"
}

ExposedOperation describes a single CRUD operation exposed over GraphQL.

type GraphQLOperation

type GraphQLOperation struct {
	Type   string // "expose", "alter_expose", "unexpose", "controller_action", "remove_action"
	Model  string
	Ops    []ExposedOperation
	Action *ControllerActionDef
}

GraphQLOperation records a single GraphQL exposure change.

type GraphQLPolicy

type GraphQLPolicy struct {
	Operations []GraphQLOperation
}

GraphQLPolicy is the base type embedded by all GraphQL exposure policy structs. It records GraphQL exposure operations for later execution or inspection.

func (*GraphQLPolicy) AlterExpose

func (p *GraphQLPolicy) AlterExpose(model string, fn func(*ExposeBuilder))

AlterExpose modifies the exposed operations for an already-exposed model.

func (*GraphQLPolicy) ControllerAction

func (p *GraphQLPolicy) ControllerAction(name string, handler interface{})

ControllerAction registers a custom controller action as a GraphQL mutation.

func (*GraphQLPolicy) Expose

func (p *GraphQLPolicy) Expose(model string, fn func(*ExposeBuilder))

Expose registers a model with the specified operations for GraphQL exposure.

func (*GraphQLPolicy) GetOperations

func (p *GraphQLPolicy) GetOperations() []GraphQLOperation

GetOperations returns the operations recorded by Up() or Down().

func (*GraphQLPolicy) RemoveAction

func (p *GraphQLPolicy) RemoveAction(name string)

RemoveAction removes a previously registered controller action.

func (*GraphQLPolicy) Reset

func (p *GraphQLPolicy) Reset()

Reset clears recorded operations so the policy struct can be reused.

func (*GraphQLPolicy) Transactional

func (p *GraphQLPolicy) Transactional() bool

Transactional returns true — policies run in a transaction by default.

func (*GraphQLPolicy) Unexpose

func (p *GraphQLPolicy) Unexpose(model string)

Unexpose removes a model entirely from the GraphQL schema.

type Index

type Index struct {
	Table   string
	Columns []string
	Unique  bool
}

Index represents a database index.

type Migration

type Migration struct {
	Operations []Operation
}

Migration is the base type embedded by all migration structs. It records schema operations for later execution or inspection.

func (*Migration) AddColumn

func (m *Migration) AddColumn(table string, fn func(*Table))

func (*Migration) AddIndex

func (m *Migration) AddIndex(table string, columns ...string)

func (*Migration) AddUniqueIndex

func (m *Migration) AddUniqueIndex(table string, columns ...string)

func (*Migration) AlterTable

func (m *Migration) AlterTable(name string, fn func(*Table))

AlterTable allows adding columns and nested relationships to an existing table.

func (*Migration) Connection

func (m *Migration) Connection() string

Connection returns the database connection name this migration targets. Returns "" to use the default connection. Override in concrete migration structs to target a specific named connection from config/database.go.

func (*Migration) CreateTable

func (m *Migration) CreateTable(name string, fn func(*Table))

func (*Migration) CreateView

func (m *Migration) CreateView(name string, fn func(*View))

func (*Migration) DropColumn

func (m *Migration) DropColumn(table, column string)

func (*Migration) DropTableIfExists

func (m *Migration) DropTableIfExists(name string)

func (*Migration) DropView

func (m *Migration) DropView(name string)

func (*Migration) GetOperations

func (m *Migration) GetOperations() []Operation

GetOperations returns the operations recorded by Up() or Down().

func (*Migration) RenameColumn

func (m *Migration) RenameColumn(table, oldName, newName string)

func (*Migration) RenameTable

func (m *Migration) RenameTable(oldName, newName string)

func (*Migration) Reset

func (m *Migration) Reset()

Reset clears recorded operations so the migration struct can be reused.

func (*Migration) Transactional

func (m *Migration) Transactional() bool

Transactional returns true — migrations run in a transaction by default. Override in concrete migration structs to opt out.

type Operation

type Operation struct {
	Type       TableOperation
	Table      string
	TableDef   *Table
	ViewDef    *View
	Index      *Index
	NewName    string // for rename operations
	OldName    string // for rename operations
	ColumnName string // for drop/rename column
	ColumnDef  func(*Table)
}

Operation records a single schema change.

type Policy

type Policy struct {
	Operations []RoleOperation
}

Policy is the base type embedded by all role policy structs. It records role operations for later execution or inspection.

func (*Policy) AlterRole

func (p *Policy) AlterRole(slug string) *RoleBuilder

AlterRole begins a role alteration operation.

func (*Policy) CreateRole

func (p *Policy) CreateRole(slug string) *RoleBuilder

CreateRole begins a role creation operation.

func (*Policy) DropRole

func (p *Policy) DropRole(slug string)

DropRole records a role drop operation.

func (*Policy) GetOperations

func (p *Policy) GetOperations() []RoleOperation

GetOperations returns the operations recorded by Up() or Down().

func (*Policy) Reset

func (p *Policy) Reset()

Reset clears recorded operations so the policy struct can be reused.

func (*Policy) Transactional

func (p *Policy) Transactional() bool

Transactional returns true — policies run in a transaction by default.

type Relationship

type Relationship struct {
	Type         RelationshipType
	Name         string // child table name (e.g., "posts")
	ChildTable   *Table // child schema definition
	ParentTable  string // parent table name (set during CreateTable)
	IsCollection bool   // .Collection() — separate collection for doc stores
	IsTopLevel   bool   // .TopLevelModel() — generate at models/ not models/parent/
}

Relationship records a nested child table declared via HasMany/HasOne.

func (*Relationship) Collection

func (r *Relationship) Collection() *Relationship

func (*Relationship) TopLevelModel

func (r *Relationship) TopLevelModel() *Relationship

type RelationshipType

type RelationshipType int

RelationshipType distinguishes HasMany from HasOne.

const (
	RelHasMany RelationshipType = iota
	RelHasOne
)

type RoleBuilder

type RoleBuilder struct {
	// contains filtered or unexported fields
}

RoleBuilder provides a fluent API for defining role operations.

func (*RoleBuilder) Can

func (b *RoleBuilder) Can(actions ...string) *RoleBuilder

Can grants the specified actions to this role.

func (*RoleBuilder) Default

func (b *RoleBuilder) Default() *RoleBuilder

Default marks this role as the default role for new users.

func (*RoleBuilder) Manages

func (b *RoleBuilder) Manages() *RoleBuilder

Manages marks this role as a managing role.

func (*RoleBuilder) Name

func (b *RoleBuilder) Name(name string) *RoleBuilder

Name sets the display name for the role.

func (*RoleBuilder) RemoveDefault

func (b *RoleBuilder) RemoveDefault() *RoleBuilder

RemoveDefault removes the default flag from this role.

func (*RoleBuilder) RemoveManages

func (b *RoleBuilder) RemoveManages() *RoleBuilder

RemoveManages removes the manages flag from this role.

func (*RoleBuilder) RevokeCan

func (b *RoleBuilder) RevokeCan(actions ...string) *RoleBuilder

RevokeCan removes the specified actions from this role (alter only).

type RoleDefinition

type RoleDefinition struct {
	Slug        string
	DisplayName string
	IsManages   bool
	IsDefault   bool
	Actions     []string

	// Used by alter operations to track removals.
	RemoveManages bool
	RemoveDefault bool
	RevokeActions []string
}

RoleDefinition describes a role and its permissions.

type RoleOperation

type RoleOperation struct {
	Type string // "create", "alter", "drop"
	Role RoleDefinition
}

RoleOperation records a single role lifecycle change.

type Table

type Table struct {
	Name          string
	Connection    string // database connection name ("" = default)
	Columns       []*Column
	Relationships []*Relationship
	IsImmutable   bool // set by Immutable() — versioned, (id, version_id) composite PK
	IsAppendOnly  bool // set by AppendOnly() — insert-only, single id PK, no updates/deletes
	HasSoftDelete bool // set by SoftDeletes() — adds deleted_at nullable column
}

Table collects column definitions for a database table.

func (*Table) AppendOnly

func (t *Table) AppendOnly()

AppendOnly marks this table as insert-only. Pickle injects a single id primary key. The generated query type has no Update or Delete methods. CreatedAt is derived from the UUID v7 timestamp in id. Use this for financial ledgers, event logs, and audit journals where records must never be modified after creation.

func (*Table) BigInteger

func (t *Table) BigInteger(name string) *Column

func (*Table) Binary

func (t *Table) Binary(name string) *Column

func (*Table) Boolean

func (t *Table) Boolean(name string) *Column

func (*Table) Date

func (t *Table) Date(name string) *Column

func (*Table) Decimal

func (t *Table) Decimal(name string, precision, scale int) *Column

func (*Table) Double

func (t *Table) Double(name string) *Column

Double adds a double-precision floating-point column (SQL DOUBLE PRECISION). In almost all cases you should use Decimal(name, precision, scale) instead.

func (*Table) Float

func (t *Table) Float(name string) *Column

Float adds a single-precision floating-point column (SQL REAL / FLOAT). In almost all cases you should use Decimal(name, precision, scale) instead.

func (*Table) HasMany

func (t *Table) HasMany(name string, fn func(*Table)) *Relationship

HasMany declares a one-to-many relationship. The child table gets an auto-injected FK column pointing back to this table's primary key.

func (*Table) HasOne

func (t *Table) HasOne(name string, fn func(*Table)) *Relationship

HasOne declares a one-to-one relationship. The child table gets an auto-injected unique FK column pointing back to this table's primary key.

func (*Table) Immutable

func (t *Table) Immutable()

Immutable marks this table as append-only. Pickle injects id and version_id as a composite primary key and generates insert-on-update query behaviour. Do not call Timestamps() on an immutable table — created_at and updated_at are derived from the UUID v7 timestamps embedded in id and version_id.

func (*Table) Integer

func (t *Table) Integer(name string) *Column

func (*Table) JSONB

func (t *Table) JSONB(name string) *Column

func (*Table) SoftDeletes

func (t *Table) SoftDeletes()

SoftDeletes adds a nullable deleted_at timestamp column. On an immutable table, Delete() inserts a new version with deleted_at set. On a mutable table, Delete() issues a standard soft-delete UPDATE. Panics on append-only tables — append-only records cannot be deleted.

func (*Table) String

func (t *Table) String(name string, length ...int) *Column

func (*Table) Text

func (t *Table) Text(name string) *Column

func (*Table) Time

func (t *Table) Time(name string) *Column

func (*Table) Timestamp

func (t *Table) Timestamp(name string) *Column

func (*Table) Timestamps

func (t *Table) Timestamps()

Timestamps adds created_at and updated_at columns with NOW() defaults. Panics if called on an immutable or append-only table — those derive timestamps from UUID v7.

func (*Table) UUID

func (t *Table) UUID(name string) *Column

type TableOperation

type TableOperation int

TableOperation represents a schema change recorded by a migration.

const (
	OpCreateTable TableOperation = iota
	OpDropTableIfExists
	OpRenameTable
	OpAddColumn
	OpDropColumn
	OpRenameColumn
	OpAddIndex
	OpAddUniqueIndex
	OpCreateView
	OpDropView
)

type View

type View struct {
	Name        string
	Sources     []ViewSource
	Columns     []*ViewColumn
	GroupByCols []string
}

View represents a database view definition.

func (*View) Column

func (v *View) Column(ref string, alias ...string)

Column adds a column reference from a source table. ref is "alias.column" (e.g. "t.id"). An optional second argument aliases the output.

func (*View) From

func (v *View) From(table, alias string)

From sets the primary source table for the view.

func (*View) GroupBy

func (v *View) GroupBy(columns ...string)

GroupBy sets the GROUP BY columns.

func (*View) Join

func (v *View) Join(table, alias, on string)

Join adds an INNER JOIN to the view.

func (*View) LeftJoin

func (v *View) LeftJoin(table, alias, on string)

LeftJoin adds a LEFT JOIN to the view.

func (*View) SelectRaw

func (v *View) SelectRaw(name, expr string) *ViewColumn

SelectRaw adds a computed column with a raw SQL expression. Returns the ViewColumn so the caller can chain type builder methods.

type ViewColumn

type ViewColumn struct {
	Column              // embedded — carries Name, Type, Precision, Scale, IsNullable
	SourceAlias  string // e.g. "t" from "t.id"
	SourceColumn string // e.g. "id" from "t.id"
	OutputAlias  string // optional alias for the output column name
	RawExpr      string // raw SQL expression (for SelectRaw)
}

ViewColumn represents a column in a view's SELECT list.

func (*ViewColumn) BigInteger

func (vc *ViewColumn) BigInteger() *ViewColumn

func (*ViewColumn) BooleanType

func (vc *ViewColumn) BooleanType() *ViewColumn

func (*ViewColumn) Decimal

func (vc *ViewColumn) Decimal(precision, scale int) *ViewColumn

func (*ViewColumn) IntegerType

func (vc *ViewColumn) IntegerType() *ViewColumn

func (*ViewColumn) JSONBType

func (vc *ViewColumn) JSONBType() *ViewColumn

func (*ViewColumn) OutputName

func (vc *ViewColumn) OutputName() string

OutputName returns the column name as it appears in the view's output.

func (*ViewColumn) StringType

func (vc *ViewColumn) StringType(length ...int) *ViewColumn

func (*ViewColumn) TextType

func (vc *ViewColumn) TextType() *ViewColumn

func (*ViewColumn) TimestampType

func (vc *ViewColumn) TimestampType() *ViewColumn

func (*ViewColumn) UUIDType

func (vc *ViewColumn) UUIDType() *ViewColumn

type ViewSource

type ViewSource struct {
	Table         string
	Alias         string
	JoinType      string // "" for FROM, "JOIN", "LEFT JOIN"
	JoinCondition string
}

ViewSource represents a table referenced by a view (FROM or JOIN).

Jump to

Keyboard shortcuts

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