Database
Index Definitions
In AQL, indexes are defined as part of the schema definition rather than created at runtime. The schema declaration specifies which fields to index and what type of index to use.
IndexDefinition
interface IndexDefinition {
fields?: string[]; // Fields for compound indexes
multi?: boolean; // Multi-valued index
}
Define Indexes
Indexes are declared in the indexes property of each table definition:
import { Schema } from "@antelopejs/interface-database";
const schema = new Schema("myapp", {
users: {
fields: { name: "string", email: "string", role: "string" },
indexes: {
email: {}, // Simple index (field name = index name)
role: {}, // Simple index
name_role: { fields: ["name", "role"] }, // Compound index
},
},
});
Simple Indexes
When fields is omitted, the index name is used as the field name:
indexes: {
email: {}, // Creates an index on the "email" field
}
Compound Indexes
Provide the fields array for indexes spanning multiple fields:
indexes: {
full_name: { fields: ["firstName", "lastName"] },
}
Multi Indexes
Set multi: true for array fields where each element should be individually indexed:
indexes: {
tags: { multi: true },
}
Use Indexes in Queries
Once defined, indexes are available for getAll() and between() operations:
const db = schema.instance();
const users = db.table("users");
// Lookup by secondary index
const admins = await users.getAll("admin", "role");
// Range query on an indexed field
const recent = await users.between("createdAt", new Date("2024-01-01"), new Date());
For more details, see Index Management.
Insert Results
The insert method on Table<T> returns a Query<string[]> that resolves to an array of the generated or provided primary keys for the inserted documents.
Table Definitions
In AQL, tables are defined as part of a schema. Each table definition specifies the fields and indexes for that table.