Skip to content

Latest commit

 

History

History
171 lines (127 loc) · 12.6 KB

README.md

File metadata and controls

171 lines (127 loc) · 12.6 KB

INTERFACE · DATA API: part of AntelopeJS, hexagonal architecture beyond your own code. A glowing button reads Star AntelopeJS.

@antelopejs/interface-data-api

npm License Discord Documentation Star AntelopeJS

CRUD REST endpoints generated from a database table for AntelopeJS, with access rules, validation and pagination.

Documentation · Implementations · All interfaces and providers

The AntelopeJS interface that generates CRUD REST endpoints from a database table. Declare a data controller and its fields, and it serves the get, list, new, edit and delete routes with access rules, validation and pagination.

What it offers

  • DataController and @RegisterDataController() turn a table class into a controller with the default routes you pick from DefaultRoutes.
  • @Access decides which fields each route reads and writes. @Mandatory and @Validator check the written values.
  • @Listable, @Sortable and @Filter shape the paginated list route.
  • @Foreign, @Joined and @Computed resolve references, flatten remote fields and add values computed in the database.

Implementations

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

Install

Add the package and its peer interfaces to your module.

pnpm add @antelopejs/interface-data-api @antelopejs/interface-api @antelopejs/interface-api-util @antelopejs/interface-core @antelopejs/interface-database @antelopejs/interface-database-decorators

Then add the implementations to the project.

ajs project modules add @antelopejs/api @antelopejs/mongodb

Quick start

import { 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=. A field without @Access stays out of every response and every write, unless @Joined or @Computed makes it read-only.

Documentation

Compatibility

The package declares these @antelopejs/* peer dependencies.

Package Supported Tested with
@antelopejs/interface-api >=0.3.0 <1.0.0 0.2.0
@antelopejs/interface-api-util >=0.1.3 <1.0.0 0.3.0
@antelopejs/interface-core >=0.2.4 <1.0.0 0.4.0
@antelopejs/interface-database >=0.1.10 <1.0.0 0.3.0
@antelopejs/interface-database-decorators >=0.1.10 <1.0.0 0.2.0

Contributing

Read CONTRIBUTING.md before you open a pull request. Run the local checks with these commands.

pnpm install
pnpm lint
pnpm test

Part of AntelopeJS

This package belongs to AntelopeJS, the TypeScript framework where modules depend on contracts and the application chooses the provider. The open-source back office AntelopeJS DMS is built on it.

Area Contracts Providers
API interface-api · interface-api-util · interface-data-api api
Auth interface-auth auth-jwt
Database interface-database · interface-database-decorators · interface-database-catalog · interface-mongodb · interface-rethinkdb postgres · mongodb · rethinkdb
Redis interface-redis · interface-redis-scheduler redis
Email interface-email nodemailer · brevo
File storage interface-file-storage file-storage-local · file-storage-s3
Payments interface-payment · interface-subscriptions · interface-stripe stripe · mollie · checkout-com · payment-subscriptions

License

Apache-2.0