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

Routes

Overview

Routes define which operations are available on a data controller. The module provides predefined route configurations that automatically generate endpoints with the correct HTTP methods, parameter extraction, and response handling.

Default Routes

The DefaultRoutes namespace provides five built-in routes covering standard CRUD operations.

import { DefaultRoutes } from "@antelopejs/interface-data-api";
RouteObjectHTTP MethodEndpoint Pattern
GetDefaultRoutes.GetGET/resource/get?id=<id>
ListDefaultRoutes.ListGET/resource/list
NewDefaultRoutes.NewPOST/resource/new
EditDefaultRoutes.EditPUT/resource/edit?id=<id>
DeleteDefaultRoutes.DeleteDELETE/resource/delete?id=<id>

Use DefaultRoutes.All to include all five routes at once.

Route Configuration

Pass a route definition object to DataController to control which endpoints are created.

// All CRUD routes
@RegisterDataController()
class UserAPI extends DataController(
  User,
  DefaultRoutes.All,
  Controller("/users"),
) {
  // ...
}

// Only specific routes
@RegisterDataController()
class UserAPI extends DataController(
  User,
  {
    get: DefaultRoutes.Get,
    list: DefaultRoutes.List,
    new: DefaultRoutes.New,
  },
  Controller("/users"),
) {
  // edit and delete endpoints are not created
}

Route Details

Get

Retrieves a single record by its identifier.

Endpoint: GET /resource/get

Query Parameters:

ParameterRequiredDescription
idYesRecord identifier

Example:

GET /users/get?id=user-123

Response:

{
  "_id": "user-123",
  "name": "Bob",
  "email": "[email protected]"
}

List

Retrieves multiple records with pagination, sorting, and filtering support.

Endpoint: GET /resource/list

Query Parameters:

ParameterRequiredDefaultDescription
limitNo10Maximum number of records to return
offsetNo0Number of records to skip
sortKeyNo-Field to sort by (must be marked @Sortable)
sortDirectionNoascSort direction: asc or desc

Example:

GET /users/list?limit=10&offset=0&sortKey=name&sortDirection=asc

Response:

{
  "results": [
    { "_id": "user-123", "name": "Alice", "email": "[email protected]" },
    { "_id": "user-456", "name": "Bob", "email": "[email protected]" }
  ],
  "total": 42,
  "offset": 0,
  "limit": 10
}

New

Creates a new record. The request body must be a JSON object with the fields to set.

Endpoint: POST /resource/new

Body: JSON object with field values.

Example:

POST /users/new
Content-Type: application/json

{
  "name": "Bob",
  "email": "[email protected]",
  "password": "secure123"
}

Response:

["user-789"]

Before inserting, the insert event is triggered on any table modifiers attached to the database table.

Edit

Updates an existing record by its identifier.

Endpoint: PUT /resource/edit

Query Parameters:

ParameterRequiredDescription
idYesRecord identifier

Body: JSON object with fields to update.

Example:

PUT /users/edit?id=user-123
Content-Type: application/json

{
  "name": "Robert",
  "email": "[email protected]"
}

Response:

200 OK

Before updating, the update event is triggered on any table modifiers attached to the database table.

Delete

Deletes one or more records by their identifiers.

Endpoint: DELETE /resource/delete

Query Parameters:

ParameterRequiredDescription
idYesRecord identifier(s). Pass multiple id parameters to delete several records at once.

Example:

DELETE /users/delete?id=user-123

Response:

200 OK

Route Options with WithOptions

The DefaultRoutes.WithOptions function customizes route behavior by overriding query parameters or route settings.

DefaultRoutes.WithOptions(callback, options?, endpoint?)
ParameterDescription
callbackThe route callback to customize (e.g., DefaultRoutes.Edit).
optionsAn object with option overrides. These values take precedence over query parameters.
endpointAn optional custom endpoint path. Defaults to the route key name.

Example

const routes = {
  edit: DefaultRoutes.Edit,
  quickEdit: DefaultRoutes.WithOptions(DefaultRoutes.Edit, {
    noMandatory: "true",
  }),
};

@RegisterDataController()
class UserAPI extends DataController(User, routes, Controller("/users")) {
  // /users/edit enforces mandatory fields
  // /users/quickEdit skips mandatory field validation
}

Available Options per Route

Get:

OptionTypeDescription
indexstringUse a secondary index instead of the primary key
noForeignstringSkip foreign key resolution

List:

OptionTypeDescription
maxPagenumberMaximum allowed value for limit (caps the page size)
noForeignbooleanSkip foreign key resolution
noPluckbooleanReturn all database fields instead of plucked fields
pluckModestringSelect a named pluck mode (see @Listable)

New:

OptionTypeDescription
noMandatorystringSkip mandatory field validation

Edit:

OptionTypeDescription
indexstringUse a secondary index instead of the primary key
noMandatorystringSkip mandatory field validation

Custom Routes

For operations beyond CRUD, define a custom route callback with func, args, and method properties.

import { Context } from "@antelopejs/interface-api";
import {
  DataController,
  DefaultRoutes,
  RegisterDataController,
} from "@antelopejs/interface-data-api";
import { Parameters } from "@antelopejs/interface-data-api/components";

const Search = {
  func: async (ctx, params) => {
    // Custom search logic
    return { results: [] };
  },
  args: [Context(), Parameters.List()],
  method: "get",
};

@RegisterDataController()
class UserAPI extends DataController(
  User,
  {
    ...DefaultRoutes.All,
    search: Search,
  },
  Controller("/users"),
) {
  // All default routes plus a custom /users/search endpoint
}

The args array contains parameter decorators that extract values from the request context. Each decorator corresponds to a parameter in the func callback, in order.

Error Handling

Default routes return standard HTTP error responses:

StatusCondition
400 Bad RequestValidation fails, missing required fields, or invalid parameters
404 Not FoundRecord does not exist
500 Internal Server ErrorDatabase operation fails

Error messages are returned as plain text in the response body.

Next Steps

See the access rights documentation to learn how to control field read/write permissions.