Data API
Generate CRUD endpoints from database tables in AntelopeJS, and choose their routes, paths and authentication.
Prerequisites
The Data API builds on the API and database interfaces. Install all three interface packages in your module.
Each package provides different imports:
@antelopejs/interface-data-apiprovidesDataController,RegisterDataControllerandDefaultRoutes.@antelopejs/interface-apiprovidesController.@antelopejs/interface-database-decoratorsprovides table definitions and models.
The Data API needs a registered table. To set one up, follow the Database guide.
Choose generated routes deliberately
Use generated CRUD routes for a table only when its read and write rules match the operations the routes expose. Import the controller file from the module entry point, register its schema, and configure an API provider and a database provider. The interface package alone starts neither service.
For private or tenant-owned records, define authorization before you expose DefaultRoutes.All. Authentication verifies a token. It does not scope records to the caller. Start with only the routes you need. If ownership rules require server-side filtering or checks, write custom handlers.
Create a data controller
The DataController function returns a controller class with CRUD endpoints for a database table. It takes three arguments: the table class, a route definition object, and a base controller. The @RegisterDataController() decorator registers the generated controller with the framework.
import {
DataController,
RegisterDataController,
DefaultRoutes,
} from "@antelopejs/interface-data-api";
import { Controller } from "@antelopejs/interface-api";
import { BasicDataModel, Model } from "@antelopejs/interface-database-decorators";
import {
Access, AccessMode, Listable, Mandatory, ModelReference,
} from "@antelopejs/interface-data-api/metadata";
import { Task } from "../db/tables/task";
class TaskModel extends BasicDataModel(Task) {}
@RegisterDataController()
class TaskDataController extends DataController(
Task,
{
get: DefaultRoutes.Get,
list: DefaultRoutes.List,
new: DefaultRoutes.New,
edit: DefaultRoutes.Edit,
delete: DefaultRoutes.Delete,
},
Controller("/api/tasks"),
) {
@ModelReference()
@Model(TaskModel)
declare model: TaskModel;
@Listable()
@Access(AccessMode.ReadOnly)
declare _id: string;
@Listable()
@Mandatory("new")
@Access(AccessMode.ReadWrite)
declare userId: string;
@Listable()
@Mandatory("new")
@Access(AccessMode.ReadWrite)
declare title: string;
@Listable()
@Mandatory("new")
@Access(AccessMode.ReadWrite)
declare description: string;
@Listable()
@Mandatory("new")
@Access(AccessMode.ReadWrite)
declare completed: boolean;
}
The Task table is defined in Combine with the database guide. The routes need the model and field declarations to work:
| Declaration | Responsibility |
|---|---|
@ModelReference() and @Model(TaskModel) | Connect the generated handlers to a model instance. Without them, requests fail with Missing model key. |
@Access | Select the fields a request can read or write. A table column alone does not expose an API field. |
@Listable | Select the fields in list responses. The field also needs read access. |
@Mandatory("new") | Require the field on create, but not on a partial edit. |
This controller creates five endpoints:
| Route | Method | Description |
|---|---|---|
GET /api/tasks/get?id=<id> | DefaultRoutes.Get | Retrieve a single task |
GET /api/tasks/list | DefaultRoutes.List | List all tasks |
POST /api/tasks/new | DefaultRoutes.New | Create a new task |
PUT /api/tasks/edit?id=<id> | DefaultRoutes.Edit | Update an existing task |
DELETE /api/tasks/delete?id=<id> | DefaultRoutes.Delete | Delete a task |
The last path segment is the key from the route definition (get, list, new, edit or delete). Single-record routes take the record ID in the id query parameter, not in a path segment.
DataController is a function that returns a class. It is not a decorator. Write extends DataController(...) to create your data controller.
Use DefaultRoutes.All
DefaultRoutes.All is shorthand for all five CRUD routes: Get, List, New, Edit and Delete.
To use it, replace only the second argument of DataController with DefaultRoutes.All. Keep the model property and the field decorators from the controller above.
The shorthand produces the same endpoints as the explicit route definition. To include only some routes, or to give a route its own options, use the explicit form.
Expose selected routes
To expose only some routes, list a subset in the route definition. The Data API generates no route you leave out.
For a read-only controller, pass this route map as the second argument. If you want a separate public path, change the base path to /api/public/tasks.
{
get: DefaultRoutes.Get,
list: DefaultRoutes.List,
}
This controller exposes only GET endpoints, with no create, update or delete operation. Use it for public or read-only APIs.
Add authentication
@Authentication from @antelopejs/interface-auth also works as a class decorator. On a Data API controller, it registers an auth check as a computed property of the controller. The framework evaluates that check for every request before any handler runs.
- Install
@antelopejs/interface-authand configure its provider. - Import
Authenticationfrom that package. - Add
@Authentication()below@RegisterDataController()on the controller above. Keep the model and field declarations.
The task tutorial shows the full authenticated class.
Every route now requires a valid authentication token. The framework rejects a request without one before it reaches the route handler.
Every authenticated user can still call the same CRUD operations. A userId column or relation does not make the records owner-only. Never use an owner ID from the client as an authorization decision.
Mix public and protected routes
Class-level @Authentication() applies to every route of the controller. To serve public reads and protected writes for the same table, split the routes across two controllers mounted at the same base path. One controller is protected and the other is public.
For articles, both controllers use the Article table and its model, and each exposes a different set of routes:
| Controller | Route map entries | Authentication |
|---|---|---|
Public reads at /api/articles | get: DefaultRoutes.Get, list: DefaultRoutes.List | None |
Protected writes at /api/articles | new: DefaultRoutes.New, edit: DefaultRoutes.Edit, delete: DefaultRoutes.Delete | Class-level @Authentication() |
Each controller still needs its model reference and its field access metadata. Anyone can read the exposed article fields, and any authenticated user can call the generated writes. If that is too permissive, add a role or ownership policy.
Change a route's path
DefaultRoutes.WithOptions takes the route, an options object, and an optional third argument for the endpoint. The third argument replaces the last path segment of that route.
DefaultRoutes.WithOptions(DefaultRoutes.Get, {}, "by-id");
With this override, the Get route is GET /api/tasks/by-id?id=<id> instead of GET /api/tasks/get?id=<id>. The options object passes query-parameter defaults to the route handler, such as { noPluck: true } or { noForeign: true }. It does not configure authentication. Change the endpoint when the default path segment does not fit your API design or conflicts with another controller.
Combine with the database guide
The Data API works with tables you define with the database decorators. This table is the one the controller above exposes:
import {
Table,
RegisterTable,
Field,
Index,
Relation,
} from "@antelopejs/interface-database-decorators";
import { User } from "./user";
@RegisterTable("tasks", "main")
class Task extends Table {
@Index()
@Field("string")
@Relation({ to: () => User })
declare userId: string;
@Field("string")
declare title: string;
@Field("string")
declare description: string;
@Field("boolean")
declare completed: boolean;
}
export { Task };
Import the table and the controller from your module entry point, and await RegisterSchema("main") in construct(). The User table comes from the Database guide.
Table metadata sets the storage types. Controller metadata sets API access. A database index does not add a query parameter. To filter a list, declare the Data API filter metadata. The interface reference covers access rules, validators, filters and relations.
Verify the generated API
Send writable fields at the top level of the JSON body, not inside a data property. To create a record in the task table above, send userId, title, description and completed. An edit takes the record ID from ?id=....
A list request returns an object with results, total, offset and limit, not a bare array. Check that object, and request both an existing ID and a missing one. If you add authentication, also send a request with no token and one with an invalid token. Those checks do not replace authorization tests between two users.