Repository<T extends OrmEntity> class
A repository for a model of type T.
CRUD helpers for a single model type.
Reads delegate to the Query builder. Writes build MutationPlan objects that are executed by the active driver.
Obtain a repository from DataSource.repo<T>(),
QueryContext.repository<T>(), or Model.repository<T>().
final repo = dataSource.repo<$User>();
final inserted = await repo.insert(
$UserInsertDto(email: 'john@example.com'),
);
final updated = await repo.update(
$UserUpdateDto(name: 'John'),
where: {'id': inserted.id},
);
final exists = await repo.exists(where: {'email': 'john@example.com'});
CRUD helpers for a single model type.
Reads delegate to the Query builder. Writes build MutationPlan objects that are executed by the active driver.
Obtain a repository from DataSource.repo<T>(),
QueryContext.repository<T>(), or Model.repository<T>().
final repo = dataSource.repo<$User>();
final inserted = await repo.insert(
$UserInsertDto(email: 'john@example.com'),
);
final updated = await repo.update(
$UserUpdateDto(name: 'John'),
where: {'id': inserted.id},
);
final exists = await repo.exists(where: {'email': 'john@example.com'});
Provides methods for inserting, updating, upserting and deleting models. The repository is composed of several mixins, each providing a specific set of operations:
- RepositoryInsertMixin - Insert operations
- RepositoryUpdateMixin - Update operations
- RepositoryUpsertMixin - Upsert operations
- RepositoryDeleteMixin - Delete operations
- RepositoryPreviewMixin - Preview methods for all operations
- RepositoryHelpersMixin - Internal helper methods
- Inheritance
-
- Object
- RepositoryBase<
T> - Repository
- Mixed-in types
Constructors
-
Repository({required ModelDefinition<
T> definition, required String driverName, required Future<MutationResult> runMutation(MutationPlan plan), required StatementPreview describeMutation(MutationPlan plan), required void attachRuntimeMetadata(Object? model), QueryContext? context})
Properties
- attachRuntimeMetadata → void Function(Object? model)
-
no setteroverride
- codecs → ValueCodecRegistry
-
no setteroverride
-
definition
→ ModelDefinition<
T> -
no setteroverride
- describeMutation → StatementPreview Function(MutationPlan plan)
-
no setteroverride
- driverName → String
-
no setteroverride
- hashCode → int
-
The hash code for this object.
no setterinherited
- queryContext → QueryContext?
-
no setteroverride
-
runMutation
→ Future<
MutationResult> Function(MutationPlan plan) -
no setteroverride
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
all(
) → Future< List< T> > -
Returns all records for this model.
inherited
-
applyWhere(
Query< T> query, Object? where, {String feature = 'where'}) → Query<T> -
Applies a repository-style
whereinput toquery.inherited -
count(
{Object? where}) → Future< int> -
Returns the count of records that match
where.inherited -
delete(
Object where) → Future< int> -
Deletes records that match the provided
whereclause.inherited -
deleteById(
Object id) → Future< int> -
Deletes a single record by primary key
id.inherited -
deleteByIds(
List< Object> ids) → Future<int> -
Deletes multiple records by their primary key
ids.inherited -
deleteByIdsReturning(
List< Object> ids) → Future<List< T> > -
Deletes multiple records by their primary key
idsand returns the models.inherited -
deleteByKeys(
List< Map< keys) → Future<String, Object?> >int> -
Deletes records from the database by their
keys.inherited -
deleteMany(
List< Object> wheres) → Future<int> -
Deletes multiple records using a list of
wheres.inherited -
deleteWhereManyReturning(
List< Object> wheres) → Future<List< T> > -
Deletes records using flexible where inputs and returns hydrated models.
inherited
-
exists(
{Object? where}) → Future< bool> -
Returns
truewhen at least one record matcheswhere.inherited -
extractPrimaryKey(
Object input) → Object? -
Extracts the primary key value from an input.
inherited
-
fieldFor(
String name) → FieldDefinition -
inherited
-
find(
Object id) → Future< T?> -
Finds a record by primary key.
inherited
-
findMany(
List< Object> ids) → Future<List< T> > -
Finds multiple records by their primary keys.
inherited
-
findOrFail(
Object id) → Future< T> -
Finds a record by primary key or throws when missing.
inherited
-
first(
{Object? where}) → Future< T?> -
Returns the first record that matches
where, ornullwhen none exist.inherited -
firstOrFail(
{Object? where}) → Future< T> -
Returns the first record that matches
whereor throws when none exist.inherited -
forceDelete(
Object where) → Future< int> -
Permanently deletes records that match
where, bypassing soft deletes.inherited -
insert(
Object model) → Future< T> -
Inserts a single
modelinto the database and returns the inserted record.inherited -
insertInputToMap(
Object input, {required bool applySentinelFiltering}) → Map< String, Object?> -
Converts an insert input to a map suitable for insertion.
inherited
-
insertMany(
List< Object> inputs, {bool returning = true}) → Future<List< T> > -
Inserts multiple items into the database and returns the inserted records.
inherited
-
insertOrIgnore(
Object model) → Future< int> -
Inserts a single item into the database, ignoring any conflicts.
inherited
-
insertOrIgnoreMany(
List< Object> inputs) → Future<int> -
Inserts multiple items into the database, ignoring any conflicts.
inherited
-
isTrackedModel(
Object input) → bool -
Checks if the input is a tracked model (has ModelAttributes).
inherited
-
jsonUpdatesFor(
T model, JsonUpdateBuilder< T> ? builder) → List<JsonUpdateClause> -
inherited
-
mapResult(
List< T> original, MutationResult result, bool returning) → List<T> -
inherited
-
mapResultWithInputs(
List< T> original, List<Map< inputMaps, MutationResult result, bool returning, {bool canCreateFromInputs = true}) → List<String, Object?> >T> -
Maps mutation results back to model instances.
inherited
-
mutationRowFromModel(
T model, {JsonUpdateBuilder< T> ? jsonUpdates}) → MutationRow -
inherited
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
previewDelete(
Map< String, Object?> key) → StatementPreview -
Returns the statement preview for deleting the row described by
key.inherited -
previewDeleteByKeys(
List< Map< keys) → StatementPreviewString, Object?> > -
Returns the statement preview for deleting records by
keys.inherited -
previewDeleteMany(
List< Object> wheres) → StatementPreview -
Returns the statement preview for deleting using flexible where inputs.
inherited
-
previewDeleteWhere(
Object where) → StatementPreview -
Returns the statement preview for deleting a single where clause.
inherited
-
previewInsert(
Object model) → StatementPreview -
Returns the statement preview for inserting a single item.
inherited
-
previewInsertMany(
List< Object> inputs) → StatementPreview -
Returns the statement preview for inserting multiple items.
inherited
-
previewInsertOrIgnoreMany(
List< Object> inputs) → StatementPreview -
Returns the statement preview for inserting items while ignoring conflicts.
inherited
-
previewUpdateMany(
List< Object> inputs, {Map<String, Object?> ? where, JsonUpdateBuilder<T> ? jsonUpdates}) → StatementPreview -
Returns the statement preview for updating items.
inherited
-
previewUpsertMany(
List< Object> inputs, {List<String> ? uniqueBy, List<String> ? updateColumns, JsonUpdateBuilder<T> ? jsonUpdates}) → StatementPreview -
Returns the statement preview for upserting items.
inherited
-
refresh(
T model) → Future< T> -
Reloads
modelfrom the database by its primary key.inherited -
replicate(
T model) → T -
Creates an in-memory copy of
modelwithout persisting it.inherited -
requireKeys(
List< Map< keys) → voidString, Object?> > -
inherited
-
requireModels(
List< T> models, String method) → void -
inherited
-
resolveColumnName(
String input) → String -
inherited
-
resolveColumnNames(
List< String> ? fields) → List<String> ? -
inherited
-
restore(
Object where) → Future< int> -
Restores soft-deleted records that match
where.inherited -
save(
Object model) → Future< T> -
Inserts or upserts
model, based on whether a primary key is present.inherited -
saveMany(
List< Object> models) → Future<List< T> > -
Saves multiple inputs by inserting those without primary keys and
upserting those with primary keys.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
-
trash(
Object where) → Future< int> -
Soft-deletes or deletes records that match
where.inherited -
update(
Object model, {Object? where, JsonUpdateBuilder< T> ? jsonUpdates}) → Future<T> -
Updates a single
modelin the database and returns the updated record.inherited -
updateInputToMap(
Object input) → Map< String, Object?> -
Converts an update input to a map suitable for updates.
inherited
-
updateMany(
List< Object> inputs, {Object? where, JsonUpdateBuilder<T> ? jsonUpdates}) → Future<List< T> > -
Updates multiple items in the database and returns the updated records.
inherited
-
updateManyRaw(
List< Object> inputs, {Object? where, JsonUpdateBuilder<T> ? jsonUpdates}) → Future<MutationResult> -
Updates multiple items in the database and returns the raw result.
inherited
-
upsert(
Object model, {List< String> ? uniqueBy, List<String> ? updateColumns, JsonUpdateBuilder<T> ? jsonUpdates}) → Future<T> -
Upserts a single item in the database and returns the result.
inherited
-
upsertMany(
List< Object> inputs, {List<String> ? uniqueBy, List<String> ? updateColumns, JsonUpdateBuilder<T> ? jsonUpdates}) → Future<List< T> > -
Upserts multiple items in the database and returns the results.
inherited
-
whereInputToMap(
Object? input) → Map< String, Object?> ? -
Converts a "where" input to a map suitable for WHERE clauses.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited