schema

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2026 License: MIT Imports: 10 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package schema holds the model parsed from the specs, the database derived from it and the diff that turns two versions of that database into a migration.

Index

Constants

View Source
const (
	SessionsTable      = "auth_sessions"
	RefreshTokensTable = "auth_refresh_tokens"
	APIKeysTable       = "auth_api_keys"
	RateLimitsTable    = "auth_rate_limits"
)
View Source
const (
	TokenHashSize = 64
	TokenIDSize   = 32
	KeyNameSize   = 120
	KeyPrefixSize = 16
	PasswordSize  = 255
)
View Source
const DeletedAtColumn = "deleted_at"
View Source
const IdempotencyLocationSize = 2048
View Source
const IdempotencyTable = "idempotency_keys"

IdempotencyTable keeps the answers to requests sent with an Idempotency-Key header, so a client that retries a create after a lost answer gets the first answer back instead of a second record. Every database with entities has one.

View Source
const MaxIdentifierLength = 63
View Source
const SharedDatabase = "shared"
View Source
const SnapshotVersion = 1

Variables

Functions

func AuthUserColumn

func AuthUserColumn(e *Entity) string

func BuildDatabases

func BuildDatabases(m *Model) map[string]*Database

func ColumnSize

func ColumnSize(f *Field) int

func ConstraintName

func ConstraintName(prefix string, parts ...string) string

func JoinColumn

func JoinColumn(e *Entity) string

func JoinPair

func JoinPair(a, b *Entity) (*Entity, *Entity)

func MigrationName

func MigrationName(oldDB *Database, changes []Change) string

func RelationPolicies

func RelationPolicies(source, target *Entity, kind RelationKind) (list Policy, link Policy, ok bool)

RelationPolicies returns the policies of the list and link routes of a relation. Linking writes a row of the join table both entities share, so every route that links the pair, from either side, needs read and update access to both.

func SameDatabase

func SameDatabase(a, b *Entity) bool

Types

type Change

type Change struct {
	Kind       ChangeKind
	TableName  string
	Table      *Table
	InlineFKs  []*ForeignKey
	Column     *Column
	OldColumn  *Column
	Index      *Index
	ForeignKey *ForeignKey
	Check      *Check
	NewTable   bool
}

func Inverse

func Inverse(c Change) Change

type ChangeKind

type ChangeKind string
const (
	ChangeCreateTable    ChangeKind = "create_table"
	ChangeDropTable      ChangeKind = "drop_table"
	ChangeAddColumn      ChangeKind = "add_column"
	ChangeDropColumn     ChangeKind = "drop_column"
	ChangeAlterColumn    ChangeKind = "alter_column"
	ChangeAddIndex       ChangeKind = "add_index"
	ChangeDropIndex      ChangeKind = "drop_index"
	ChangeAddForeignKey  ChangeKind = "add_foreign_key"
	ChangeDropForeignKey ChangeKind = "drop_foreign_key"
	ChangeAddCheck       ChangeKind = "add_check"
	ChangeDropCheck      ChangeKind = "drop_check"
)

type Check

type Check struct {
	Name   string   `json:"name"`
	Column string   `json:"column"`
	Values []string `json:"values"`
}

type Column

type Column struct {
	Name     string     `json:"name"`
	Kind     ColumnKind `json:"kind"`
	Size     int        `json:"size,omitempty"`
	Nullable bool       `json:"nullable,omitempty"`
	Identity bool       `json:"identity,omitempty"`
	Default  *Default   `json:"default,omitempty"`
}

func FieldColumn

func FieldColumn(f *Field) *Column

type ColumnKind

type ColumnKind string
const (
	ColumnString  ColumnKind = "string"
	ColumnBool    ColumnKind = "bool"
	ColumnInt16   ColumnKind = "int16"
	ColumnInt32   ColumnKind = "int32"
	ColumnInt64   ColumnKind = "int64"
	ColumnFloat32 ColumnKind = "float32"
	ColumnFloat64 ColumnKind = "float64"
	ColumnBytes   ColumnKind = "bytes"
	ColumnTime    ColumnKind = "time"
	ColumnUUID    ColumnKind = "uuid"
)

type Database

type Database struct {
	Tables []*Table `json:"tables"`
}

func Build

func Build(m *Model) *Database

func (*Database) Table

func (d *Database) Table(name string) *Table

type DatabaseMode

type DatabaseMode string
const (
	DatabaseOwn    DatabaseMode = "own"
	DatabaseShared DatabaseMode = "shared"
)

type Default

type Default struct {
	Kind  DefaultKind `json:"kind"`
	Value string      `json:"value,omitempty"`
}

type DefaultKind

type DefaultKind string
const (
	DefaultLiteral DefaultKind = "literal"
	DefaultNow     DefaultKind = "now"
)

