logo

qbjs

API Reference

Types

API reference for TypeScript types and interfaces

This page documents all TypeScript types and interfaces exported by qbjs.

AST Types

QueryAST

The main AST structure representing a parsed query. This is the stable contract between parser and compilers.

interface QueryAST {
  fields: string[] | null;
  pagination: Pagination;
  sort: SortSpec[];
  filter: FilterNode | null;
}
PropertyTypeDescription
fieldsstring[] | nullFields to select (null means all fields)
paginationPaginationPagination specification
sortSortSpec[]Sort specifications (empty array means no sorting)
filterFilterNode | nullFilter expression (null means no filtering)

Pagination

Pagination specification with offset-based pagination.

interface Pagination {
  offset: number;
  limit: number;
}
PropertyTypeDescription
offsetnumberNumber of items to skip
limitnumberMaximum number of items to return

SortSpec

Specification for sorting a single field.

interface SortSpec {
  field: string;
  direction: SortDirection;
}
PropertyTypeDescription
fieldstringThe field name to sort by
directionSortDirectionThe sort direction (ascending or descending)

SortDirection

Sort direction for ordering query results.

type SortDirection = 'asc' | 'desc';

Filter Types

FilterNode

A filter node can be either a field filter or a logical filter. This allows for arbitrarily complex filter expressions.

type FilterNode = FieldFilter | LogicalFilter;

FieldFilter

A filter condition on a specific field.

interface FieldFilter {
  type: 'field';
  field: string;
  operator: FilterOperator;
  value: unknown;
}
PropertyTypeDescription
type'field'Discriminator for filter node type
fieldstringThe field name to filter on
operatorFilterOperatorThe comparison operator
valueunknownThe value to compare against

LogicalFilter

A logical combination of filter conditions.

interface LogicalFilter {
  type: 'logical';
  operator: LogicalOperator;
  conditions: FilterNode[];
}
PropertyTypeDescription
type'logical'Discriminator for filter node type
operatorLogicalOperatorThe logical operator (and, or, not)
conditionsFilterNode[]The conditions to combine

FilterOperator

Filter operators supported by the query system.

type FilterOperator =
  | 'eq'
  | 'eqi'
  | 'ne'
  | 'nei'
  | 'lt'
  | 'lte'
  | 'gt'
  | 'gte'
  | 'in'
  | 'notIn'
  | 'contains'
  | 'containsi'
  | 'notContains'
  | 'notContainsi'
  | 'startsWith'
  | 'endsWith'
  | 'null'
  | 'notNull'
  | 'between';
OperatorDescription
eqEqual
eqiEqual (case-insensitive)
neNot equal
neiNot equal (case-insensitive)
ltLess than
lteLess than or equal
gtGreater than
gteGreater than or equal
inIn array
notInNot in array
containsContains substring
containsiContains substring (case-insensitive)
notContainsDoes not contain substring
notContainsiDoes not contain substring (case-insensitive)
startsWithStarts with
endsWithEnds with
nullIs null
notNullIs not null
betweenBetween two values

LogicalOperator

Logical operators for combining filter conditions.

type LogicalOperator = 'and' | 'or' | 'not';

Type Guards

isFieldFilter

Type guard to check if a FilterNode is a FieldFilter.

function isFieldFilter(node: FilterNode): node is FieldFilter;

isLogicalFilter

Type guard to check if a FilterNode is a LogicalFilter.

function isLogicalFilter(node: FilterNode): node is LogicalFilter;

Example

import { isFieldFilter, isLogicalFilter } from '@qbjs/core';

function processFilter(node: FilterNode) {
  if (isFieldFilter(node)) {
    console.log(`Field: ${node.field}, Operator: ${node.operator}`);
  } else if (isLogicalFilter(node)) {
    console.log(`Logical: ${node.operator}, Conditions: ${node.conditions.length}`);
  }
}

Parser Types

QueryInput

Input query parameters that can be parsed.

interface QueryInput {
  fields?: string | null;
  page?: string | number | null;
  limit?: string | number | null;
  sort?: string | null;
  filter?: unknown;
}

ParserResult

Result of parsing a query string.

interface ParserResult {
  ast: QueryAST | null;
  errors: ParseError[];
  warnings: ParseWarning[];
}

ParseError

Parse error returned when parsing fails.

interface ParseError {
  code: 'INVALID_FIELD' | 'INVALID_OPERATOR' | 'INVALID_VALUE' | 'EXCEEDED_LIMIT' | 'SECURITY_VIOLATION';
  field: string;
  message: string;
  path: string[];
}

ParseWarning

Parse warning returned for non-fatal issues.

interface ParseWarning {
  code: 'FIELD_IGNORED' | 'LIMIT_CAPPED' | 'DEFAULT_APPLIED';
  field: string;
  message: string;
  suggestion?: string;
}

Compiler Types

CompilerResult

Result of compiling a QueryAST to an ORM query.

interface CompilerResult<T> {
  query: T | null;
  errors: CompileError[];
  warnings: CompileWarning[];
}

CompileError

An error that occurred during compilation.

interface CompileError {
  code: CompileErrorCode;
  field: string;
  message: string;
}

