[data-reveal]{opacity:1!important;transform:none!important}
DocumentationData API

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.

npm install @antelopejs/interface-data-api @antelopejs/interface-api @antelopejs/interface-database-decorators

Each package provides different imports:

  • @antelopejs/interface-data-api provides DataController, RegisterDataController and DefaultRoutes.
  • @antelopejs/interface-api provides Controller.
  • @antelopejs/interface-database-decorators provides 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.

src/data-api/tasks.ts typescript
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:

DeclarationResponsibility
@ModelReference() and @Model(TaskModel)Connect the generated handlers to a model instance. Without them, requests fail with Missing model key.
@AccessSelect the fields a request can read or write. A table column alone does not expose an API field.
@ListableSelect 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:

RouteMethodDescription
GET /api/tasks/get?id=<id>DefaultRoutes.GetRetrieve a single task
GET /api/tasks/listDefaultRoutes.ListList all tasks
POST /api/tasks/newDefaultRoutes.NewCreate a new task
PUT /api/tasks/edit?id=<id>DefaultRoutes.EditUpdate an existing task
DELETE /api/tasks/delete?id=<id>DefaultRoutes.DeleteDelete 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.

Note

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.

typescript
{
  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.

  1. Install @antelopejs/interface-auth and configure its provider.
  2. Import Authentication from that package.
  3. 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:

ControllerRoute map entriesAuthentication
Public reads at /api/articlesget: DefaultRoutes.Get, list: DefaultRoutes.ListNone
Protected writes at /api/articlesnew: DefaultRoutes.New, edit: DefaultRoutes.Edit, delete: DefaultRoutes.DeleteClass-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.

typescript
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:

src/db/tables/task.ts typescript
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.