@polyglot-sql/sdk 0.1.10 → 0.1.12
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 +122 -1
- package/dist/cdn/polyglot.esm.js +1976 -1894
- package/dist/index.cjs +124 -0
- package/dist/index.d.cts +198 -6
- package/dist/index.d.ts +198 -6
- package/dist/index.js +167 -1
- package/dist/polyglot_sql_wasm_bg.wasm +0 -0
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -532,7 +532,7 @@ if (!result.success) {
|
|
|
532
532
|
Trace how columns flow through SQL queries, from source tables to the result set.
|
|
533
533
|
|
|
534
534
|
```typescript
|
|
535
|
-
import { lineage, getSourceTables } from '@polyglot-sql/sdk';
|
|
535
|
+
import { lineage, lineageWithSchema, getSourceTables } from '@polyglot-sql/sdk';
|
|
536
536
|
|
|
537
537
|
// Trace a column through joins, CTEs, and subqueries
|
|
538
538
|
const result = lineage('total', 'SELECT o.total FROM orders o JOIN users u ON o.user_id = u.id');
|
|
@@ -541,6 +541,19 @@ if (result.success) {
|
|
|
541
541
|
console.log(result.lineage.downstream); // source nodes
|
|
542
542
|
}
|
|
543
543
|
|
|
544
|
+
// Schema-aware lineage (same schema format as validateWithSchema)
|
|
545
|
+
const schema = {
|
|
546
|
+
tables: [
|
|
547
|
+
{ name: 'users', columns: [{ name: 'id', type: 'INT' }] },
|
|
548
|
+
{ name: 'orders', columns: [{ name: 'user_id', type: 'INT' }] },
|
|
549
|
+
],
|
|
550
|
+
};
|
|
551
|
+
const schemaLineage = lineageWithSchema(
|
|
552
|
+
'id',
|
|
553
|
+
'SELECT id FROM users u JOIN orders o ON u.id = o.user_id',
|
|
554
|
+
schema,
|
|
555
|
+
);
|
|
556
|
+
|
|
544
557
|
// Get all source tables that contribute to a column
|
|
545
558
|
const tables = getSourceTables('total', 'SELECT o.total FROM orders o JOIN users u ON o.user_id = u.id');
|
|
546
559
|
if (tables.success) {
|
|
@@ -625,6 +638,7 @@ const formattedSafe = pg.formatWithOptions('SELECT a,b FROM t', Dialect.Generic,
|
|
|
625
638
|
| Function | Description |
|
|
626
639
|
|----------|-------------|
|
|
627
640
|
| `lineage(column, sql, dialect?, trimSelects?)` | Trace column lineage through a query |
|
|
641
|
+
| `lineageWithSchema(column, sql, schema, dialect?, trimSelects?)` | Trace lineage with schema-based qualification |
|
|
628
642
|
| `getSourceTables(column, sql, dialect?)` | Get source tables for a column |
|
|
629
643
|
| `diff(source, target, dialect?, options?)` | Diff two SQL statements |
|
|
630
644
|
| `hasChanges(edits)` | Check if diff has non-keep edits |
|
|
@@ -804,6 +818,113 @@ console.log(isInitialized()); // true
|
|
|
804
818
|
|
|
805
819
|
> **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
820
|
|
|
821
|
+
## Bundler Configuration
|
|
822
|
+
|
|
823
|
+
The ESM build uses **top-level `await`** (ES2022) and **`new URL("...", import.meta.url)`** for WASM loading. Both are web standards supported by all modern bundlers, but some require explicit configuration.
|
|
824
|
+
|
|
825
|
+
### Vite
|
|
826
|
+
|
|
827
|
+
Vite works out of the box with `vite-plugin-wasm`. This is the setup used by the [Polyglot Playground](https://github.com/tobilg/polyglot):
|
|
828
|
+
|
|
829
|
+
```typescript
|
|
830
|
+
// vite.config.ts
|
|
831
|
+
import { defineConfig } from 'vite';
|
|
832
|
+
import wasm from 'vite-plugin-wasm';
|
|
833
|
+
|
|
834
|
+
export default defineConfig({
|
|
835
|
+
plugins: [wasm()],
|
|
836
|
+
build: {
|
|
837
|
+
target: 'esnext', // required for top-level await
|
|
838
|
+
},
|
|
839
|
+
optimizeDeps: {
|
|
840
|
+
exclude: ['@polyglot-sql/sdk'], // prevent esbuild from pre-bundling WASM
|
|
841
|
+
},
|
|
842
|
+
});
|
|
843
|
+
```
|
|
844
|
+
|
|
845
|
+
### Next.js
|
|
846
|
+
|
|
847
|
+
Next.js requires enabling WebAssembly experiments in the webpack config:
|
|
848
|
+
|
|
849
|
+
```javascript
|
|
850
|
+
// next.config.js
|
|
851
|
+
module.exports = {
|
|
852
|
+
webpack: (config) => {
|
|
853
|
+
config.experiments = {
|
|
854
|
+
...config.experiments,
|
|
855
|
+
asyncWebAssembly: true,
|
|
856
|
+
topLevelAwait: true,
|
|
857
|
+
layers: true,
|
|
858
|
+
};
|
|
859
|
+
return config;
|
|
860
|
+
},
|
|
861
|
+
};
|
|
862
|
+
```
|
|
863
|
+
|
|
864
|
+
For **production SSR builds**, Next.js may emit `.wasm` files at a different path than where it tries to load them. A common workaround is to add a plugin that fixes the output path:
|
|
865
|
+
|
|
866
|
+
```javascript
|
|
867
|
+
// next.config.js
|
|
868
|
+
class WasmChunksFixPlugin {
|
|
869
|
+
apply(compiler) {
|
|
870
|
+
compiler.hooks.thisCompilation.tap('WasmChunksFixPlugin', (compilation) => {
|
|
871
|
+
compilation.hooks.processAssets.tap(
|
|
872
|
+
{ name: 'WasmChunksFixPlugin' },
|
|
873
|
+
(assets) =>
|
|
874
|
+
Object.entries(assets).forEach(([pathname, source]) => {
|
|
875
|
+
if (!pathname.match(/\.wasm$/)) return;
|
|
876
|
+
compilation.deleteAsset(pathname);
|
|
877
|
+
const name = pathname.split('/')[1];
|
|
878
|
+
const info = compilation.assetsInfo.get(pathname);
|
|
879
|
+
compilation.emitAsset(name, source, info);
|
|
880
|
+
})
|
|
881
|
+
);
|
|
882
|
+
});
|
|
883
|
+
}
|
|
884
|
+
}
|
|
885
|
+
|
|
886
|
+
module.exports = {
|
|
887
|
+
webpack: (config, { isServer, dev }) => {
|
|
888
|
+
config.experiments = {
|
|
889
|
+
...config.experiments,
|
|
890
|
+
asyncWebAssembly: true,
|
|
891
|
+
topLevelAwait: true,
|
|
892
|
+
layers: true,
|
|
893
|
+
};
|
|
894
|
+
if (!dev && isServer) {
|
|
895
|
+
config.output.webassemblyModuleFilename = 'chunks/[id].wasm';
|
|
896
|
+
config.plugins.push(new WasmChunksFixPlugin());
|
|
897
|
+
}
|
|
898
|
+
return config;
|
|
899
|
+
},
|
|
900
|
+
};
|
|
901
|
+
```
|
|
902
|
+
|
|
903
|
+
### webpack 5 (standalone)
|
|
904
|
+
|
|
905
|
+
Enable the required experiments in your webpack config:
|
|
906
|
+
|
|
907
|
+
```javascript
|
|
908
|
+
// webpack.config.js
|
|
909
|
+
module.exports = {
|
|
910
|
+
experiments: {
|
|
911
|
+
asyncWebAssembly: true,
|
|
912
|
+
topLevelAwait: true,
|
|
913
|
+
},
|
|
914
|
+
// ...
|
|
915
|
+
};
|
|
916
|
+
```
|
|
917
|
+
|
|
918
|
+
`topLevelAwait` is enabled by default since webpack 5.83.0. `asyncWebAssembly` must be set explicitly.
|
|
919
|
+
|
|
920
|
+
### Troubleshooting
|
|
921
|
+
|
|
922
|
+
If you see `undefined` exports (e.g., `Dialect` is `undefined`) or errors about `require("fs")`:
|
|
923
|
+
|
|
924
|
+
1. Make sure `asyncWebAssembly` and `topLevelAwait` experiments are enabled in your bundler config
|
|
925
|
+
2. Ensure the bundler is resolving the ESM build (the `"import"` or `"browser"` export condition), not the CJS build which uses Node.js APIs
|
|
926
|
+
3. For SSR frameworks (Next.js, Nuxt), check that the `.wasm` file is correctly included in the server build output
|
|
927
|
+
|
|
807
928
|
## License
|
|
808
929
|
|
|
809
930
|
[MIT](../../LICENSE)
|