@szdatatalk/sqlward 0.3.2 → 0.3.3

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/NOTICE ADDED
@@ -0,0 +1,4 @@
1
+ SqlWard
2
+ Copyright 2026 Suzhou DataTalk Information Technology Co., Ltd. (苏州数说信息科技有限公司)
3
+
4
+ This product includes SqlWard (https://github.com/shygoly/sqlward | https://www.npmjs.com/package/@szdatatalk/sqlward), developed by Suzhou DataTalk Information Technology Co., Ltd.
package/index.d.ts CHANGED
@@ -1,19 +1,202 @@
1
+ /**
2
+ * `@szdatatalk/sqlward` — Offline, zero-Python Pure-Rust WebAssembly runtime for
3
+ * column-level SQL lineage (8 dialects), fail-closed M6 SQL security guardrails,
4
+ * transitive column ACLs, cryptographic decision receipts, and dbt/OMOP/Robot adapters.
5
+ *
6
+ * Never executes SQL and never requires a database connection.
7
+ *
8
+ * @example
9
+ * ```ts
10
+ * import { instantiateWasm } from "@szdatatalk/sqlward";
11
+ *
12
+ * const sw = await instantiateWasm();
13
+ * const lineage = sw.parseColumn({
14
+ * dialect: "postgres",
15
+ * sql: "INSERT INTO dws.orders (id, net) SELECT id, amount - discount FROM ods.orders",
16
+ * });
17
+ * ```
18
+ * @packageDocumentation
19
+ */
20
+
1
21
  export const PROMOTED: true;
2
- export const CONTRACT_VERSION: "0.3.2";
22
+ export const CONTRACT_VERSION: "0.3.3";
3
23
  export const MAX_WASM_INPUT_BYTES: 1048576;
4
24
  export const MAX_WASM_DBT_INPUT_BYTES: 8388608;
5
25
 
26
+ /** Supported SQL dialects parsed offline in Rust + WebAssembly. */
27
+ export type SqlDialect =
28
+ | "postgres"
29
+ | "mysql"
30
+ | "doris"
31
+ | "starrocks"
32
+ | "oracle"
33
+ | "spark"
34
+ | "hive"
35
+ | "tsql";
36
+
6
37
  export interface LineageEdge {
7
38
  sourceTable: string;
8
39
  sourceColumn: string;
9
40
  targetTable: string;
10
41
  targetColumn: string;
11
- dependencyType: string;
42
+ dependencyType: "DIRECT" | "INDIRECT" | "Direct" | "Indirect" | string;
12
43
  relation: string;
13
44
  transformExpr?: string | null;
14
45
  maskingApplied?: boolean;
15
46
  }
16
47
 
48
+ export interface ParseColumnRequest {
49
+ dialect: SqlDialect | string;
50
+ sql: string;
51
+ catalogSchema?: Record<string, string[]>;
52
+ defaultSchema?: string;
53
+ preserveAsWritten?: boolean;
54
+ [key: string]: unknown;
55
+ }
56
+
57
+ export interface ParseColumnStatement {
58
+ statementIndex?: number;
59
+ status?: string;
60
+ edges: LineageEdge[];
61
+ notes?: string[];
62
+ [key: string]: unknown;
63
+ }
64
+
65
+ export interface ParseColumnResult {
66
+ status: "OK" | "INCONCLUSIVE" | "ERROR" | string;
67
+ statements: ParseColumnStatement[];
68
+ notes?: string[];
69
+ [key: string]: unknown;
70
+ }
71
+
72
+ export interface PolicyRelationMetadata {
73
+ kind: "table" | "view" | string;
74
+ sensitivityLabels?: string[];
75
+ columns?: string[];
76
+ columnLabels?: Record<string, string[]>;
77
+ viewSql?: string;
78
+ [key: string]: unknown;
79
+ }
80
+
81
+ export interface PolicySnapshot {
82
+ policyId: string;
83
+ policyVersion: string;
84
+ profile: "read-only" | "analysis" | "custom" | string;
85
+ relations: Record<string, PolicyRelationMetadata>;
86
+ deniedLabels?: string[];
87
+ enforceColumnAcl?: boolean;
88
+ allowMultipleStatements?: boolean;
89
+ allowWriteStatements?: boolean;
90
+ allowUnknownFunctions?: boolean;
91
+ allowUnknownRelations?: boolean;
92
+ allowedFunctions?: string[];
93
+ allowedRoles?: string[];
94
+ allowedTenants?: string[];
95
+ columnGrants?: Array<{
96
+ principalOrRole: string;
97
+ relation: string;
98
+ column: string;
99
+ effect: "ALLOW" | "DENY";
100
+ }>;
101
+ [key: string]: unknown;
102
+ }
103
+
104
+ export interface TrustedPrincipal {
105
+ principalId: string;
106
+ tenantId: string;
107
+ roles: string[];
108
+ }
109
+
110
+ export interface PolicyEvaluationRequest {
111
+ dialect: SqlDialect | string;
112
+ sql: string;
113
+ snapshot: PolicySnapshot;
114
+ principal: TrustedPrincipal;
115
+ caseSettingProvided?: boolean;
116
+ [key: string]: unknown;
117
+ }
118
+
119
+ export interface PolicyDecisionReport {
120
+ contractVersion: string;
121
+ decision: "ALLOW" | "DENY" | string;
122
+ parseStatus: string;
123
+ sqlHash: string;
124
+ statementCount: number;
125
+ reasonCodes: string[];
126
+ reasonDetails: string[];
127
+ diagnostics: unknown[];
128
+ [key: string]: unknown;
129
+ }
130
+
131
+ export interface DbtAnalysisRequest {
132
+ /** Raw dbt `manifest.json` string or parsed JSON object. */
133
+ manifest: Record<string, unknown> | string;
134
+ /** Optional raw dbt `catalog.json` string or parsed JSON object. */
135
+ catalog?: Record<string, unknown> | string | null;
136
+ /** Optional raw dbt `run_results.json` string or parsed JSON object. */
137
+ run_results?: Record<string, unknown> | string | null;
138
+ /** Run SQL column-level lineage extraction on compiled model SQL (default `true`). */
139
+ analyze_sql?: boolean;
140
+ analyzeSql?: boolean;
141
+ [key: string]: unknown;
142
+ }
143
+
144
+ export interface DbtProjectSnapshot {
145
+ contract_version: string;
146
+ operation: string;
147
+ status: string;
148
+ snapshot_id: string;
149
+ evidence_id: string;
150
+ dialect: string;
151
+ protected_decision_allowed: boolean;
152
+ nodes: Array<Record<string, unknown>>;
153
+ edges: {
154
+ declared: Array<Record<string, unknown>>;
155
+ inferred: Array<Record<string, unknown>>;
156
+ };
157
+ completeness: {
158
+ capability_gaps?: string[];
159
+ [key: string]: unknown;
160
+ };
161
+ diagnostics: Array<Record<string, unknown>>;
162
+ [key: string]: unknown;
163
+ }
164
+
165
+ export interface DbtDiffRequest {
166
+ base: DbtProjectSnapshot | Record<string, unknown>;
167
+ head: DbtProjectSnapshot | Record<string, unknown>;
168
+ }
169
+
170
+ export interface DbtSnapshotDiff {
171
+ contract_version: string;
172
+ operation: string;
173
+ base_snapshot_id: string;
174
+ head_snapshot_id: string;
175
+ added_nodes: string[];
176
+ removed_nodes: string[];
177
+ added_declared_edges: Array<Record<string, unknown>>;
178
+ removed_declared_edges: Array<Record<string, unknown>>;
179
+ added_inferred_edges: Array<Record<string, unknown>>;
180
+ removed_inferred_edges: Array<Record<string, unknown>>;
181
+ }
182
+
183
+ export interface DbtImpactRequest {
184
+ snapshot: DbtProjectSnapshot | Record<string, unknown>;
185
+ origin_node: string;
186
+ direction?: "downstream" | "upstream" | "both";
187
+ max_hops?: number;
188
+ }
189
+
190
+ export interface DbtImpactResult {
191
+ contract_version: string;
192
+ operation: string;
193
+ origin_node: string;
194
+ direction: string;
195
+ impacted_nodes: string[];
196
+ depth_by_node: Record<string, number>;
197
+ impacted_inferred_columns: string[];
198
+ }
199
+
17
200
  export interface EdgeComparisonReport {
18
201
  matchCount: number;
19
202
  missingInActual: string[];
@@ -34,22 +217,122 @@ export interface SqlWardWasmInstance {
34
217
  promoted: true;
35
218
  maxInputBytes: number;
36
219
  maxDbtInputBytes: number;
220
+
221
+ /** Return runtime version, supported dialects, and input byte ceilings. */
37
222
  versionInfo(): WasmVersionInfo;
38
- parseColumn(request: Record<string, unknown> | string): Record<string, unknown>;
39
- evaluatePolicy(request: Record<string, unknown> | string): Record<string, unknown>;
223
+
224
+ /**
225
+ * Extract deterministic column-level SQL lineage (`DIRECT` / `INDIRECT` edges) offline.
226
+ *
227
+ * @example
228
+ * ```ts
229
+ * const res = sw.parseColumn({
230
+ * dialect: "postgres",
231
+ * sql: "INSERT INTO dws.orders (id, net) SELECT id, amount - discount FROM ods.orders",
232
+ * });
233
+ * console.log(res.status, res.statements[0].edges);
234
+ * ```
235
+ */
236
+ parseColumn(request: ParseColumnRequest | string): ParseColumnResult;
237
+
238
+ /**
239
+ * Evaluate fail-closed M6 SQL security policy (read-only/analysis/custom profiles,
240
+ * write/DDL/dynamic-SQL/locking rejection, and transitive view/CTE column ACLs).
241
+ *
242
+ * @example
243
+ * ```ts
244
+ * const decision = sw.evaluatePolicy({
245
+ * dialect: "postgres",
246
+ * sql: "DELETE FROM ods.orders",
247
+ * snapshot: {
248
+ * policyId: "pol-1",
249
+ * policyVersion: "1.0.0",
250
+ * profile: "read-only",
251
+ * relations: { "ods.orders": { kind: "table", sensitivityLabels: [] } },
252
+ * },
253
+ * principal: { principalId: "u1", tenantId: "t1", roles: ["analyst"] },
254
+ * });
255
+ * // decision.decision === "DENY"
256
+ * ```
257
+ */
258
+ evaluatePolicy(request: PolicyEvaluationRequest | string): PolicyDecisionReport;
259
+
260
+ /** Compile and cache a `PolicySnapshot` in WASM memory for repeated low-latency evaluations. */
261
+ preparePolicySnapshot(request: Record<string, unknown> | string): Record<string, unknown>;
262
+
263
+ /** Evaluate SQL against a previously prepared cached policy snapshot handle. */
264
+ evaluatePolicyPrepared(request: Record<string, unknown> | string): PolicyDecisionReport;
265
+
266
+ /** Evict a cached prepared policy snapshot from WASM memory. */
267
+ evictPolicySnapshot(request: Record<string, unknown> | string): Record<string, unknown>;
268
+
269
+ /** Verify constant-time HMAC-SHA256 M6 policy decision receipts. */
40
270
  verifyReceipt(request: Record<string, unknown> | string): Record<string, unknown>;
271
+
272
+ /** Verify sealed workflow evidence bundles. */
41
273
  verifyWorkflowEvidence(bundle: Record<string, unknown> | string): Record<string, unknown>;
274
+
275
+ /** Schema-grounded column binding against a catalog snapshot (`binding-report/v1`). */
42
276
  bind(request: Record<string, unknown> | string): Record<string, unknown>;
43
- analyzeDbt(request: Record<string, unknown> | string, options?: { slim?: boolean }): Record<string, unknown>;
44
- diffDbt(request: Record<string, unknown> | string): Record<string, unknown>;
45
- impactDbt(request: Record<string, unknown> | string): Record<string, unknown>;
46
- sealDbt(snapshot: Record<string, unknown> | string): Record<string, unknown>;
277
+
278
+ /**
279
+ * Analyze a dbt project (`manifest.json` + optional `catalog.json` / `run_results.json`)
280
+ * in a single WASM call with automatic lossless slimming (`slim: true` by default).
281
+ * For large manifests (> 8 MiB after slimming), prefer {@link analyzeDbtBatched}.
282
+ */
283
+ analyzeDbt(request: DbtAnalysisRequest | string, options?: { slim?: boolean }): DbtProjectSnapshot;
284
+
285
+ /**
286
+ * Compute structural & column-level lineage diff between base and head dbt snapshots.
287
+ *
288
+ * @example
289
+ * ```ts
290
+ * const diff = sw.diffDbt({ base: baseSnap, head: headSnap });
291
+ * console.log(diff.added_nodes, diff.removed_inferred_edges);
292
+ * ```
293
+ */
294
+ diffDbt(request: DbtDiffRequest | string): DbtSnapshotDiff;
295
+
296
+ /**
297
+ * Compute bounded upstream/downstream blast-radius impact from a dbt node.
298
+ *
299
+ * @example
300
+ * ```ts
301
+ * const impact = sw.impactDbt({
302
+ * snapshot: headSnap,
303
+ * origin_node: "model.analytics.stg_orders",
304
+ * direction: "downstream",
305
+ * max_hops: 6,
306
+ * });
307
+ * ```
308
+ */
309
+ impactDbt(request: DbtImpactRequest | string): DbtImpactResult;
310
+
311
+ /** Re-seal canonical `snapshot_id` and `evidence_id` digests on a dbt snapshot. */
312
+ sealDbt(snapshot: DbtProjectSnapshot | Record<string, unknown> | string): DbtProjectSnapshot;
313
+
314
+ /** Profile OMOP CDM 5.4 clinical lineage (`omop-lineage-profile/v1`). */
47
315
  profileOmop(request: Record<string, unknown> | string): Record<string, unknown>;
316
+
317
+ /** Gate protected clinical column changes (`mapping-impact-analysis/v1`). */
48
318
  analyzeMappingImpact(request: Record<string, unknown> | string): Record<string, unknown>;
319
+
320
+ /** Query sealed RobotDataOps artifact lineage impact (`robot-artifact-lineage/v1`). */
49
321
  queryRobotImpact(query: Record<string, unknown> | string): Record<string, unknown>;
322
+
323
+ /** Verify offline HMAC-SHA256 control-plane entitlements (`control-plane-interop/v1`). */
50
324
  verifyInterop(request: Record<string, unknown> | string): Record<string, unknown>;
325
+
326
+ /** Retrieve ranked, sensitivity-redacted semantic schema context for Text-to-SQL prompts. */
327
+ retrieveContext(request: Record<string, unknown> | string): Record<string, unknown>;
328
+
329
+ /** Compute canonical SHA-256 lineage graph digest from column edges. */
51
330
  graphDigestFromEdges(edges: LineageEdge[] | string): string;
331
+
332
+ /** Traverse downstream impacted columns from a `(table, column)` origin. */
52
333
  impactDownstream(edges: LineageEdge[] | string, originTable: string, originColumn: string): unknown;
334
+
335
+ /** Traverse upstream source columns for a `(table, column)` target. */
53
336
  impactUpstream(edges: LineageEdge[] | string, originTable: string, originColumn: string): unknown;
54
337
  }
55
338
 
@@ -83,20 +366,59 @@ export function compareEdges(
83
366
  expectedDirect: Omit<LineageEdge, "dependencyType">[],
84
367
  expectedIndirect: Omit<LineageEdge, "dependencyType">[],
85
368
  ): EdgeComparisonReport;
369
+
370
+ /**
371
+ * Load and instantiate the bundled Pure-Rust `sqlward_wasm.wasm` module.
372
+ *
373
+ * @example
374
+ * ```ts
375
+ * import { instantiateWasm } from "@szdatatalk/sqlward";
376
+ *
377
+ * const sw = await instantiateWasm();
378
+ * console.log(sw.versionInfo());
379
+ * ```
380
+ */
86
381
  export function instantiateWasm(wasmSource?: string | Uint8Array | ArrayBuffer): Promise<SqlWardWasmInstance>;
382
+
383
+ /** Execute a bounded WASM operation inside an isolated Node Worker thread with timeout & AbortSignal support. */
87
384
  export function runWasmWorker(
88
385
  operation: WasmWorkerOperation,
89
386
  payload: unknown,
90
387
  options?: WasmWorkerOptions,
91
388
  ): Promise<unknown>;
389
+
390
+ /** Losslessly strip non-lineage bulk fields from a dbt `manifest.json` while keeping DAG & compiled SQL intact. */
92
391
  export function slimDbtManifest(
93
392
  manifest: Record<string, unknown> | string,
94
393
  options?: { includeCompiledCode?: boolean; onlyModelIds?: string[] | null },
95
394
  ): Record<string, unknown>;
395
+
396
+ /** Losslessly strip non-column metadata from a dbt `catalog.json`. */
96
397
  export function slimDbtCatalog(
97
398
  catalog: Record<string, unknown> | string | null | undefined,
98
399
  ): Record<string, unknown> | null;
400
+
401
+ /**
402
+ * Analyze arbitrary-size dbt projects offline with automatic lossless manifest slimming,
403
+ * Worker batching, and bit-for-bit `snapshot_id` & `evidence_id` parity with single-shot analysis.
404
+ *
405
+ * @example
406
+ * ```ts
407
+ * import fs from "node:fs";
408
+ * import { analyzeDbtBatched } from "@szdatatalk/sqlward";
409
+ *
410
+ * const snap = await analyzeDbtBatched(
411
+ * {
412
+ * manifest: fs.readFileSync("target/manifest.json", "utf8"),
413
+ * catalog: fs.readFileSync("target/catalog.json", "utf8"),
414
+ * analyze_sql: true,
415
+ * },
416
+ * { batchSize: 50, useWorker: true },
417
+ * );
418
+ * console.log(snap.snapshot_id, snap.edges.inferred.length);
419
+ * ```
420
+ */
99
421
  export function analyzeDbtBatched(
100
- request: Record<string, unknown>,
422
+ request: DbtAnalysisRequest,
101
423
  options?: WasmWorkerOptions & { batchSize?: number; maxBatchBytes?: number; useWorker?: boolean },
102
- ): Promise<Record<string, unknown>>;
424
+ ): Promise<DbtProjectSnapshot>;
package/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Offline Node & WASM runtime SDK (`@szdatatalk/sqlward`, v0.3.2).
2
+ * Offline Node & WASM runtime SDK (`@szdatatalk/sqlward`, v0.3.3).
3
3
  *
4
4
  * Zero Python dependency, zero compiler required at runtime, zero network access.
5
5
  * Executes `sqlward_wasm.wasm` directly in-process or inside a terminable
@@ -15,7 +15,7 @@ import { fileURLToPath } from "node:url";
15
15
  import { Worker } from "node:worker_threads";
16
16
 
17
17
  export const PROMOTED = true;
18
- export const CONTRACT_VERSION = "0.3.2";
18
+ export const CONTRACT_VERSION = "0.3.3";
19
19
  export const MAX_WASM_INPUT_BYTES = 1_048_576;
20
20
  export const MAX_WASM_DBT_INPUT_BYTES = 8 * 1_048_576;
21
21
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@szdatatalk/sqlward",
3
- "version": "0.3.2",
3
+ "version": "0.3.3",
4
4
  "license": "Apache-2.0",
5
5
  "repository": {
6
6
  "type": "git",
@@ -27,7 +27,8 @@
27
27
  "wasm-worker.js",
28
28
  "wasm/sqlward_wasm.wasm",
29
29
  "README.md",
30
- "LICENSE"
30
+ "LICENSE",
31
+ "NOTICE"
31
32
  ],
32
33
  "keywords": [
33
34
  "szdatatalk",
@@ -46,7 +47,7 @@
46
47
  "access": "public"
47
48
  },
48
49
  "scripts": {
49
- "stage-wasm": "node -e \"const fs=require('fs'),p=require('path');const dst=p.resolve('wasm/sqlward_wasm.wasm');const srcs=[p.resolve('../../native/target/wasm32-unknown-unknown/release/sqlward_wasm.wasm'),p.resolve('../../evidence/phase2/artifacts/wasm/sqlward_wasm.wasm')];const src=srcs.find((s)=>fs.existsSync(s));if(src){fs.mkdirSync(p.dirname(dst),{recursive:true});fs.copyFileSync(src,dst);}if(!fs.existsSync(dst)){throw new Error('Missing wasm/sqlward_wasm.wasm; build sqlward-wasm first');}const lic=p.resolve('../../LICENSE');if(fs.existsSync(lic))fs.copyFileSync(lic,p.resolve('LICENSE'));\"",
50
+ "stage-wasm": "node -e \"const fs=require('fs'),p=require('path');const dst=p.resolve('wasm/sqlward_wasm.wasm');const srcs=[p.resolve('../../native/target/wasm32-unknown-unknown/release/sqlward_wasm.wasm'),p.resolve('../../evidence/phase2/artifacts/wasm/sqlward_wasm.wasm')];const src=srcs.find((s)=>fs.existsSync(s));if(src){fs.mkdirSync(p.dirname(dst),{recursive:true});fs.copyFileSync(src,dst);}if(!fs.existsSync(dst)){throw new Error('Missing wasm/sqlward_wasm.wasm; build sqlward-wasm first');}for(const f of ['LICENSE','NOTICE']){const s=p.resolve('../../'+f);if(fs.existsSync(s))fs.copyFileSync(s,p.resolve(f));}\"",
50
51
  "prepack": "npm run stage-wasm",
51
52
  "test": "node --test test/contract.test.js"
52
53
  },
Binary file