CRUD REST endpoints generated from a database table: declare a data controller and its fields, and get get, list, new, edit and delete routes with access rules, validation and pagination.
DataControllerand@RegisterDataController()turn a table class into a controller with the default routes you pick fromDefaultRoutes.@Access,@Mandatoryand@Validatordecide which fields each route reads and writes, and check them.@Listable,@Sortableand@Filtershape the paginatedlistroute.@Foreign,@Joinedand@Computedresolve references, flatten remote fields and add values computed in the database.
The package runs in your module and has no implementing module of its own. The project needs modules that implement the interfaces it builds on:
| Interface | Module |
|---|---|
@antelopejs/interface-api |
@antelopejs/api |
@antelopejs/interface-database |
@antelopejs/mongodb or @antelopejs/rethinkdb |
Add the package and the interfaces your controllers import to your module:
pnpm add @antelopejs/interface-data-api @antelopejs/interface-api @antelopejs/interface-database-decoratorsThen add the implementations to the project:
ajs project modules add @antelopejs/api @antelopejs/mongodbimport { Controller } from "@antelopejs/interface-api";
import {
DataController,
DefaultRoutes,
RegisterDataController,
} from "@antelopejs/interface-data-api";
import {
Access,
AccessMode,
Listable,
Mandatory,
ModelReference,
Sortable,
} from "@antelopejs/interface-data-api/metadata";
import {
BasicDataModel,
Field,
Model,
RegisterSchema,
RegisterTable,
Table,
} from "@antelopejs/interface-database-decorators";
@RegisterTable("tasks", "todo")
class Task extends Table {
declare _id: string;
@Field("string")
declare summary: string;
}
class TaskModel extends BasicDataModel(Task, "tasks") {}
@RegisterDataController()
class TaskAPI extends DataController(
Task,
DefaultRoutes.All,
Controller("/tasks"),
) {
@ModelReference()
@Model(TaskModel)
declare taskModel: TaskModel;
@Listable()
@Access(AccessMode.ReadOnly)
declare _id: string;
@Listable()
@Sortable()
@Mandatory("new")
@Access(AccessMode.ReadWrite)
declare summary: string;
}
export async function construct() {
await RegisterSchema("todo");
}The controller serves GET /tasks/get?id=, GET /tasks/list, POST /tasks/new, PUT /tasks/edit?id= and DELETE /tasks/delete?id=. Fields without @Access stay out of every response.
- Introduction
- Data controllers
- Routes and loaded-model guards
- Access rights
- Validators
- Listable fields
- Foreign keys
- Filters
- Modifiers
- Joined fields
- Computed fields
Use with @antelopejs/core 1.x and the following peer dependencies:
| Package | Supported | Tested with |
|---|---|---|
@antelopejs/interface-api |
>=0.0.15 <1.0.0 |
0.0.15 |
@antelopejs/interface-api-util |
>=0.1.3 <1.0.0 |
0.1.3 |
@antelopejs/interface-core |
>=0.2.4 <1.0.0 |
0.2.4 |
@antelopejs/interface-database |
>=0.1.10 <1.0.0 |
0.1.11 |
@antelopejs/interface-database-decorators |
>=0.1.10 <1.0.0 |
0.1.10 |
Read CONTRIBUTING.md before you open a pull request. The local checks are:
pnpm install
pnpm lint
pnpm test