@duckcodeailabs/dql-notebook 0.2.1 → 0.2.2

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.
@@ -1 +1 @@
1
- {"version":3,"file":"execute.d.ts","sourceRoot":"","sources":["../src/execute.ts"],"names":[],"mappings":"AAAA,OAAO,EAML,KAAK,aAAa,EACnB,MAAM,0BAA0B,CAAC;AAClC,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,gCAAgC,CAAC;AACnE,OAAO,KAAK,EAAE,YAAY,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAC;AAEvE,MAAM,WAAW,qBAAqB;IACpC,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,EAAE,MAAM,CAAC;IACZ,SAAS,EAAE,YAAY,EAAE,CAAC;IAC1B,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACnC,WAAW,CAAC,EAAE,mBAAmB,CAAC;IAClC,KAAK,EAAE,KAAK,CAAC;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,OAAO,CAAA;KAAE,CAAC,CAAC;CACtE;AAED,MAAM,WAAW,yBAAyB;IACxC,aAAa,CAAC,EAAE,aAAa,CAAC;IAC9B,4EAA4E;IAC5E,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,wBAAgB,kBAAkB,CAChC,IAAI,EAAE,YAAY,EAClB,OAAO,CAAC,EAAE,yBAAyB,GAClC,qBAAqB,GAAG,IAAI,CAqD9B"}
1
+ {"version":3,"file":"execute.d.ts","sourceRoot":"","sources":["../src/execute.ts"],"names":[],"mappings":"AAAA,OAAO,EAML,KAAK,aAAa,EACnB,MAAM,0BAA0B,CAAC;AAClC,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,gCAAgC,CAAC;AACnE,OAAO,KAAK,EAAE,YAAY,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAC;AAGvE,MAAM,WAAW,qBAAqB;IACpC,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,EAAE,MAAM,CAAC;IACZ,SAAS,EAAE,YAAY,EAAE,CAAC;IAC1B,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACnC,WAAW,CAAC,EAAE,mBAAmB,CAAC;IAClC,KAAK,EAAE,KAAK,CAAC;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,OAAO,CAAA;KAAE,CAAC,CAAC;CACtE;AAED,MAAM,WAAW,yBAAyB;IACxC,aAAa,CAAC,EAAE,aAAa,CAAC;IAC9B,4EAA4E;IAC5E,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,wBAAgB,kBAAkB,CAChC,IAAI,EAAE,YAAY,EAClB,OAAO,CAAC,EAAE,yBAAyB,GAClC,qBAAqB,GAAG,IAAI,CAiE9B"}
package/dist/execute.js CHANGED
@@ -1,12 +1,23 @@
1
1
  import { NodeKind, parse, } from '@duckcodeailabs/dql-core';