type CompileErrorCode = 'UNKNOWN_COLUMN' | 'TYPE_MISMATCH' | 'UNSUPPORTED_OPERATOR';

CompileWarning

A warning that occurred during compilation.

interface CompileWarning {
  code: CompileWarningCode;
  field: string;
  message: string;
  suggestion?: string;
}

type CompileWarningCode = 'COLUMN_IGNORED' | 'OPERATOR_FALLBACK' | 'PERFORMANCE_HINT';

QueryCompiler

Interface for query compilers that transform AST to ORM queries.

interface QueryCompiler<TTable, TQuery> {
  compile(ast: QueryAST, table: TTable): CompilerResult<TQuery>;
}

Security Types

SecurityConfig

Security configuration for query validation.

interface SecurityConfig {
  allowedFields?: string[];
  allowedRelations?: string[];
  maxLimit?: number;
  operators?: FilterOperator[];
  defaultLimit?: number;
  defaultPage?: number;
}

ResolvedSecurityConfig

Fully resolved security configuration with all required fields.

interface ResolvedSecurityConfig {
  allowedFields: string[];
  allowedRelations: string[];
  maxLimit: number;
  operators: FilterOperator[];
  defaultLimit: number;
  defaultPage: number;
}

SecurityError

Security validation error.

interface SecurityError {
  code: 'FIELD_NOT_ALLOWED' | 'OPERATOR_NOT_ALLOWED' | 'LIMIT_EXCEEDED';
  field: string;
  message: string;
  path: string[];
}

SecurityWarning

Security validation warning.

interface SecurityWarning {
  code: 'LIMIT_CAPPED';
  field: string;
  message: string;
  originalValue: number;
  cappedValue: number;
}

SecurityValidationResult

Result of security validation.

interface SecurityValidationResult {
  valid: boolean;
  errors: SecurityError[];
  warnings: SecurityWarning[];
  ast: QueryAST | null;
}

Query Builder Types

QueryBuilder

Query builder interface that orchestrates parsing, validation, and compilation.

interface QueryBuilder<TTable, TQuery> {
  parse(input: QueryInput): QueryBuilderParseResult;
  parseFromUrl(url: string): QueryBuilderParseResult;
  compile(ast: QueryAST, table: TTable): QueryBuilderCompileResult<TQuery>;
  execute(input: QueryInput, table: TTable): QueryBuilderExecuteResult<TQuery>;
  executeFromUrl(url: string, table: TTable): QueryBuilderExecuteResult<TQuery>;
  getSecurityConfig(): SecurityConfig;
}

CreateQueryBuilderOptions

Options for creating a query builder.

interface CreateQueryBuilderOptions<TTable, TQuery> {
  config?: SecurityConfig;
  compiler: QueryCompiler<TTable, TQuery>;
}

QueryBuilderParseResult

Result of parsing a query through the query builder.

interface QueryBuilderParseResult {
  ast: QueryAST | null;
  errors: QueryBuilderError[];
  warnings: QueryBuilderWarning[];
}

QueryBuilderCompileResult

Result of compiling a query through the query builder.

interface QueryBuilderCompileResult<TQuery> {
  query: TQuery | null;
  errors: CompileError[];
  warnings: CompileWarning[];
}

QueryBuilderExecuteResult

Result of executing a full query through the query builder.

interface QueryBuilderExecuteResult<TQuery> {
  query: TQuery | null;
  ast: QueryAST | null;
  errors: QueryBuilderError[];
  warnings: QueryBuilderWarning[];
}

QueryBuilderError

Combined error type for query builder operations.

type QueryBuilderError = ParseError | SecurityError | CompileError;

QueryBuilderWarning

Combined warning type for query builder operations.

type QueryBuilderWarning = ParseWarning | SecurityWarning | CompileWarning;

Constants

FILTER_OPERATORS

All supported filter operators as an array for validation.

const FILTER_OPERATORS: readonly FilterOperator[] = [
  'eq', 'eqi', 'ne', 'nei', 'lt', 'lte', 'gt', 'gte',
  'in', 'notIn', 'contains', 'containsi', 'notContains', 'notContainsi',
  'startsWith', 'endsWith', 'null', 'notNull', 'between'
] as const;

LOGICAL_OPERATORS

All supported logical operators as an array for validation.

const LOGICAL_OPERATORS: readonly LogicalOperator[] = ['and', 'or', 'not'] as const;

SORT_DIRECTIONS

All supported sort directions as an array for validation.

const SORT_DIRECTIONS: readonly SortDirection[] = ['asc', 'desc'] as const;

DEFAULT_PAGINATION

Default pagination values.

const DEFAULT_PAGINATION: Pagination = {
  offset: 0,
  limit: 10,
};

DEFAULT_SECURITY_CONFIG

Default security configuration.

const DEFAULT_SECURITY_CONFIG: ResolvedSecurityConfig = {
  allowedFields: [],
  allowedRelations: [],
  maxLimit: 100,
  operators: [...FILTER_OPERATORS],
  defaultLimit: 10,
  defaultPage: 1,
};

On this page