Registry / database / sql-simulator

sql-simulator

JSON →
library0.54.0jsnpmunverified

SQL Simulator is a JavaScript library that parses SQL DDL queries (CREATE, ALTER, DROP) for tables and enum types, producing an AST representing the datastore schema. Current version 0.54.0 is in active development with frequent releases. Unlike full SQL parsers, it focuses solely on schema-defining statements, making it lightweight for code generation, schema comparison, and migration tooling. It supports CREATE/ALTER/DROP TABLE and TYPE (enums only), but not indexes, triggers, or functions. Parser output is structured as an object with database, schemas, and tables.

npm install sql-simulator
INSTALL
IMPORT
SIG · SQL-SIMULATOR
S
sql-simulator
databasejavascriptv0.54.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default import
import sqlSimulator from 'sql-simulator'
const sqlSimulator = require('sql-simulator')
Package is ESM-only; CommonJS require will fail.
parse
import { parse } from 'sql-simulator'
const { parse } = require('sql-simulator')
Named export available for direct parsing.
default with destructuring
import sql from 'sql-simulator'; const ast = sql.parse('...')
import { default as sql } from 'sql-simulator'
Default export is an object with methods; destructuring the default is unnecessary.

Shows parsing CREATE TABLE, CREATE TYPE, ALTER TABLE, and DROP TABLE queries into an AST.

import sqlSimulator from 'sql-simulator'; const sql = ` CREATE TABLE users ( id SERIAL PRIMARY KEY, name TEXT NOT NULL, email TEXT UNIQUE ); CREATE TYPE mood AS ENUM ('happy', 'sad', 'neutral'); ALTER TABLE users ADD COLUMN age INTEGER; DROP TABLE old_logs; `; try { const ast = sqlSimulator.parse(sql); console.log(JSON.stringify(ast, null, 2)); } catch (err) { console.error('Parse error:', err.message); }
Debug
Known issues
breakingAs of v0.50.0, the package switched to ESM-only. CommonJS require() will throw.
fix
Use import syntax or upgrade to Node >=16 with { "type": "module" }.
affects: >=0.50.0
deprecatedThe function 'simulate' was renamed to 'parse' in v0.30.0. 'simulate' still works but logs a deprecation warning.
fix
Use 'parse' instead of 'simulate'.
affects: >=0.30.0 <0.35.0
gotchaCREATE INDEX statements are not supported and will cause a parse error.
fix
Strip or skip CREATE INDEX statements before parsing.
affects: all
gotchaOnly enumerated types (ENUM) are supported in CREATE TYPE; composite types will fail.
fix
Ensure only ENUM types are passed.
affects: all
breakingv0.40.0 changed the AST structure: 'tables' array now uses 'columns' instead of 'fields'.
fix
Update code accessing 'fields' to use 'columns'.
affects: >=0.40.0
Errors
Common errors & fixes
ERR_REQUIRE_ESM: require() of ES Module /path/to/node_modules/sql-simulator/dist/index.js from /path/to/app.js not supported.
Package is ESM-only; CommonJS require() fails.
fix
Use import syntax or set type:module in package.json.
TypeError: sqlSimulator.parse is not a function
Importing default incorrectly (e.g., import { parse } from 'sql-simulator' when using default).
fix
Use import sqlSimulator from 'sql-simulator'; then sqlSimulator.parse(...).
Unexpected token: 'INDEX'
CREATE INDEX is not supported.
fix
Remove or skip CREATE INDEX statements.
Unsupported type: composite
Only ENUM types are supported for CREATE TYPE.
fix
Use ENUM type or skip composite type definitions.
Upgrade
Version history
0.54.0latest on npm
Audit
Dependencies
moorequiredTokenizer for SQL lexing
nearleyrequiredParser generator for grammar
Agent activity
10 hits · last 30 days
node
8
Meta
1
OpenAI (training)
1
Resources
sql-simulator — npm install sql-simulator · libregistry