2
+ import { resolveSemanticRefs, hasSemanticRefs } from './semantic-refs.js';
2
3
  export function buildExecutionPlan(cell, options) {
3
4
  if (cell.type === 'markdown' || cell.type === 'chart') {
4
5
  return null;
5
6
  }
6
7
  if (cell.type === 'sql') {
8
+ // Resolve @metric(name) and @dim(name) references in SQL cells
9
+ let resolvedSql = cell.source;
10
+ if (options?.semanticLayer && hasSemanticRefs(cell.source)) {
11
+ const resolution = resolveSemanticRefs(cell.source, options.semanticLayer);
12
+ if (resolution.unresolvedRefs.length > 0) {
13
+ throw new Error(`Unresolved semantic references in SQL cell: ${resolution.unresolvedRefs.join(', ')}. ` +
14
+ 'Check that these metrics/dimensions exist in your semantic layer.');
15
+ }
16
+ resolvedSql = resolution.resolvedSql;
17
+ }
7
18
  return {
8
19
  title: cell.title || 'SQL cell',
9
- sql: cell.source,
20
+ sql: resolvedSql,
10
21
  sqlParams: [],
11
22
  variables: {},
12
23
  chartConfig: cell.config,
@@ -97,14 +108,16 @@ function buildSemanticPlan(block, semanticLayer, driver) {
97
108
  throw new Error('Semantic block requires a semantic-layer/ configuration. ' +
98
109
  'Add metric and dimension YAML files to your project\'s semantic-layer/ directory.');
99
110
  }
100
- // Extract metric reference from the block's metricRef field
111
+ // Extract metric references: metricsRef (array) takes precedence over metricRef (single)
101
112
  const metrics = [];
102
113
  const dimensions = [];
103
- if (block.metricRef) {
114
+ if (block.metricsRef && block.metricsRef.length > 0) {
115
+ metrics.push(...block.metricsRef);
116
+ }
117
+ else if (block.metricRef) {
104
118
  metrics.push(block.metricRef);
105
119
  }
106
- // Also extract dimensions from block params if they reference dimension names
107
- // For now, dimensions can be passed via block params named "dimensions"
120
+ // Also extract dimensions/metrics from block params if they reference names
108
121
  for (const param of block.params?.params ?? []) {
109
122
  const val = evaluateExpression(param.initializer);
110
123
  if (param.name === 'dimensions' && Array.isArray(val)) {
@@ -116,7 +129,7 @@ function buildSemanticPlan(block, semanticLayer, driver) {
116
129
  }
117
130
  if (metrics.length === 0) {
118
131
  throw new Error(`Semantic block "${block.name}" has no metric references. ` +
119
- 'Add metricRef = "metric_name" to your block declaration.');
132
+ 'Add metric = "metric_name" or metrics = ["metric1", "metric2"] to your block declaration.');
120
133
  }
121
134
  const composed = semanticLayer.composeQuery({ metrics, dimensions, driver });
122
135
  if (!composed) {
@@ -1 +1 @@
1
- {"version":3,"file":"execute.js","sourceRoot":"","sources":["../src/execute.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,QAAQ,EACR,KAAK,GAKN,MAAM,0BAA0B,CAAC;AAmBlC,MAAM,UAAU,kBAAkB,CAChC,IAAkB,EAClB,OAAmC;IAEnC,IAAI,IAAI,CAAC,IAAI,KAAK,UAAU,IAAI,IAAI,CAAC,IAAI,KAAK,OAAO,EAAE,CAAC;QACtD,OAAO,IAAI,CAAC;IACd,CAAC;IAED,IAAI,IAAI,CAAC,IAAI,KAAK,KA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Q,CAAC,UAAU;YACtB,OAAO,IAAI,CAAC,IAAI,CAAC;QACnB,KAAK,QAAQ,CAAC,YAAY;YACxB,OAAO,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,kBAAkB,CAAC,OAAO,CAAC,CAAC,CAAC;QACrE,KAAK,QAAQ,CAAC,YAAY;YACxB,OAAO,IAAI,CAAC,KAAK,CAAC;QACpB,KAAK,QAAQ,CAAC,UAAU;YACtB,OAAO,GAAG,kBAAkB,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC,QAAQ,IAAI,kBAAkB,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC;QAC/F,KAAK,QAAQ,CAAC,YAAY;YACxB,OAAO,IAAI,CAAC,MAAM,CAAC;QACrB,KAAK,QAAQ,CAAC,cAAc;YAC1B,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,OAAO,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,MAAM,CAAC,kBAAkB,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IACnH,CAAC;AACH,CAAC"}
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './document.js';
2
2
  export * from './execute.js';
3
3
  export * from './connector-forms.js';
4
+ export * from './semantic-refs.js';
4
5
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC;AAC9B,cAAc,cAAc,CAAC;AAC7B,cAAc,sBAAsB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC;AAC9B,cAAc,cAAc,CAAC;AAC7B,cAAc,sBAAsB,CAAC;AACrC,cAAc,oBAAoB,CAAC"}
package/dist/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './document.js';
2
2
  export * from './execute.js';
3
3
  export * from './connector-forms.js';
4
+ export * from './semantic-refs.js';
4
5
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC;AAC9B,cAAc,cAAc,CAAC;AAC7B,cAAc,sBAAsB,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC;AAC9B,cAAc,cAAc,CAAC;AAC7B,cAAc,sBAAsB,CAAC;AACrC,cAAc,oBAAoB,CAAC"}
@@ -0,0 +1,46 @@
1
+ /**
2
+ * Semantic reference resolver for SQL cells.
3
+ *
4
+ * Resolves `@metric(name)` and `@dim(name)` references in plain SQL strings
5
+ * by looking up the metric/dimension definition in the SemanticLayer and
6
+ * inlining the SQL expression.
7
+ *
8
+ * Example:
9
+ * Input: SELECT @dim(segment), @metric(total_revenue) FROM fct_revenue GROUP BY @dim(segment)
10
+ * Output: SELECT segment_tier AS segment, SUM(amount) AS total_revenue FROM fct_revenue GROUP BY segment_tier
11
+ */
12
+ import type { SemanticLayer } from '@duckcodeailabs/dql-core';
13
+ export interface SemanticRefResolution {
14
+ /** The resolved SQL with all semantic references expanded. */
15
+ resolvedSql: string;
16
+ /** Names of metrics that were resolved. */
17
+ resolvedMetrics: string[];
18
+ /** Names of dimensions that were resolved. */
19
+ resolvedDimensions: string[];
20
+ /** Any references that could not be resolved. */
21
+ unresolvedRefs: string[];
22
+ }
23
+ /**
24
+ * Check if a SQL string contains any semantic references.
25
+ */
26
+ export declare function hasSemanticRefs(sql: string): boolean;
27
+ /**
28
+ * Resolve all `@metric(name)` and `@dim(name)` references in a SQL string.
29
+ *
30
+ * - `@metric(total_revenue)` → expands to the metric's SQL expression with an alias
31
+ * - `@dim(segment)` or `@dimension(segment)` → expands to the dimension's SQL expression with an alias
32
+ *
33
+ * If a reference can't be found in the semantic layer, it is left as-is and
34
+ * added to `unresolvedRefs` so the caller can report a helpful error.
35
+ */
36
+ export declare function resolveSemanticRefs(sql: string, semanticLayer: SemanticLayer | undefined): SemanticRefResolution;
37
+ /**
38
+ * Get a list of available semantic references for autocomplete.
39
+ */
40
+ export declare function getSemanticRefCompletions(semanticLayer: SemanticLayer | undefined): Array<{
41
+ type: 'metric' | 'dimension';
42
+ name: string;
43
+ label: string;
44
+ description: string;
45
+ }>;
46
+ //# sourceMappingURL=semantic-refs.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"semantic-refs.d.ts","sourceRoot":"","sources":["../src/semantic-refs.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAK9D,MAAM,WAAW,qBAAqB;IACpC,8DAA8D;IAC9D,WAAW,EAAE,MAAM,CAAC;IACpB,2CAA2C;IAC3C,eAAe,EAAE,MAAM,EAAE,CAAC;IAC1B,8CAA8C;IAC9C,kBAAkB,EAAE,MAAM,EAAE,CAAC;IAC7B,iDAAiD;IACjD,cAAc,EAAE,MAAM,EAAE,CAAC;CAC1B;AAED;;GAEG;AACH,wBAAgB,eAAe,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAEpD;AAED;;;;;;;;GAQG;AACH,wBAAgB,mBAAmB,CACjC,GAAG,EAAE,MAAM,EACX,aAAa,EAAE,aAAa,GAAG,SAAS,GACvC,qBAAqB,CA2DvB;AA+BD;;GAEG;AACH,wBAAgB,yBAAyB,CACvC,aAAa,EAAE,aAAa,GAAG,SAAS,GACvC,KAAK,CAAC;IAAE,IAAI,EAAE,QAAQ,GAAG,WAAW,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,WAAW,EAAE,MAAM,CAAA;CAAE,CAAC,CAwB3F"}
@@ -0,0 +1,134 @@
1
+ /**
2
+ * Semantic reference resolver for SQL cells.
3
+ *
4
+ * Resolves `@metric(name)` and `@dim(name)` references in plain SQL strings
5
+ * by looking up the metric/dimension definition in the SemanticLayer and
6
+ * inlining the SQL expression.
7
+ *
8
+ * Example:
9
+ * Input: SELECT @dim(segment), @metric(total_revenue) FROM fct_revenue GROUP BY @dim(segment)
10
+ * Output: SELECT segment_tier AS segment, SUM(amount) AS total_revenue FROM fct_revenue GROUP BY segment_tier
11
+ */
12
+ /** Pattern to match @metric(name) or @dim(name) references. */
13
+ const SEMANTIC_REF_PATTERN = /@(metric|dim|dimension)\(\s*([a-zA-Z_][a-zA-Z0-9_.]*)\s*\)/g;
14
+ /**
15
+ * Check if a SQL string contains any semantic references.
16
+ */
17
+ export function hasSemanticRefs(sql) {
18
+ return SEMANTIC_REF_PATTERN.test(sql);
19
+ }
20
+ /**
21
+ * Resolve all `@metric(name)` and `@dim(name)` references in a SQL string.
22
+ *
23
+ * - `@metric(total_revenue)` → expands to the metric's SQL expression with an alias
24
+ * - `@dim(segment)` or `@dimension(segment)` → expands to the dimension's SQL expression with an alias
25
+ *
26
+ * If a reference can't be found in the semantic layer, it is left as-is and
27
+ * added to `unresolvedRefs` so the caller can report a helpful error.
28
+ */
29
+ export function resolveSemanticRefs(sql, semanticLayer) {
30
+ const resolvedMetrics = [];
31
+ const resolvedDimensions = [];
32
+ const unresolvedRefs = [];
33
+ if (!semanticLayer) {
34
+ // No semantic layer — check if there are refs that need resolving
35
+ const refs = [];
36
+ sql.replace(SEMANTIC_REF_PATTERN, (_match, _type, name) => {
37
+ refs.push(name);
38
+ return _match;
39
+ });
40
+ if (refs.length > 0) {
41
+ return {
42
+ resolvedSql: sql,
43
+ resolvedMetrics: [],
44
+ resolvedDimensions: [],
45
+ unresolvedRefs: refs,
46
+ };
47
+ }
48
+ return { resolvedSql: sql, resolvedMetrics: [], resolvedDimensions: [], unresolvedRefs: [] };
49
+ }
50
+ // Track which position in the SQL each ref appears to handle GROUP BY correctly
51
+ // (in GROUP BY, we should NOT include the alias or aggregation wrapper)
52
+ const resolvedSql = sql.replace(SEMANTIC_REF_PATTERN, (match, refType, name) => {
53
+ if (refType === 'metric') {
54
+ const metric = semanticLayer.getMetric(name);
55
+ if (!metric) {
56
+ unresolvedRefs.push(`metric:${name}`);
57
+ return match;
58
+ }
59
+ resolvedMetrics.push(name);
60
+ // In SELECT context: wrap with aggregation and alias
61
+ // In GROUP BY / WHERE context: just the raw expression
62
+ // We use a heuristic: check if this ref appears after GROUP BY, ORDER BY, WHERE, HAVING
63
+ return `${metric.sql} AS ${sanitizeAlias(name)}`;
64
+ }
65
+ // dim or dimension
66
+ const dim = semanticLayer.getDimension(name);
67
+ if (!dim) {
68
+ unresolvedRefs.push(`dimension:${name}`);
69
+ return match;
70
+ }
71
+ resolvedDimensions.push(name);
72
+ return `${dim.sql} AS ${sanitizeAlias(name)}`;
73
+ });
74
+ // Second pass: fix GROUP BY / ORDER BY / WHERE / HAVING clauses
75
+ // In these clauses, we should use just the raw SQL without alias
76
+ const fixedSql = fixClauseRefs(resolvedSql, semanticLayer);
77
+ return {
78
+ resolvedSql: fixedSql,
79
+ resolvedMetrics,
80
+ resolvedDimensions,
81
+ unresolvedRefs,
82
+ };
83
+ }
84
+ /**
85
+ * In GROUP BY, ORDER BY, WHERE, HAVING clauses, replace "expr AS alias" with just "expr".
86
+ * This is needed because SQL doesn't allow aliases in GROUP BY for most databases.
87
+ */
88
+ function fixClauseRefs(sql, semanticLayer) {
89
+ // Find all "expression AS alias" patterns that appear after GROUP BY, ORDER BY, WHERE, HAVING
90
+ const clausePattern = /\b(GROUP\s+BY|ORDER\s+BY|WHERE|HAVING)\b/gi;
91
+ const match = clausePattern.exec(sql);
92
+ if (!match)
93
+ return sql;
94
+ const clauseStart = match.index;
95
+ const beforeClause = sql.substring(0, clauseStart);
96
+ const clauseAndAfter = sql.substring(clauseStart);
97
+ // In the clause portion, strip " AS alias" from resolved refs
98
+ const fixed = clauseAndAfter.replace(/(\S+)\s+AS\s+([a-zA-Z_][a-zA-Z0-9_]*)/g, (_m, expr, _alias) => {
99
+ return expr;
100
+ });
101
+ return beforeClause + fixed;
102
+ }
103
+ /**
104
+ * Sanitize a name to be a valid SQL alias.
105
+ */
106
+ function sanitizeAlias(name) {
107
+ return name.replace(/\./g, '_').replace(/[^a-zA-Z0-9_]/g, '');
108
+ }
109
+ /**
110
+ * Get a list of available semantic references for autocomplete.
111
+ */
112
+ export function getSemanticRefCompletions(semanticLayer) {
113
+ if (!semanticLayer)
114
+ return [];
115
+ const completions = [];
116
+ for (const metric of semanticLayer.listMetrics()) {
117
+ completions.push({
118
+ type: 'metric',
119
+ name: metric.name,
120
+ label: metric.label,
121
+ description: metric.description ?? '',
122
+ });
123
+ }
124
+ for (const dim of semanticLayer.listDimensions()) {
125
+ completions.push({
126
+ type: 'dimension',
127
+ name: dim.name,
128
+ label: dim.label,
129
+ description: dim.description ?? '',
130
+ });
131
+ }
132
+ return completions;
133
+ }
134
+ //# sourceMappingURL=semantic-refs.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"semantic-refs.js","sourceRoot":"","sources":["../src/semantic-refs.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAIH,+DAA+D;AAC/D,MAAM,oBAAoB,GAAG,6DAA6D,CAAC;AAa3F;;GAEG;AACH,MAAM,UAAU,eAAe,CAAC,GAAW;IACzC,OAAO,oBAAoB,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AACxC,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,mBAAmB,CACjC,GAAW,EACX,aAAwC;IAExC,MAAM,eAAe,GAAa,EAAE,CAAC;IACrC,MAAM,kBAAkB,GAAa,EAAE,CAAC;IACxC,MAAM,cAAc,GAAa,EAAE,CAAC;IAEpC,IAAI,CAAC,aAAa,EAAE,CAAC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,EAAE,CAAC,CAAC;AAChE,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,yBAAyB,CACvC,aAAwC;IAExC,IAAI,CAAC,aAAa;QAAE,OAAO,EAAE,CAAC;IAE9B,MAAM,WAAW,GAA8F,EAAE,CAAC;IAElH,KAAK,MAAM,MAAM,IAAI,aAAa,CAAC,WAAW,EAAE,EAAE,CAAC;QACjD,WAAW,CAAC,IAAI,CAAC;YACf,IAAI,EAAE,QAAQ;YACd,IAAI,EAAE,MAAM,CAAC,IAAI;YACjB,KAAK,EAAE,MAAM,CAAC,KAAK;YACnB,WAAW,EAAE,MAAM,CAAC,WAAW,IAAI,EAAE;SACtC,CAAC,CAAC;IACL,CAAC;IAED,KAAK,MAAM,GAAG,IAAI,aAAa,CAAC,cAAc,EAAE,EAAE,CAAC;QACjD,WAAW,CAAC,IAAI,CAAC;YACf,IAAI,EAAE,WAAW;YACjB,IAAI,EAAE,GAAG,CAAC,IAAI;YACd,KAAK,EAAE,GAAG,CAAC,KAAK;YAChB,WAAW,EAAE,GAAG,CAAC,WAAW,IAAI,EAAE;SACnC,CAAC,CAAC;IACL,CAAC;IAED,OAAO,WAAW,CAAC;AACrB,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@duckcodeailabs/dql-notebook",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "description": "Notebook document model and execution helpers for DQL",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",
@@ -12,6 +12,11 @@
12
12
  "import": "./dist/index.js"
13
13
  }
14
14
  },
15
+ "scripts": {
16
+ "build": "rm -rf dist tsconfig.tsbuildinfo && tsc",
17
+ "test": "vitest run --passWithNoTests",
18
+ "clean": "rm -rf dist tsconfig.tsbuildinfo"
19
+ },
15
20
  "files": [
16
21
  "dist"
17
22
  ],
@@ -24,17 +29,12 @@
24
29
  "access": "public"
25
30
  },
26
31
  "dependencies": {
27
- "@duckcodeailabs/dql-core": "0.3.0",
28
- "@duckcodeailabs/dql-connectors": "0.1.4"
32
+ "@duckcodeailabs/dql-core": "workspace:*",
33
+ "@duckcodeailabs/dql-connectors": "workspace:*"
29
34
  },
30
35
  "devDependencies": {
31
36
  "@types/node": "^22.0.0",
32
37
  "typescript": "^5.7.0",
33
38
  "vitest": "^3.0.0"
34
- },
35
- "scripts": {
36
- "build": "rm -rf dist tsconfig.tsbuildinfo && tsc",
37
- "test": "vitest run --passWithNoTests",
38
- "clean": "rm -rf dist tsconfig.tsbuildinfo"
39
39
  }
40
- }
40
+ }
package/LICENSE DELETED
@@ -1,123 +0,0 @@
1
- Apache License
2
- Version 2.0, January 2004
3
- http://www.apache.org/licenses/
4
-
5
- TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
-
7
- 1. Definitions.
8
-
9
- "License" shall mean the terms and conditions for use, reproduction, and
10
- distribution as defined by Sections 1 through 9 of this document.
11
-
12
- "Licensor" shall mean the copyright owner or entity authorized by the copyright
13
- owner that is granting the License.
14
-
15
- "Legal Entity" shall mean the union of the acting entity and all other entities
16
- that control, are controlled by, or are under common control with that entity.
17
- For the purposes of this definition, "control" means (i) the power, direct or
18
- indirect, to cause the direction or management of such entity, whether by
19
- contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the
20
- outstanding shares, or (iii) beneficial ownership of such entity.
21
-
22
- "You" (or "Your") shall mean an individual or Legal Entity exercising
23
- permissions granted by this License.
24
-
25
- "Source" form shall mean the preferred form for making modifications, including
26
- but not limited to software source code, documentation source, and configuration
27
- files.
28
-
29
- "Object" form shall mean any form resulting from mechanical transformation or
30
- translation of a Source form, including but not limited to compiled object code,
31
- generated documentation, and conversions to other media types.
32
-
33
- "Work" shall mean the work of authorship, whether in Source or Object form, made
34
- available under the License, as indicated by a copyright notice that is included
35
- in or attached to the work.
36
-
37
- "Derivative Works" shall mean any work, whether in Source or Object form, that
38
- is based on (or derived from) the Work and for which the editorial revisions,
39
- annotations, elaborations, or other modifications represent, as a whole, an
40
- original work of authorship.
41
-
42
- "Contribution" shall mean any work of authorship, including the original version
43
- of the Work and any modifications or additions to that Work or Derivative Works
44
- thereof, that is intentionally submitted to Licensor for inclusion in the Work
45
- by the copyright owner or by an individual or Legal Entity authorized to submit
46
- on behalf of the copyright owner.
47
-
48
- "Contributor" shall mean Licensor and any individual or Legal Entity on behalf
49
- of whom a Contribution has been received by Licensor and subsequently
50
- incorporated within the Work.
51
-
52
- 2. Grant of Copyright License.
53
-
54
- Subject to the terms and conditions of this License, each Contributor hereby
55
- grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
56
- irrevocable copyright license to reproduce, prepare Derivative Works of,
57
- publicly display, publicly perform, sublicense, and distribute the Work and such
58
- Derivative Works in Source or Object form.
59
-
60
- 3. Grant of Patent License.
61
-
62
- Subject to the terms and conditions of this License, each Contributor hereby
63
- grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
64
- irrevocable patent license to make, have made, use, offer to sell, sell, import,
65
- and otherwise transfer the Work.
66
-
67
- 4. Redistribution.
68
-
69
- You may reproduce and distribute copies of the Work or Derivative Works thereof
70
- in any medium, with or without modifications, and in Source or Object form,
71
- provided that You meet the following conditions:
72
-
73
- (a) You must give any other recipients of the Work or Derivative Works a copy of
74
- this License; and
75
-
76
- (b) You must cause any modified files to carry prominent notices stating that
77
- You changed the files; and
78
-
79
- (c) You must retain, in the Source form of any Derivative Works that You
80
- distribute, all copyright, patent, trademark, and attribution notices from the
81
- Source form of the Work, excluding those notices that do not pertain to any part
82
- of the Derivative Works; and
83
-
84
- (d) If the Work includes a "NOTICE" text file as part of its distribution, then
85
- any Derivative Works that You distribute must include a readable copy of the
86
- attribution notices contained within such NOTICE file, excluding those notices
87
- that do not pertain to any part of the Derivative Works.
88
-
89
- 5. Submission of Contributions.
90
-
91
- Unless You explicitly state otherwise, any Contribution intentionally submitted
92
- for inclusion in the Work by You to the Licensor shall be under the terms and
93
- conditions of this License, without any additional terms or conditions.
94
-
95
- 6. Trademarks.
96
-
97
- This License does not grant permission to use the trade names, trademarks,
98
- service marks, or product names of the Licensor, except as required for
99
- reasonable and customary use in describing the origin of the Work.
100
-
101
- 7. Disclaimer of Warranty.
102
-
103
- Unless required by applicable law or agreed to in writing, Licensor provides the
104
- Work on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
105
- express or implied, including, without limitation, any warranties or conditions
106
- of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR
107
- PURPOSE.
108
-
109
- 8. Limitation of Liability.
110
-
111
- In no event and under no legal theory, whether in tort (including negligence),
112
- contract, or otherwise, unless required by applicable law, shall any Contributor
113
- be liable to You for damages, including any direct, indirect, special,
114
- incidental, or consequential damages of any character arising as a result of this
115
- License or out of the use or inability to use the Work.
116
-
117
- 9. Accepting Warranty or Additional Liability.
118
-
119
- While redistributing the Work or Derivative Works thereof, You may choose to
120
- offer, and charge a fee for, acceptance of support, warranty, indemnity, or
121
- other liability obligations and/or rights consistent with this License.
122
-
123
- END OF TERMS AND CONDITIONS