npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@elastic/esql

v1.3.0

Published

A set of ts tools to parse, build and transform ES|QL queries programmatically.

Readme

The purpose of this package is to provide comprehensive ES|QL functionality including low-level parsing, building, traversal, pretty-printing and manipulation features on top of a custom compact AST representation.

Installation

npm install @elastic/esql
yarn add @elastic/esql
pnpm add @elastic/esql

Requirements

  • Node.js >= 18.0.0

Contents of the package

Creating an ES|QL AST

This package offers 3 tools that allow creating an AST for a query.

The parser allows to convert a query in text form into an AST.

import { Parser } from '@elastic/esql';

const src = 'FROM index | WHERE col0 > 100';
const { root, errors } = await Parser.parse(src);

The composer provides a high-level, secure, and developer-friendly way to build ES|QL queries, allowing to get both an AST or string representation of them.

import { esql } from '@elastic/esql';

const param = 123; // Dynamic parameter, e.g. received from the UI.

const query = esql`
  FROM index
    | WHERE @timestamp >= ${{ param }}
    | SORT @timestamp DESC
    | KEEP service.name, log.level`;

query.pipe`LIMIT 10`;

Check also the synth API for building independent nodes.

The builder is a low level API that allows to create AST nodes.

import { Builder } from '@elastic/esql';

const limitCommandASTNode = Builder.command({
    name: 'limit',
    args: [Builder.expression.literal.integer(10)],
});

Traversing an ES|QL AST

This package exposes 2 ways of traversing an ES|QL AST.

The walker class is simpler to use, the developer can provide a set of callbacks which are called when the walker visits a specific type of node.

import { Walker } from '@elastic/esql';

const walker = new Walker({
  visitCommand: (node: ESQLCommand) => {
    // Called for every command node.
  },
  visitFunction: (fn: ESQLFunction) => {
    // Called every time a function expression is visited.
  },
});

walker.walk(ast);

The visitor API provides a feature-rich way to traverse the ES|QL AST. It is more powerful than the Walker API, as it allows to traverse the AST in a more flexible way. Said that, it's also more complicated to use as it does not automatically traverse the entire tree, read its dedicated documentation to get insights on it.

import { Visitor } from '@elastic/esql';

new Visitor()
  .on('visitExpression', (ctx) => console.log(ctx.node.type))
  .on('visitCommand', (ctx) => [...ctx.visitArguments()])
  .on('visitQuery', (ctx) => [...ctx.visitCommands()])
  .visitQuery(root);

Modifying an ES|QL AST

The mutate API provides methods to navigate and modify the AST.

import { Parser, mutate, BasicPrettyPrinter } from '@elastic/esql';

const { root } = Parser.parse('FROM index METADATA _lang');

// [ '_lang' ]
console.log([...mutate.commands.from.metadata.list(root)]); 

mutate.commands.from.metadata.upsert(root, '_id');

// [ '_lang', '_id' ]
console.log([...mutate.commands.from.metadata.list(root)]); 

const src = BasicPrettyPrinter.print(root);

// FROM index METADATA _lang, _id
console.log(src); 

Pretty printing

The pretty_print API lets you format the AST to text.

import { parse, WrappingPrettyPrinter } from '@elastic/esql';

const src = 'FROM index | WHERE x > 100 | LIMIT 100';
const { root } = parse(src, { withFormatting: true });
const text = WrappingPrettyPrinter.print(root, { multiline: true });

/**
  FROM index
  | WHERE x > 100
  | LIMIT 100
*/
console.log(text);

Licence

Licensed under Elastic License 2.0.