-
Notifications
You must be signed in to change notification settings - Fork 1
Architecture: Generic Data API
fulleni edited this page Jan 18, 2026
·
2 revisions
The API Server employs a Metadata-Driven Architecture for data operations. Instead of writing individual controllers and routes for every data model (e.g., HeadlinesController, TopicsController), the system uses a single, unified gateway that handles CRUD operations dynamically.
The entire data layer is abstracted into a Registry Pattern.
-
Route:
/api/v1/data(Collection) and/api/v1/data/[id](Item). -
Mechanism: The route handler inspects the
modelquery parameter (e.g.,?model=headline) and delegates execution to a registered handler.
-
Location:
lib/src/registry/model_registry.dart -
Role: Defines what data exists. It maps a string key (e.g.,
'headline') to aModelConfigobject that knows how to:- Deserialize JSON into a Dart object (using
fromJson). - Serialize a Dart object into JSON (using
toJson). - Extract the ID from an object.
- Determine ownership (for permission checks).
- Deserialize JSON into a Dart object (using
-
Location:
lib/src/registry/data_operation_registry.dart -
Role: Defines how to interact with data. It maps the same string key to specific repository functions:
-
itemCreators: Function to create an item. -
allItemsReaders: Function to read a collection (with filtering/sorting). -
itemUpdaters: Function to update an item. -
itemDeleters: Function to delete an item.
-
-
Role: Acts as the traffic controller:
-
Identify: Reads the
modelfrom the request. -
Lookup: Retrieves the correct
ModelConfigand Operation from the registries. - Execute: Calls the repository function.
- Respond: Formats the result into a standardized JSON response.
-
Identify: Reads the
The system enforces data integrity at the gateway level, before any business logic is executed.
-
Schema Validation: Because every request body is deserialized using the
ModelConfig.fromJsonmethod (which usesjson_serializablewithchecked: true), any missing required fields, incorrect data types, or invalid enum values trigger an immediateBadRequestException. -
Middleware Validation: The
DataFetchMiddlewarepre-validates that the requested item exists before the handler runs. -
Ownership Checks: The
OwnershipCheckMiddlewareautomatically enforces rules like "Only the author can edit this comment" based on thegetOwnerIdfunction in theModelConfig.
The generic API is not a "dumb pipe"; it supports sophisticated data retrieval capabilities powered by the DataMongodb client.
-
Deep Filtering: Clients can pass a JSON
filterparameter that supports complex MongoDB-style queries (e.g.,{"status": "published", "tags": {"$in": ["tech"]}}). -
Multi-Field Sorting: The
sortparameter allows sorting by multiple fields (e.g.,sort=createdAt:desc,title:asc). -
Cursor-Based Pagination: Unlike offset pagination which gets slower as you go deeper, this system uses Keyset Pagination (via the
cursorparameter). This ensures constant-time performance (O(1)) even when paging through millions of records.
For comprehensive details regarding licensing, including trial and commercial options for the entire toolkit, please refer to the toolkit organization page.