Base interface for all storable entities in EntiDB.
Any class that needs to be stored in a EntiDB collection must implement this interface. The interface provides a minimal contract for serialization while allowing full flexibility in class design.
Implementation Requirements
- Provide an id getter (can be
nullfor new entities) - Implement toMap to serialize fields (excluding
id) - Create a
fromMapfactory constructor for deserialization
Example Implementation
class Task implements Entity {
@override
final String? id;
final String title;
final bool completed;
final DateTime? dueDate;
Task({
this.id,
required this.title,
this.completed = false,
this.dueDate,
});
@override
Map<String, dynamic> toMap() => {
'title': title,
'completed': completed,
if (dueDate != null) 'dueDate': dueDate!.toIso8601String(),
};
factory Task.fromMap(String id, Map<String, dynamic> map) => Task(
id: id,
title: map['title'] as String,
completed: map['completed'] as bool? ?? false,
dueDate: map['dueDate'] != null
? DateTime.parse(map['dueDate'] as String)
: null,
);
}
ID Generation
When inserting an entity with a null id, EntiDB automatically generates
a UUID v4 identifier. If an id is provided, it will be used as-is.
// Auto-generated ID
await collection.insert(Task(title: 'Buy milk'));
// Custom ID
await collection.insert(Task(id: 'task-001', title: 'Buy eggs'));
Nested Objects
For entities containing nested objects, serialize them recursively:
class Order implements Entity {
@override
final String? id;
final List<OrderItem> items;
Order({this.id, required this.items});
@override
Map<String, dynamic> toMap() => {
'items': items.map((item) => item.toMap()).toList(),
};
factory Order.fromMap(String id, Map<String, dynamic> map) => Order(
id: id,
items: (map['items'] as List)
.map((item) => OrderItem.fromMap(item))
.toList(),
);
}
Type Safety
The generic collection system ensures type safety at compile time:
final tasks = await db.collection<Task>('tasks', fromMap: Task.fromMap);
// Returns Task, not Map or Document
final task = await tasks.get('task-001');
print(task?.title); // Full autocomplete support
// Type-safe queries
final completed = await tasks.find(
QueryBuilder().whereEquals('completed', true).build(),
);
// completed is List<Task>
Properties
Methods
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toMap(
) → Map< String, dynamic> - Converts this entity to a map for storage.
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited