{"id":26427,"library":"sqlglot-ts","title":"sqlglot-ts","description":"TypeScript port of SQLGlot — a SQL parser and transpiler with browser-first design and zero runtime dependencies. Current version 0.1.5 is an early release; DuckDB dialect is thoroughly tested against Python SQLGlot's suite (500+ test cases), while other included dialects (PostgreSQL, BigQuery, Snowflake, etc.) have partial coverage. Unique differentiator: unbundled ESM modules allow tree-shaking per dialect, keeping bundle sizes small (core ~80KB gzipped, plus ~20–40KB per dialect). Unlike alternatives (e.g., node-sql-parser, moo-based parsers), sqlglot-ts emphasizes transpilation between dialects and full AST traversal.","status":"active","version":"0.1.5","language":"javascript","source_language":"en","source_url":"https://github.com/Flamefork/sqlglot-ts","tags":["javascript","sql","parser","transpiler","ast","duckdb"],"install":[{"cmd":"npm install sqlglot-ts","lang":"bash","label":"npm"},{"cmd":"yarn add sqlglot-ts","lang":"bash","label":"yarn"},{"cmd":"pnpm add sqlglot-ts","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"ESM-only; CommonJS require() fails in Node < 22 or without experimental VM modules.","wrong":"const { parseOne } = require('sqlglot-ts')","symbol":"parseOne","correct":"import { parseOne } from 'sqlglot-ts'"},{"note":"Dialects must be imported as side effects before calling transpileOne; otherwise the default standard SQL dialect is used, leading to unexpected results.","wrong":"import { transpileOne } from 'sqlglot-ts'; transpileOne(sql, { read: 'duckdb', write: 'postgres' })","symbol":"transpileOne","correct":"import { transpileOne } from 'sqlglot-ts'; import 'sqlglot-ts/dialects/duckdb'; import 'sqlglot-ts/dialects/postgres'"},{"note":"Dialect classes are exported from 'sqlglot-ts/dialects', not the main package. Importing from 'sqlglot-ts' only yields core functions.","wrong":"import { DuckDB } from 'sqlglot-ts'","symbol":"DuckDB","correct":"import { DuckDB } from 'sqlglot-ts/dialects'"},{"note":"Expression types and classes are under 'sqlglot-ts/expressions'; re-exports from main package are not available.","wrong":"import { Column } from 'sqlglot-ts'","symbol":"exp","correct":"import * as exp from 'sqlglot-ts/expressions'"}],"quickstart":{"code":"import { parseOne, transpileOne } from 'sqlglot-ts';\nimport 'sqlglot-ts/dialects/duckdb';\nimport 'sqlglot-ts/dialects/postgres';\nconst ast = parseOne('SELECT TRY_CAST(x AS INT)');\nconsole.log(ast.sql());\nconst result = transpileOne('SELECT TRY_CAST(x AS INT)', { read: 'duckdb', write: 'postgres' });\nconsole.log(result);","lang":"typescript","description":"Parses a DuckDB-specific SQL statement and transpiles it to PostgreSQL, demonstrating dialect imports and transpilation."},"warnings":[{"fix":"Add import 'sqlglot-ts/dialects/your-dialect' at top of file before usage.","message":"Dialect side-effect imports must precede any parsing/transpilation calls.","severity":"breaking","affected_versions":">=0.1.0"},{"fix":"Check ast.errors after parsing to detect syntax errors.","message":"parseOne() does not throw on invalid SQL; returns a partial AST with errors attached.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Use import { DuckDB } from 'sqlglot-ts/dialects' when you need the class; retain side-effect imports for registration.","message":"Direct import of dialect classes from 'sqlglot-ts/dialects' is preferred over 'sqlglot-ts/dialects/duckdb' side-effect imports for type access.","severity":"deprecated","affected_versions":">=0.1.0"},{"fix":"Use transpileOne for single statement input; for multi-statement SQL, use transpile() and handle the array.","message":"transpileOne() returns a string; transpile() returns an array of strings. Mixing them up causes type errors.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Upgrade to Node.js 18 or later.","message":"Node.js >=18 required; engine restriction enforced.","severity":"breaking","affected_versions":">=0.1.0"},{"fix":"Configure your bundler (e.g., Vite: module.rules) to treat 'sqlglot-ts/dialects/*' as side-effectful.","message":"Browser bundlers may tree-shake dialect registrations if imports are not recognized as side effects.","severity":"gotcha","affected_versions":">=0.1.0"}],"env_vars":null,"last_verified":"2026-05-01T00:00:00.000Z","next_check":"2026-07-30T00:00:00.000Z","problems":[{"fix":"Ensure tsconfig.json includes 'moduleResolution': 'node16' or 'nodenext' and 'allowImportingTsExtensions': false.","cause":"Missing or misconfigured TypeScript module resolution; deep imports may not be recognized.","error":"Cannot find module 'sqlglot-ts/dialects/duckdb' or its corresponding type declarations."},{"fix":"Add import 'sqlglot-ts/dialects/duckdb' at the top of your file before any sqlglot-ts usage.","cause":"Dialect side-effect import missing before calling parse/transpile functions.","error":"Error: Dialect 'duckdb' not registered. Did you import a dialect module?"},{"fix":"Check ast.errors after parseOne() to validate; handle invalid SQL gracefully.","cause":"ast is not a proper Expression object; likely parseOne() returned an error object due to invalid SQL.","error":"TypeError: ast.walk is not a function"},{"fix":"Import the dialect class: import { Postgres } from 'sqlglot-ts/dialects' then use Postgres.generate(ast).","cause":"Dialect class not imported or incorrectly imported. generate is a static method on dialect classes.","error":"Cannot read properties of undefined (reading 'generate')"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}