schema

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package schema defines the declarative, schema-driven data model for GoModularAuth.

In GoModularAuth, persistent schemas are defined centrally and can be extended by plugin modules (e.g. adding two_factor_enabled to users, or registering new models like passkeys or organizations). Database adapters consume the compiled Schema to execute queries, map fields, and generate or execute database migrations.

Index

Constants

View Source
const (
	ModelUsers         = "users"
	ModelAccounts      = "accounts"
	ModelCredentials   = "credentials"
	ModelSessions      = "sessions"
	ModelVerifications = "verifications"
)

Core model names.

Variables

View Source
var (
	// ErrModelNotFound indicates the requested model does not exist in the schema.
	ErrModelNotFound = errors.New("schema: model not found")
	// ErrModelExists indicates a model with the same name is already registered.
	ErrModelExists = errors.New("schema: model already exists")
	// ErrFieldExists indicates a field already exists in the model.
	ErrFieldExists = errors.New("schema: field already exists")
	// ErrInvalidField indicates a field definition is invalid.
	ErrInvalidField = errors.New("schema: invalid field")
	// ErrInvalidModel indicates a model definition is invalid.
	ErrInvalidModel = errors.New("schema: invalid model")
	// ErrInvalidReference indicates a foreign key reference targets a nonexistent model or field.
	ErrInvalidReference = errors.New("schema: invalid foreign key reference")
)

Functions

This section is empty.

Types

type DataType

type DataType string

DataType represents the canonical data types supported across all storage engines.

const (
	TypeString  DataType = "string"
	TypeInt64   DataType = "int64"
	TypeFloat64 DataType = "float64"
	TypeBool    DataType = "bool"
	TypeTime    DataType = "time"
	TypeBytes   DataType = "bytes"
	TypeJSON    DataType = "json"
)

func (DataType) IsValid

func (dt DataType) IsValid() bool

IsValid reports whether dt is one of the supported canonical data types.

type Field

type Field struct {
	Name       string
	DBName     string // Column name in database; defaults to Name if empty
	Type       DataType
	Required   bool
	Unique     bool
	Indexed    bool
	Default    any
	References *Reference
}

Field describes a single column or property within a Model.

func (Field) EffectiveDBName

func (f Field) EffectiveDBName() string

EffectiveDBName returns the database column name, defaulting to Name if DBName is empty.

type Index

type Index struct {
	Name   string
	Fields []string
	Unique bool
}

Index describes a single or composite index on a model.

type Model

type Model struct {
	Name       string
	TableName  string
	PrimaryKey string
	Fields     map[string]Field
	Indexes    []Index
}

Model represents a domain entity table or collection in the storage engine.

func AccountModel

func AccountModel() *Model

AccountModel returns the canonical core schema definition for the accounts table.

func CredentialModel

func CredentialModel() *Model

CredentialModel returns the canonical core schema definition for the credentials table.

func NewModel

func NewModel(name, tableName, primaryKey string) *Model

NewModel creates an empty Model initialized with the given name, table name, and primary key.

func SessionModel

func SessionModel() *Model

SessionModel returns the canonical core schema definition for the sessions table.

func UserModel

func UserModel() *Model

UserModel returns the canonical core schema definition for the users table.

func VerificationModel

func VerificationModel() *Model

VerificationModel returns the canonical core schema definition for the verification tokens table.

func (*Model) AddField

func (m *Model) AddField(f Field) error

AddField registers a field into the model, returning an error if a field with the same name exists.

func (*Model) Clone

func (m *Model) Clone() *Model

Clone creates a deep copy of the model.

func (*Model) GetField

func (m *Model) GetField(name string) (Field, bool)

GetField retrieves a field by name.

type Reference

type Reference struct {
	Model    string
	Field    string
	OnDelete string // e.g. "CASCADE", "SET NULL", "RESTRICT"
}

Reference defines a relational foreign key constraint to another model.

type Schema

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

Schema aggregates all Models in the authentication engine and manages extensions.

func DefaultCoreSchema

func DefaultCoreSchema() (*Schema, error)

DefaultCoreSchema initializes and returns a Schema populated with all core models and validated.

func New

func New() *Schema

New creates an empty Schema.

func (*Schema) AddModel

func (s *Schema) AddModel(m *Model) error

AddModel registers a model into the schema.

func (*Schema) Clone

func (s *Schema) Clone() *Schema

Clone returns a deep copy of the Schema.

func (*Schema) ExtendModel

func (s *Schema) ExtendModel(modelName string, fields ...Field) error

ExtendModel allows plugins to inject new fields into an existing model.

func (*Schema) GetModel

func (s *Schema) GetModel(name string) (*Model, bool)

GetModel retrieves a model by name from the schema.

func (*Schema) Models

func (s *Schema) Models() map[string]*Model

Models returns a cloned map of all registered models.

func (*Schema) Validate

func (s *Schema) Validate() error

Validate checks the overall integrity of the schema, ensuring all primary keys, field data types, indexes, and foreign key references are consistent.

Jump to

Keyboard shortcuts

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