type Diagnostic

type Diagnostic struct {
	Pos
	Severity   Severity `json:"severity"`
	Code       string   `json:"code"`
	Message    string   `json:"message"`
	Suggestion string   `json:"suggestion,omitempty"`
}

func (Diagnostic) String

func (d Diagnostic) String() string

type Diagnostics

type Diagnostics []Diagnostic

func (Diagnostics) Error

func (ds Diagnostics) Error() string

func (Diagnostics) Errors

func (ds Diagnostics) Errors() Diagnostics

func (Diagnostics) HasErrors

func (ds Diagnostics) HasErrors() bool

func (Diagnostics) Sort

func (ds Diagnostics) Sort()

func (Diagnostics) Warnings

func (ds Diagnostics) Warnings() Diagnostics

type DiffOptions

type DiffOptions struct {
	RecreateDependents bool
}

type DiffResult

type DiffResult struct {
	Changes  []Change
	Warnings []string
}

func Diff

func Diff(oldDB, newDB *Database, opts DiffOptions) DiffResult

func (DiffResult) Down

func (r DiffResult) Down() []Change

func (DiffResult) Empty

func (r DiffResult) Empty() bool

type Entity

type Entity struct {
	Name       string
	Domain     string
	File       string
	Pos        Pos
	Table      string
	SoftDelete bool
	NoRoutes   bool
	ReadOnly   bool
	UUIDImport string
	Service    *Service
	Auth       []Method
	Register   bool
	Public     Operations
	Roles      []string
	WriteRoles []string
	Fields     []*Field
	Relations  []*Relation
}

func (*Entity) AutoCreateField

func (e *Entity) AutoCreateField() *Field

func (*Entity) BelongsTo

func (e *Entity) BelongsTo() []*Field

func (*Entity) CreateFields

func (e *Entity) CreateFields() []*Field

func (*Entity) DatabaseName

func (e *Entity) DatabaseName() string

func (*Entity) Field

func (e *Entity) Field(name string) *Field

func (*Entity) FieldNames

func (e *Entity) FieldNames() []string

func (*Entity) FilterFields

func (e *Entity) FilterFields() []*Field

func (*Entity) HasMethod

func (e *Entity) HasMethod(m Method) bool

func (*Entity) Identity

func (e *Entity) Identity() *Field

func (*Entity) IsAuth

func (e *Entity) IsAuth() bool

func (*Entity) LoginMethod

func (e *Entity) LoginMethod() Method

func (*Entity) Owner

func (e *Entity) Owner() *Field

func (*Entity) PK

func (e *Entity) PK() *Field

func (*Entity) Password

func (e *Entity) Password() *Field

func (*Entity) Path

func (e *Entity) Path() string

func (*Entity) PathParam

func (e *Entity) PathParam() string

func (*Entity) Policy

func (e *Entity) Policy(op Operation) Policy

func (*Entity) Role

func (e *Entity) Role() *Field

func (*Entity) UniqueFields

func (e *Entity) UniqueFields() []*Field

func (*Entity) UpdateFields

func (e *Entity) UpdateFields() []*Field

func (*Entity) Var

func (e *Entity) Var() string

type Field

type Field struct {
	Name         string
	Kind         Kind
	Nullable     bool
	Column       string
	JSON         string
	Pos          Pos
	PK           bool
	AutoPK       bool
	AutoCreate   bool
	AutoUpdate   bool
	Required     bool
	NotEmpty     bool
	Unique       bool
	Index        bool
	Email        bool
	Min          string
	Max          string
	Enum         []string
	Default      string
	HasDefault   bool
	OmitCreate   bool
	OmitUpdate   bool
	OmitResponse bool
	Ref          *Reference
	Identity     bool
	Password     bool
	Role         bool
	Owner        bool
}

func (*Field) Auto

func (f *Field) Auto() bool

func (*Field) CrossService

func (f *Field) CrossService(owner *Entity) bool

func (*Field) External

func (f *Field) External(owner *Entity) bool

func (*Field) Filterable

func (f *Field) Filterable() bool

func (*Field) GoType

func (f *Field) GoType() string

func (*Field) InCreate

func (f *Field) InCreate() bool

func (*Field) InUpdate

func (f *Field) InUpdate() bool

func (*Field) InputJSON

func (f *Field) InputJSON() string

func (*Field) InputName

func (f *Field) InputName() string

type ForeignKey

type ForeignKey struct {
	Name       string   `json:"name"`
	Columns    []string `json:"columns"`
	RefTable   string   `json:"refTable"`
	RefColumns []string `json:"refColumns"`
	OnDelete   OnDelete `json:"onDelete"`
}

type Index

type Index struct {
	Name       string   `json:"name"`
	Columns    []string `json:"columns"`
	Unique     bool     `json:"unique,omitempty"`
	NotDeleted bool     `json:"notDeleted,omitempty"`
}

