@@ -26,77 +26,116 @@ npm install sqlparser-rs
2626## Quick Start
2727
2828``` typescript
29- import { Parser , GenericDialect , PostgreSqlDialect } from ' sqlparser-rs' ;
29+ import { parse , format , validate } from ' sqlparser-rs' ;
3030
31- // Simple parsing
32- const statements = Parser . parse (' SELECT * FROM users' , new GenericDialect () );
31+ // Simple parsing (uses generic dialect by default)
32+ const statements = parse (' SELECT * FROM users' );
3333console .log (statements );
3434
35- // With specific dialect
36- const pgStatements = Parser .parse (
37- ' SELECT * FROM users WHERE id = $1' ,
38- new PostgreSqlDialect ()
39- );
35+ // With specific dialect (string name)
36+ const pgStatements = parse (' SELECT * FROM users WHERE id = $1' , ' postgresql' );
4037
4138// Format SQL
42- const formatted = Parser . format (' select * from users' , new GenericDialect () );
39+ const formatted = format (' select * from users' );
4340console .log (formatted ); // "SELECT * FROM users"
4441
4542// Validate SQL
4643try {
47- Parser . validate (' SELEC * FROM users' , new GenericDialect () );
44+ validate (' SELEC * FROM users' );
4845} catch (error ) {
4946 console .log (' Invalid SQL:' , error .message );
5047}
5148```
5249
5350## API
5451
55- ### Parser
52+ ### Top-Level Functions
5653
57- The main class for parsing SQL.
58-
59- #### Static Methods
54+ The simplest way to use the parser:
6055
6156``` typescript
62- // Parse SQL into statements
63- const statements = Parser .parse (sql : string , dialect : Dialect ): Statement [];
64-
65- // Parse and return JSON string
66- const json = Parser .parseToJson (sql : string , dialect : Dialect ): string ;
57+ import { parse , format , validate } from ' sqlparser-rs' ;
6758
68- // Parse and return formatted SQL string
69- const formatted = Parser .parseToString (sql : string , dialect : Dialect ): string ;
59+ // Parse SQL - dialect defaults to 'generic'
60+ parse (' SELECT * FROM users' );
61+ parse (' SELECT * FROM users' , ' postgresql' );
62+ parse (' SELECT * FROM users' , ' mysql' );
7063
7164// Format SQL (round-trip through parser)
72- const formatted = Parser .format (sql : string , dialect : Dialect ): string ;
65+ format (' select * from users' ); // "SELECT * FROM users"
66+
67+ // Validate SQL syntax (throws ParserError if invalid)
68+ validate (' SELECT * FROM users' ); // true
69+ ```
70+
71+ ### Dialect Options
7372
74- // Validate SQL syntax
75- const isValid = Parser .validate (sql : string , dialect : Dialect ): boolean ;
73+ Dialects can be specified as strings or class instances:
7674
77- // Get list of supported dialects
78- const dialects = Parser .getSupportedDialects (): string [];
75+ ``` typescript
76+ import { parse , PostgreSqlDialect } from ' sqlparser-rs' ;
77+
78+ // String dialect names (recommended)
79+ parse (' SELECT $1' , ' postgresql' );
80+ parse (' SELECT `col` FROM t' , ' mysql' );
81+
82+ // Or dialect instances
83+ parse (' SELECT $1' , new PostgreSqlDialect ());
7984```
8085
81- #### Instance Methods (Builder Pattern)
86+ Supported dialect strings: ` 'generic' ` , ` 'ansi' ` , ` 'mysql' ` , ` 'postgresql' ` , ` 'sqlite' ` , ` 'snowflake' ` , ` 'redshift' ` , ` 'mssql' ` , ` 'clickhouse' ` , ` 'bigquery' ` , ` 'duckdb' ` , ` 'databricks' ` , ` 'hive' ` , ` 'oracle' `
87+
88+ ### Parser Class
89+
90+ For advanced use cases with options:
8291
8392``` typescript
8493import { Parser , PostgreSqlDialect } from ' sqlparser-rs' ;
8594
95+ // Static methods (same as top-level functions)
96+ Parser .parse (' SELECT 1' );
97+ Parser .parse (' SELECT 1' , ' postgresql' );
98+ Parser .format (' select 1' );
99+ Parser .validate (' SELECT 1' );
100+ Parser .parseToJson (' SELECT 1' ); // Returns JSON string
101+ Parser .parseToString (' SELECT 1' ); // Returns formatted string
102+ Parser .getSupportedDialects (); // Returns dialect names
103+
104+ // Builder pattern for custom options
86105const parser = new Parser (new PostgreSqlDialect ())
87- .withRecursionLimit (50 ) // Set max recursion depth
88- .withOptions ({ // Set parser options
89- trailingCommas: true
90- });
106+ .withRecursionLimit (50 )
107+ .withOptions ({ trailingCommas: true });
91108
92109const statements = parser .parse (' SELECT * FROM users' );
93110```
94111
112+ ### WASM Initialization
113+
114+ The WASM module auto-initializes on import. For most use cases, no action is needed.
115+
116+ If you need to ensure WASM is ready before calling sync APIs (e.g., in tests):
117+
118+ ``` typescript
119+ import { ready , parse } from ' sqlparser-rs' ;
120+
121+ // Wait for WASM to be ready
122+ await ready ();
123+
124+ // Now sync APIs are guaranteed to work
125+ const ast = parse (' SELECT 1' );
126+ ```
127+
95128### Dialects
96129
97- All dialects from the upstream Rust crate are supported:
130+ All dialects from the upstream Rust crate are supported. You can use string names directly with ` parse() ` , ` format() ` , and ` validate() ` :
98131
99132``` typescript
133+ // String names (recommended)
134+ parse (' SELECT 1' , ' postgresql' );
135+ parse (' SELECT 1' , ' mysql' );
136+ parse (' SELECT 1' , ' bigquery' );
137+
138+ // Or import dialect classes for type safety
100139import {
101140 GenericDialect , // Permissive, accepts most SQL syntax
102141 AnsiDialect , // ANSI SQL standard
@@ -111,20 +150,21 @@ import {
111150 DuckDbDialect , // DuckDB
112151 DatabricksDialect , // Databricks
113152 HiveDialect , // Apache Hive
153+ OracleDialect , // Oracle
114154} from ' sqlparser-rs' ;
115155
116- // Create dialect from string
156+ // Create dialect from string programmatically
117157import { dialectFromString } from ' sqlparser-rs' ;
118158const dialect = dialectFromString (' postgresql' ); // Returns PostgreSqlDialect instance
119159```
120160
121161### Error Handling
122162
123163``` typescript
124- import { Parser , GenericDialect , ParserError } from ' sqlparser-rs' ;
164+ import { parse , ParserError } from ' sqlparser-rs' ;
125165
126166try {
127- Parser . parse (' SELEC * FROM users' , new GenericDialect () );
167+ parse (' SELEC * FROM users' );
128168} catch (error ) {
129169 if (error instanceof ParserError ) {
130170 console .log (' Parse error:' , error .message );
@@ -140,9 +180,10 @@ try {
140180Full TypeScript types are provided for the AST:
141181
142182``` typescript
143- import type { Statement , Query , Expr , Ident , ObjectName } from ' sqlparser-rs' ;
183+ import { parse } from ' sqlparser-rs' ;
184+ import type { Statement , Query } from ' sqlparser-rs' ;
144185
145- const statements: Statement [] = Parser . parse (' SELECT 1' , new GenericDialect () );
186+ const statements: Statement [] = parse (' SELECT 1' );
146187
147188// Statement is a discriminated union type
148189for (const stmt of statements ) {
@@ -191,7 +232,7 @@ npm test
191232This package follows the upstream [ sqlparser-rs] ( https://github.com/apache/datafusion-sqlparser-rs ) version:
192233
193234- ** Major.Minor** matches the upstream Rust crate version
194- - ** Patch** is for TypeScript binding fixes (e.g., ` 0.60.3 ` = upstream ` 0.60.0 ` + binding fix)
235+ - ** Patch** is for TypeScript binding fixes (e.g., ` 0.60.3-rc2 ` = upstream ` 0.60.0 ` + binding fix)
195236
196237| This package | sqlparser-rs |
197238| --------------| --------------|
0 commit comments