@polyglot-sql/sdk 0.1.8 → 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 +110 -2
- package/dist/cdn/polyglot.esm.js +2108 -2009
- package/dist/index.cjs +5375 -0
- package/dist/index.d.cts +14760 -0
- package/dist/index.d.ts +141 -0
- package/dist/index.js +177 -4
- package/dist/polyglot_sql_wasm_bg.wasm +0 -0
- package/package.json +10 -5
package/README.md
CHANGED
|
@@ -50,6 +50,38 @@ console.log(sql[0]);
|
|
|
50
50
|
// x = 1
|
|
51
51
|
```
|
|
52
52
|
|
|
53
|
+
### Format With Guard Options
|
|
54
|
+
|
|
55
|
+
Formatting guard defaults from Rust core:
|
|
56
|
+
- `maxInputBytes`: `16 * 1024 * 1024`
|
|
57
|
+
- `maxTokens`: `1_000_000`
|
|
58
|
+
- `maxAstNodes`: `1_000_000`
|
|
59
|
+
- `maxSetOpChain`: `256`
|
|
60
|
+
|
|
61
|
+
```typescript
|
|
62
|
+
import { formatWithOptions, Dialect } from '@polyglot-sql/sdk';
|
|
63
|
+
|
|
64
|
+
const result = formatWithOptions(
|
|
65
|
+
'SELECT a,b FROM t WHERE x=1',
|
|
66
|
+
Dialect.PostgreSQL,
|
|
67
|
+
{
|
|
68
|
+
maxInputBytes: 2 * 1024 * 1024,
|
|
69
|
+
maxTokens: 250_000,
|
|
70
|
+
maxAstNodes: 250_000,
|
|
71
|
+
maxSetOpChain: 128,
|
|
72
|
+
},
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
if (!result.success) {
|
|
76
|
+
// Includes one of:
|
|
77
|
+
// E_GUARD_INPUT_TOO_LARGE
|
|
78
|
+
// E_GUARD_TOKEN_BUDGET_EXCEEDED
|
|
79
|
+
// E_GUARD_AST_BUDGET_EXCEEDED
|
|
80
|
+
// E_GUARD_SET_OP_CHAIN_EXCEEDED
|
|
81
|
+
console.error(result.error);
|
|
82
|
+
}
|
|
83
|
+
```
|
|
84
|
+
|
|
53
85
|
## Fluent Query Builder
|
|
54
86
|
|
|
55
87
|
Build SQL queries programmatically with full type safety. All builder operations are backed by the Rust engine via WASM.
|
|
@@ -430,9 +462,37 @@ const cartesian = validateWithSchema(
|
|
|
430
462
|
);
|
|
431
463
|
```
|
|
432
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
|
+
|
|
433
493
|
## Error Reporting
|
|
434
494
|
|
|
435
|
-
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.
|
|
436
496
|
|
|
437
497
|
```typescript
|
|
438
498
|
import { parse, transpile, Dialect } from '@polyglot-sql/sdk';
|
|
@@ -442,15 +502,19 @@ if (!result.success) {
|
|
|
442
502
|
console.log(result.error); // "Parse error at line 1, column 11: ..."
|
|
443
503
|
console.log(result.errorLine); // 1
|
|
444
504
|
console.log(result.errorColumn); // 11
|
|
505
|
+
console.log(result.errorStart); // 10 (byte offset)
|
|
506
|
+
console.log(result.errorEnd); // 11 (byte offset, exclusive)
|
|
445
507
|
}
|
|
446
508
|
```
|
|
447
509
|
|
|
448
|
-
|
|
510
|
+
`ParseResult`, `TranspileResult`, and `TokenizeResult` include optional position fields:
|
|
449
511
|
|
|
450
512
|
| Field | Type | Description |
|
|
451
513
|
|-------|------|-------------|
|
|
452
514
|
| `errorLine` | `number \| undefined` | 1-based line number where the error occurred |
|
|
453
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) |
|
|
454
518
|
|
|
455
519
|
These fields are only present when `success` is `false`. On success, they are `undefined`.
|
|
456
520
|
|
|
@@ -533,6 +597,10 @@ import { Polyglot, Dialect } from '@polyglot-sql/sdk';
|
|
|
533
597
|
const pg = Polyglot.getInstance();
|
|
534
598
|
const result = pg.transpile('SELECT 1', Dialect.MySQL, Dialect.PostgreSQL);
|
|
535
599
|
const formatted = pg.format('SELECT a,b FROM t');
|
|
600
|
+
const formattedSafe = pg.formatWithOptions('SELECT a,b FROM t', Dialect.Generic, {
|
|
601
|
+
maxInputBytes: 2 * 1024 * 1024,
|
|
602
|
+
maxSetOpChain: 128,
|
|
603
|
+
});
|
|
536
604
|
```
|
|
537
605
|
|
|
538
606
|
## API Reference
|
|
@@ -545,6 +613,8 @@ const formatted = pg.format('SELECT a,b FROM t');
|
|
|
545
613
|
| `parse(sql, dialect?)` | Parse SQL into AST |
|
|
546
614
|
| `generate(ast, dialect?)` | Generate SQL from AST |
|
|
547
615
|
| `format(sql, dialect?)` | Pretty-print SQL |
|
|
616
|
+
| `formatWithOptions(sql, dialect?, options?)` | Pretty-print SQL with guard overrides |
|
|
617
|
+
| `tokenize(sql, dialect?)` | Tokenize SQL into a token stream with source spans |
|
|
548
618
|
| `validate(sql, dialect?, options?)` | Validate SQL syntax/semantics |
|
|
549
619
|
| `validateWithSchema(sql, schema, dialect?, options?)` | Validate against a database schema |
|
|
550
620
|
| `getDialects()` | List supported dialect names |
|
|
@@ -696,6 +766,44 @@ For browser use without a bundler:
|
|
|
696
766
|
</script>
|
|
697
767
|
```
|
|
698
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
|
+
|
|
699
807
|
## License
|
|
700
808
|
|
|
701
809
|
[MIT](../../LICENSE)
|