type Kind

type Kind int
const (
	KindString Kind = iota + 1
	KindBool
	KindInt16
	KindInt32
	KindInt
	KindInt64
	KindFloat32
	KindFloat64
	KindBytes
	KindTime
	KindUUID
)

func (Kind) BitSize

func (k Kind) BitSize() int

func (Kind) ColumnKind

func (k Kind) ColumnKind() ColumnKind

func (Kind) GoType

func (k Kind) GoType() string

func (Kind) IsFloat

func (k Kind) IsFloat() bool

func (Kind) IsInteger

func (k Kind) IsInteger() bool

func (Kind) IsNumeric

func (k Kind) IsNumeric() bool

func (Kind) String

func (k Kind) String() string

type Method

type Method string
const (
	MethodSession Method = "session"
	MethodJWT     Method = "jwt"
	MethodAPIKey  Method = "apikey"
)

type Model

type Model struct {
	Entities []*Entity
	Services []*Service
}

func (*Model) AuthEntity

func (m *Model) AuthEntity() *Entity

func (*Model) Entity

func (m *Model) Entity(name string) *Entity

func (*Model) EntityNames

func (m *Model) EntityNames() []string

func (*Model) Service

func (m *Model) Service(name string) *Service

func (*Model) Workspace

func (m *Model) Workspace() bool

type OnDelete

type OnDelete string
const (
	OnDeleteRestrict OnDelete = "restrict"
	OnDeleteCascade  OnDelete = "cascade"
	OnDeleteSetNull  OnDelete = "setnull"
)

type Operation

type Operation string
const (
	OpRead   Operation = "read"
	OpCreate Operation = "create"
	OpUpdate Operation = "update"
	OpDelete Operation = "delete"
)

type Operations

type Operations struct {
	Read   bool
	Create bool
	Update bool
	Delete bool
}

func (Operations) Any

func (o Operations) Any() bool

func (Operations) Has

func (o Operations) Has(op Operation) bool

func (*Operations) Set

func (o *Operations) Set(op Operation)

type Policy

type Policy struct {
	Public bool
	Roles  []string
}

func Combine

func Combine(a, b Policy) (Policy, bool)

type Pos

type Pos struct {
	File   string `json:"file,omitempty"`
	Line   int    `json:"line,omitempty"`
	Column int    `json:"column,omitempty"`
}

func (Pos) IsValid

func (p Pos) IsValid() bool

func (Pos) String

func (p Pos) String() string

type PrimaryKey

type PrimaryKey struct {
	Name    string   `json:"name"`
	Columns []string `json:"columns"`
}

type Reference

type Reference struct {
	Entity   string
	OnDelete OnDelete
	Target   *Entity `json:"-"`
}

type Relation

type Relation struct {
	Name   string
	Kind   RelationKind
	Target string
	FK     string
	Via    string
	Pos    Pos
}

func (*Relation) PathSegment

func (r *Relation) PathSegment() string

type RelationKind

type RelationKind string
const (
	RelationHasMany    RelationKind = "has_many"
	RelationManyToMany RelationKind = "many_to_many"
)

type Service

type Service struct {
	Name     string
	TypeName string
	File     string
	Pos      Pos
	Database DatabaseMode
	Entities []*Entity
}

func (*Service) DatabaseName

func (s *Service) DatabaseName() string

type Severity

type Severity string
const (
	SeverityError   Severity = "error"
	SeverityWarning Severity = "warning"
)

type Snapshot

type Snapshot struct {
	Version    int      `json:"version"`
	Dialect    string   `json:"dialect"`
	Migrations []string `json:"migrations"`
	Tables     []*Table `json:"tables"`
}

func NewSnapshot

func NewSnapshot(dialect string, db *Database, migrations []string) *Snapshot

func UnmarshalSnapshot

func UnmarshalSnapshot(data []byte) (*Snapshot, error)

func (*Snapshot) Database

func (s *Snapshot) Database() *Database

func (*Snapshot) Marshal

func (s *Snapshot) Marshal() ([]byte, error)

type Table

type Table struct {
	Name        string        `json:"name"`
	Columns     []*Column     `json:"columns"`
	PrimaryKey  *PrimaryKey   `json:"primaryKey"`
	Indexes     []*Index      `json:"indexes,omitempty"`
	ForeignKeys []*ForeignKey `json:"foreignKeys,omitempty"`
	Checks      []*Check      `json:"checks,omitempty"`
}

func (*Table) Check

func (t *Table) Check(name string) *Check

func (*Table) Column

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

func (*Table) ForeignKey

func (t *Table) ForeignKey(name string) *ForeignKey

func (*Table) Index

func (t *Table) Index(name string) *Index

Jump to

Keyboard shortcuts

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