sql-compiler

raw JSON →
1.0.2 verified Fri May 01 auth: no javascript

A SQL compiler library for Node.js (version 1.0.2, latest) that tokenizes, parses, and generates SQL, allowing dynamic editing of the query AST. Releases are infrequent (no recent updates). Differentiators: provides a full pipeline (tokenizer, parser, code generator) and a manipulable AST, unlike pure SQL parsers. Requires Node >=15 (ESM-only). Not widely adopted; use with caution for production.

error Error [ERR_MODULE_NOT_FOUND]: Cannot find module 'sql-compiler/tokenizer'
cause Missing full path with .mjs extension in import.
fix
import tokenizer from 'sql-compiler/modules/tokenizer.mjs';
error TypeError: parser(...) is not a function or parser is undefined
cause Parser import path is incorrect.
fix
import parser from 'sql-compiler/modules/parser/parser.mjs';
error TypeError: Cannot read properties of undefined (reading 'value')
cause Passing raw SQL string to parser instead of tokens.
fix
parser(tokenizer(sql)) and ensure tokenizer returns an array.
gotcha Imports require full file paths with .mjs extension; using shorthand paths fails.
fix Use exact import paths like 'sql-compiler/modules/tokenizer.mjs'.
gotcha Parser expects a token array from tokenizer; passing raw SQL string to parser will fail.
fix Always tokenize first: parser(tokenizer(sql)).
gotcha The AST structure is specific to this compiler; direct manipulation may break if query structure differs.
fix Study the AST format in documentation before manipulating.
gotcha Only SELECT statements are fully supported; other SQL statements may not parse correctly.
fix Currently limited to SELECT; avoid using for INSERT/UPDATE/DELETE.
npm install sql-compiler
yarn add sql-compiler
pnpm add sql-compiler

Parses a SQL SELECT query, manipulates the AST to add a column, and generates the updated SQL string.

import tokenizer from 'sql-compiler/modules/tokenizer.mjs';
import parser from 'sql-compiler/modules/parser/parser.mjs';
import codeGenerator from 'sql-compiler/modules/code-generator.mjs';

const sql = `SELECT 'Leonardo' AS name, 'Dicaprio' AS lastname FROM dual l`;
const tokens = tokenizer(sql);
const { value: ast } = parser(tokens);
ast.value.columns.push({ type: 'NUMERIC', value: '20', alias: 'age' });
const updatedSql = codeGenerator(ast);
console.log(updatedSql);
// Output: SELECT 'Leonardo' AS name, 'Dicaprio' AS lastname, 20 AS age FROM dual l