Skip to content

API Reference

This section serves as a high-level reference for the public API exported by Bavard. For detailed usage instructions and examples, please refer to the Guide.

Core Components

The foundational classes that power the ORM.

ClassDescriptionDocumentation
ModelThe abstract base class for all your entities. Handles hydration, dirty checking, and persistence.Creating Models
QueryBuilder<T>Fluent interface for constructing SQL queries safely. Returned by User().query().Query Builder
DatabaseManagerSingleton service locator for managing the active database connection and transactions.Initial Setup
DatabaseAdapterInterface that must be implemented to connect Bavard to a specific database driver (SQLite, Postgres, etc.).Adapter
GrammarStrategy pattern interface for dialect-specific SQL generation (e.g., SQLite vs Postgres).Adapters
PivotSpecialized Model for intermediate tables in Many-to-Many relationships.Relationships

Schema Definition

Classes used inside the static const schema Record to define type-safe columns.

Column ClassMaps to Dart TypeSQL EquivalentDescription
TextColumnStringTEXT / VARCHARSupports contains, startsWith, endsWith.
IntColumnintINTEGERSupports numeric comparisons (>, <, etc.).
DoubleColumndoubleREAL / DOUBLESupports numeric comparisons.
BoolColumnboolINTEGER (0/1)Stores boolean values as integers for compatibility.
DateTimeColumnDateTimeTEXT (ISO-8601)Stores dates as standardized strings.
JsonColumndynamicTEXT (JSON)Stores Maps/Lists. Enables JsonPath querying.
EnumColumn<T>EnumTEXTStores the name of the Enum.

Relationships

Classes representing the associations between models.

Standard Relations

RelationTypeDescription
HasOne<R>1:1The foreign key is on the related table.
HasMany<R>1:NThe foreign key is on the related table.
BelongsTo<R>1:1 / N:1The foreign key is on the current table.
BelongsToMany<R>N:NUses an intermediate pivot table.
HasManyThrough<R, I>Distant 1:NAccess a related model via an intermediate model.

Polymorphic Relations

RelationDescription
MorphTo<T>The "child" side. Stores _id and _type. Can belong to multiple parent types.
MorphOne<R>A parent has one polymorphic child (e.g., a Post has one Image).
MorphMany<R>A parent has many polymorphic children (e.g., a Video has many Comments).
MorphToMany<R>Many-to-Many via a polymorphic pivot table (e.g., Tags on Posts and Videos).

Mixins (Concerns)

Traits that compose the Model behavior.

Core Mixins (Included by Default)

These are automatically mixed into Model and are always available.

MixinFunctionality
HasCastsHandles type conversion (hydration/dehydration) of attributes based on columns or casts.
HasEventsProvides lifecycle hooks (onSaving, onDeleted, etc.).
HasRelationshipsManages relation definitions and lazy/eager loading.
HasAttributeHelpersAdds typed accessors like model.string('name') or bracket notation model['name'].
HasGuardsAttributesManages mass-assignment protection (fillable, guarded).

Optional Mixins

Add these to your specific models to enable enhanced features.

MixinFunctionality
HasTimestampsAutomatically manages created_at and updated_at.
HasSoftDeletesEnables "trash" functionality. Records are marked as deleted (via deleted_at) instead of removed.
HasUuidsAutomatically generates UUID v4 strings for the primary key (client-side generation).
HasGlobalScopesAllows registering queries that apply to every fetch operation (e.g., Multi-Tenancy).

Exceptions

Exceptions thrown by the framework that you should handle.

ExceptionCause
ModelNotFoundExceptionThrown by findOrFail() or firstOrFail() when no result is found.
QueryExceptionThrown when a raw SQL error occurs (syntax error, constraint violation).
TransactionExceptionThrown when a transaction fails or is explicitly rolled back.
InvalidQueryExceptionThrown when the QueryBuilder detects unsafe or malformed input.
DatabaseNotInitializedExceptionThrown if you try to use a Model before calling setDatabase().

Annotations & Tooling

SymbolDescription
@fillableAnnotation to mark a class for Code Generation.
@bavardPivotAnnotation to mark a Pivot class for code generation.
MockDatabaseSpyA test utility to spy on generated SQL and mock results. Supports normalized keys for flexible assertions.

Released under the MIT License.