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

Data API Documentation

Overview

The Data API interface provides a declarative approach to building RESTful APIs with automatic CRUD operations. It connects your database models directly to API endpoints, handling validation, access control, foreign key resolution, and pagination with minimal boilerplate.

Key Features

  • Database-to-API Mapping - Expose database tables as REST endpoints through data controllers.
  • Default CRUD Routes - Built-in get, list, new, edit, and delete operations that work out of the box.
  • Access Control - Define read-only, write-only, or read-write permissions per field, with per-action overrides.
  • Field Validation - Attach custom validation functions to individual fields and enforce mandatory fields per operation.
  • Listable Fields and Pagination - Control which fields appear in list responses, with support for multiple pluck modes and paginated results.
  • Sorting - Mark fields as sortable to enable ordering in list queries.
  • Foreign Key Resolution - Automatically resolve relationships between tables with optional field plucking.
  • Joined Fields - Flatten a scalar field from another table onto each row so it can be sorted, filtered, and searched natively in the database.
  • Computed Fields - Merge in-database computed expressions (row-local values or aggregates over other tables) onto each row, natively sortable, filterable, and pageable.
  • Filters - Add default or custom filter logic to list endpoints using query parameters.
  • Modifier Integration - Automatic data transformation (encryption, hashing, localization) through database-decorators.

Dependencies

This interface relies on the following AntelopeJS packages:

Quick Start

The following example creates a full CRUD API for a users table.

import { Controller } from "@antelopejs/interface-api";
import {
  DataController,
  DefaultRoutes,
  RegisterDataController,
} from "@antelopejs/interface-data-api";
import {
  Access,
  AccessMode,
  Listable,
  Mandatory,
  ModelReference,
} from "@antelopejs/interface-data-api/metadata";
import {
  BasicDataModel,
  Field,
  Index,
  Model,
  RegisterTable,
  Table,
} from "@antelopejs/interface-database-decorators";

// Define the database table
@RegisterTable("users")
class User extends Table {
  @Index({ primary: true })
  @Field("string")
  declare _id: string;

  @Index()
  @Field("string")
  declare email: string;

  @Field("string")
  declare name: string;

  @Field("string")
  declare password: string;
}

// Create the database model
class UserModel extends BasicDataModel(User, "users") {}

// Create the data controller
@RegisterDataController()
class UserAPI extends DataController(
  User,
  DefaultRoutes.All,
  Controller("/users"),
) {
  @ModelReference()
  @Model(UserModel, "my-database")
  declare userModel: UserModel;

  @Listable()
  @Access(AccessMode.ReadOnly)
  declare _id: string;

  @Listable()
  @Access(AccessMode.ReadWrite)
  @Mandatory("new", "edit")
  declare email: string;

  @Listable()
  @Access(AccessMode.ReadWrite)
  @Mandatory("new")
  declare name: string;

  @Access(AccessMode.WriteOnly)
  @Mandatory("new")
  declare password: string;
}

This generates the following REST endpoints:

MethodEndpointDescription
GET/users/get?id=<id>Retrieve a single user
GET/users/list?limit=10List users with pagination
POST/users/newCreate a new user
PUT/users/edit?id=<id>Update an existing user
DELETE/users/delete?id=<id>Delete a user

Documentation Sections

  • Data Controllers - Create and configure data controllers
  • Routes - Default routes, custom routes, and route options
  • Access Rights - Control field read/write permissions
  • Validators - Field-level validation and mandatory fields
  • Listable Fields - Pagination, pluck modes, and sorting
  • Foreign Keys - Establish relationships between tables
  • Filters - Add filtering capabilities to list endpoints
  • Modifiers - Automatic data transformation with database-decorators
  • Joined Fields - Flatten a remote field onto rows for native sort/filter
  • Computed Fields - In-database computed values, sortable and pageable natively