Documentation•Reference

HazelJS Frame Package

npm downloads

@hazeljs/​frame queries typed records, CSV files, and Parquet files in Node.js. Build queries synchronously and execute them asynchronously with DuckDB.

Quick Reference

  • Purpose — Filter, group, aggregate, and sort tabular data with a typed API.
  • When to use — Query a file or an array of records, or expose a host-managed dataset to an agent.
  • Key concepts — Column schemas, immutable query builders, versioned plans, and async execution.
  • Dependencies — Native DuckDB bindings through @duckdb/​node-api. @hazeljs/​agent is an optional peer.
  • Common patterns — Define a source, build a query, then call .toRows().
  • Common mistakes — Applying .limit() before sorting a top-N result or relying on inference for an all-null column.

Purpose

Frame gives a Node.js service one query API for in-memory records and file-backed data. Column names and value types flow from the source schema into filters, aggregations, and result rows.

Architecture

graph TD
  A["Typed records, CSV, or Parquet"] --> B["FrameQuery"]
  B --> C["Versioned query plan"]
  C --> D["DuckDB execution"]
  D --> E["Result rows"]

Advantages

  • File-backed queries scan and aggregate in DuckDB before returning the result to JavaScript.
  • Immutable queries let several reports share the same source and filters.
  • Bigint columns support exact integer totals, including currency in minor units.

Installation

The examples target HazelJS 2.2.0 and require Node.js 20 or later. Frame is a standalone package with native DuckDB bindings.

npm install @hazeljs/​frame@2.2.0

Quick Start

Filter and aggregate records

import { count, frame, sum } from '@hazeljs/​frame';

const sales = frame([
  { store: 'Stockholm', completed: true, amountOre: 4500n },
  { store: 'Malmö', completed: true, amountOre: 2300n },
  { store: 'Stockholm', completed: true, amountOre: 1500n },
  { store: 'Malmö', completed: false, amountOre: 9000n },
]);

const topStores = await sales
  .filter('completed', 'eq', true)
  .groupBy('store')
  .aggregate({ grossOre: sum('amountOre'), orders: count() })
  .sortBy('grossOre', 'desc')
  .thenBy('store', 'asc')
  .head()
  .toRows();
/​/​ [{ store: 'Stockholm', grossOre: 6000n, orders: 2n },
/​/​  { store: 'Malmö', grossOre: 2300n, orders: 1n }]

Query builders are immutable, so you can use sales again to build another query. Operations apply in call order. Call .limit() after sorting to return the top results, or before filtering to filter a smaller input. Use .explain() to inspect the execution plan.

API Overview

Supported column kinds are string, number, boolean, bigint, and timestamp.

OperationBehavior
filter(column, op, value)Supports eq, neq, lt, lte, gt, and gte. Use isNull and isNotNull without a value.
sum(column)Sums numeric values and returns null for an all-null group. Use bigint for exact integer currency amounts.
count() and count(column)Counts all rows or the non-null values in a column, respectively. Counts are returned as bigint.
sortBy() and thenBy()Sort by one or more columns with nulls last.
head(n = 5) and tail(n = 5)Select the first or last rows in query order. Negative counts drop rows from the opposite end.
toPlan() and serialize()Export the versioned plan for inspection or transport.

Recipes

Select the first or last rows

Use pandas-style head() and tail() to preview a Frame query. Call .toRows() to execute it.

import { frame } from '@hazeljs/​frame';

const orders = frame([{ id: 3 }, { id: 1 }, { id: 2 }]).sortBy('id');

const first = await orders.head(2).toRows(); /​/​ ids 1, 2
const last = await orders.tail(2).toRows();  /​/​ ids 2, 3
const skipFirst = await orders.tail(-1).toRows(); /​/​ ids 2, 3
const skipLast = await orders.head(-1).toRows();  /​/​ ids 1, 2

Both methods default to five rows. Pass zero for an empty result. tail() keeps the order of the selected rows. Sort on a unique key or add a tie-breaker with .thenBy() when you need a deterministic result. A positive tail() and negative head() may scan the full input to the slice in DuckDB.

Query CSV and Parquet

import { fromCsv, fromParquet } from '@hazeljs/​frame';

const schema = {
  store: 'string',
  completed: 'boolean',
  amountOre: 'bigint',
} as const;

const csv = fromCsv('.​/sales.csv', { schema });
const parquet = fromParquet('.​/sales.parquet', { schema });

const completedSales = await csv
  .filter('completed', 'eq', true)
  .select('store', 'amountOre')
  .toRows();

CSV requires an explicit schema. Supply one for typed Parquet queries and for in-memory columns whose type cannot be inferred.

Expose a registered dataset to an agent

The host registers a dataset with DatasetRegistry and specifies the columns a caller can see. It can apply a tenant filter through the dataset's scope, then execute a caller's versioned query plan with runRegisteredQuery. The caller supplies a dataset ID and a validated plan. Only the host controls the data source and execution options such as the row limit and timeout.

Use decodeToolRows for tool responses containing encoded bigints. For ordinary Node.js queries, .toRows() returns JavaScript values directly.

Analyze Frame results

Use the Stats Frame adapters to summarize a number column in DuckDB or collect aligned columns for inference.

Benchmarks

Frame recorded the lowest median on all five verified queries in the 100,000-row benchmark against Simple Data Analysis, pandas, and danfo.js. The top-five stores query took 4.8 ms on an Apple M3 Max. Frame benchmark results

Run the Frame suite from the HazelJS source checkout with Node.js 20 or later and Python 3. It compares Frame, Simple Data Analysis, pandas, and danfo.js on the same rows and reports warm query times separately from cold process startup. Frame, Simple Data Analysis, and pandas read Parquet. danfo.js reads the matching CSV.

npm ci
npm run build -w @hazeljs/​frame
cd benchmarks/​frame
npm ci
python3 -m venv .venv
.venv/​bin/​pip install pandas pyarrow
npm start