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

Table Definitions

In AQL, tables are defined as part of a schema. Each table definition specifies the fields and indexes for that table.

TableDefinition

interface TableDefinition {
  fields: Record<string, FieldType>;
  indexes: Record<string, IndexDefinition>;
}

FieldType

Field types describe the structure of each field:

type FieldType =
  | string                              // Primitive type name (e.g., "string", "number")
  | Array<StringFieldType>              // Array of a given type
  | { [subfield: string]: StringFieldType } // Nested object
  | import("io-ts").Mixed;              // io-ts codec at the field root

A field can be described either with the string-token vocabulary (primitives, arrays, nested records) or with a single io-ts codec at the root of the field. Codecs are not mixed inside string-tree records — use io-ts combinators (t.type, t.array, …) to nest within a codec.

import * as t from "io-ts";

const Order = {
  fields: {
    id: "string",
    status: t.union([t.literal("open"), t.literal("closed")]),
    meta: t.type({ source: t.string, retries: t.number }),
  },
  indexes: {},
};

Define Tables in a Schema

Tables are declared in the SchemaDefinition passed to the Schema constructor:

import { Schema } from "@antelopejs/interface-database";

const schema = new Schema("myapp", {
  users: {
    fields: {
      name: "string",
      email: "string",
      age: "number",
      address: {
        street: "string",
        city: "string",
        zip: "string",
      },
      tags: ["string"],
    },
    indexes: {
      email: {},
      age: {},
    },
  },
  posts: {
    fields: {
      title: "string",
      content: "string",
      authorId: "string",
      publishedAt: "date",
    },
    indexes: {
      authorId: {},
      publishedAt: {},
    },
  },
});

Access Tables

After creating a schema instance, access tables by name:

await schema.createInstance();

const users = schema.instance().table("users");
const posts = schema.instance().table("posts");

For type-safe access, provide a generic type parameter:

interface User {
  _id: string;
  name: string;
  email: string;
  age: number;
}

const users = schema.instance().table<User>("users");

For more details on table operations, see Table Operations.