query
An LLM-authorable relational query language with an in-memory runtime and a SQL converter.
@aeye/query lets a model (or a developer) write a query as plain JSON against types you register. The query is validated before it runs, has an inferred output type and typed bind parameters, can be cost-bounded, runs in memory, and converts to SQL for Postgres, SQLite, SQL Server or portable ANSI.
npm install @aeye/query@aeye/core, @aeye/schema (the model-facing grammar), @aeye/gin (tables and query signatures as gin types) and zod (its own value checks) install with it.
Why not let the model write SQL?
- Checked before execution. Unknown tables and fields, mismatched types and conflicting parameters are reported as diagnostics the model can fix, with "did you mean" suggestions.
- A schema built from your data model. The grammar the model writes against is generated from your registered types, so it can be locked to real type and field names. See LLM Tooling.
- The physical database stays private. Row and field security, computed fields, hidden joins and search columns live in backing code the model never sees.
- One query, two runtimes. The same query runs in memory and converts to SQL.
Quick start
Register types, wire in rows, then validate, run and convert a query.
import { createRegistry, QueryEngine, arrayExecutor, type QueryDef, type TypeDef } from '@aeye/query';
const userDef: TypeDef = {
name: 'user',
fields: [
{ name: 'id', type: { kind: 'number', whole: true } },
{ name: 'name', type: { kind: 'text' } },
{ name: 'age', type: { kind: 'number', whole: true }, nullable: true },
],
// A unique single-field index makes `id` the identity (join) key.
indexes: [{ exprs: [{ expr: { kind: 'field-ref', source: 'user', field: 'id' }, count: 1 }] }],
count: 1000, // estimated rows, for cost estimates
};
const registry = createRegistry();
registry.registerType(registry.parseType(userDef));
const engine = new QueryEngine(registry, {
executors: { user: arrayExecutor([{ id: 1, name: 'Ada', age: 36 }]) },
});
const query: QueryDef = {
kind: 'select',
fields: [{ expr: { kind: 'field-ref', source: 'user', field: 'name' } }],
from: { kind: 'type', type: 'user' },
where: [{
kind: 'comparison', op: '>',
left: { kind: 'field-ref', source: 'user', field: 'age' },
right: { kind: 'literal', value: 30 },
}],
};
const problems = engine.validateQuery(query);
if (!problems.hasErrors) {
const { rows } = await engine.run(query); // [{ name: 'Ada' }]
const { sql, params } = engine.toSQL(query, 'postgres');
}Give it to a model
buildQueryTool is a ready @aeye/core tool. Its arguments are generated from your types, the model's answer is decoded into a runnable query, and calling the tool runs it.
import { buildQueryTool } from '@aeye/query';
const queryTool = buildQueryTool(engine, { depth: 'paired' });
const assistant = ai.prompt({
name: 'assistant',
description: 'Answers questions about the data',
content: 'Answer the question by querying the data: {{question}}',
input: (input: { question: string }) => input,
tools: [queryTool],
});Where it fits
| Package | Role |
|---|---|
@aeye/core | The Tool the query is offered to a model through |
@aeye/schema | The model-facing grammar, rendered for each model's dialect |
@aeye/gin | Table rows and query arguments as gin types |
@aeye/query | Types, queries, validation, runtime, SQL, DDL |
Read next
- Types & Fields: the data model, relations, indexes, text casing.
- Queries & Expressions: statement kinds, expressions, the
e.*builder. - Running & SQL: parameters, filters, pagination, drill-down, cost, dialects.
- Backing & Security: computed fields, row and field security, permissions, search.
- Functions & Operators: the built-in library and your own.
- Tables & DDL: table shapes, DDL and generated functions.
- LLM Tooling: schemas, schema depth, the query tool, describing the engine.
The package also ships aeye-query.md, a fuller reference for coding agents, and runnable examples.