@polyglot-sql/sdk 0.1.9 → 0.1.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +73 -2
- package/dist/cdn/polyglot.esm.js +2062 -1997
- package/dist/index.cjs +5375 -0
- package/dist/index.d.cts +14760 -0
- package/dist/index.d.ts +116 -0
- package/dist/index.js +106 -4
- package/dist/polyglot_sql_wasm_bg.wasm +0 -0
- package/package.json +10 -5
package/README.md
CHANGED
|
@@ -462,9 +462,37 @@ const cartesian = validateWithSchema(
|
|
|
462
462
|
);
|
|
463
463
|
```
|
|
464
464
|
|
|
465
|
+
## Tokenize
|
|
466
|
+
|
|
467
|
+
Access the raw SQL token stream with full source position spans. Useful for syntax highlighting, custom linters, or editor integrations.
|
|
468
|
+
|
|
469
|
+
```typescript
|
|
470
|
+
import { tokenize, Dialect } from '@polyglot-sql/sdk';
|
|
471
|
+
|
|
472
|
+
const result = tokenize('SELECT a, b FROM t', Dialect.Generic);
|
|
473
|
+
if (result.success) {
|
|
474
|
+
for (const token of result.tokens!) {
|
|
475
|
+
console.log(token.tokenType, token.text, token.span);
|
|
476
|
+
// "Select" "SELECT" { start: 0, end: 6, line: 1, column: 1 }
|
|
477
|
+
// "Var" "a" { start: 7, end: 8, line: 1, column: 8 }
|
|
478
|
+
// ...
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
```
|
|
482
|
+
|
|
483
|
+
Each token includes:
|
|
484
|
+
|
|
485
|
+
| Field | Type | Description |
|
|
486
|
+
|-------|------|-------------|
|
|
487
|
+
| `tokenType` | `string` | Token type name (e.g. `"Select"`, `"Var"`, `"Comma"`) |
|
|
488
|
+
| `text` | `string` | Raw source text of the token |
|
|
489
|
+
| `span` | `SpanInfo` | Source position: `start`/`end` byte offsets, `line`/`column` (1-based) |
|
|
490
|
+
| `comments` | `string[]` | Leading comments attached to this token |
|
|
491
|
+
| `trailingComments` | `string[]` | Trailing comments attached to this token |
|
|
492
|
+
|
|
465
493
|
## Error Reporting
|
|
466
494
|
|
|
467
|
-
Parse and
|
|
495
|
+
Parse, transpile, and tokenize errors include source position information with both line/column and byte offset ranges, making it easy to highlight errors in editors or show precise error messages.
|
|
468
496
|
|
|
469
497
|
```typescript
|
|
470
498
|
import { parse, transpile, Dialect } from '@polyglot-sql/sdk';
|
|
@@ -474,15 +502,19 @@ if (!result.success) {
|
|
|
474
502
|
console.log(result.error); // "Parse error at line 1, column 11: ..."
|
|
475
503
|
console.log(result.errorLine); // 1
|
|
476
504
|
console.log(result.errorColumn); // 11
|
|
505
|
+
console.log(result.errorStart); // 10 (byte offset)
|
|
506
|
+
console.log(result.errorEnd); // 11 (byte offset, exclusive)
|
|
477
507
|
}
|
|
478
508
|
```
|
|
479
509
|
|
|
480
|
-
|
|
510
|
+
`ParseResult`, `TranspileResult`, and `TokenizeResult` include optional position fields:
|
|
481
511
|
|
|
482
512
|
| Field | Type | Description |
|
|
483
513
|
|-------|------|-------------|
|
|
484
514
|
| `errorLine` | `number \| undefined` | 1-based line number where the error occurred |
|
|
485
515
|
| `errorColumn` | `number \| undefined` | 1-based column number where the error occurred |
|
|
516
|
+
| `errorStart` | `number \| undefined` | Start byte offset of the error range (0-based) |
|
|
517
|
+
| `errorEnd` | `number \| undefined` | End byte offset of the error range (exclusive) |
|
|
486
518
|
|
|
487
519
|
These fields are only present when `success` is `false`. On success, they are `undefined`.
|
|
488
520
|
|
|
@@ -582,6 +614,7 @@ const formattedSafe = pg.formatWithOptions('SELECT a,b FROM t', Dialect.Generic,
|
|
|
582
614
|
| `generate(ast, dialect?)` | Generate SQL from AST |
|
|
583
615
|
| `format(sql, dialect?)` | Pretty-print SQL |
|
|
584
616
|
| `formatWithOptions(sql, dialect?, options?)` | Pretty-print SQL with guard overrides |
|
|
617
|
+
| `tokenize(sql, dialect?)` | Tokenize SQL into a token stream with source spans |
|
|
585
618
|
| `validate(sql, dialect?, options?)` | Validate SQL syntax/semantics |
|
|
586
619
|
| `validateWithSchema(sql, schema, dialect?, options?)` | Validate against a database schema |
|
|
587
620
|
| `getDialects()` | List supported dialect names |
|
|
@@ -733,6 +766,44 @@ For browser use without a bundler:
|
|
|
733
766
|
</script>
|
|
734
767
|
```
|
|
735
768
|
|
|
769
|
+
## CommonJS (CJS) Usage
|
|
770
|
+
|
|
771
|
+
For Node.js projects using `require()`, the SDK ships a CJS build. Since WASM cannot be loaded synchronously, you must call `init()` before using any other function:
|
|
772
|
+
|
|
773
|
+
```javascript
|
|
774
|
+
const { init, transpile, parse, select, col, lit, isInitialized } = require('@polyglot-sql/sdk');
|
|
775
|
+
|
|
776
|
+
async function main() {
|
|
777
|
+
await init();
|
|
778
|
+
|
|
779
|
+
// Now all functions work
|
|
780
|
+
const result = transpile('SELECT IFNULL(a, b)', 'mysql', 'postgresql');
|
|
781
|
+
console.log(result.sql[0]); // SELECT COALESCE(a, b)
|
|
782
|
+
|
|
783
|
+
const parsed = parse('SELECT 1', 'generic');
|
|
784
|
+
console.log(parsed.success); // true
|
|
785
|
+
|
|
786
|
+
const sql = select('id', 'name').from('users')
|
|
787
|
+
.where(col('id').eq(lit(1)))
|
|
788
|
+
.toSql();
|
|
789
|
+
console.log(sql); // SELECT id, name FROM users WHERE id = 1
|
|
790
|
+
}
|
|
791
|
+
|
|
792
|
+
main();
|
|
793
|
+
```
|
|
794
|
+
|
|
795
|
+
You can check initialization status with `isInitialized()`:
|
|
796
|
+
|
|
797
|
+
```javascript
|
|
798
|
+
const { init, isInitialized } = require('@polyglot-sql/sdk');
|
|
799
|
+
|
|
800
|
+
console.log(isInitialized()); // false
|
|
801
|
+
await init();
|
|
802
|
+
console.log(isInitialized()); // true
|
|
803
|
+
```
|
|
804
|
+
|
|
805
|
+
> **Note:** The ESM build (`import`) auto-initializes via top-level `await`, so `init()` is not required there. The CJS build requires it because `require()` is synchronous.
|
|
806
|
+
|
|
736
807
|
## License
|
|
737
808
|
|
|
738
809
|
[MIT](../../LICENSE)
|