tplm-lang 0.3.2 → 0.3.4

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/dist/index.d.ts CHANGED
@@ -34,9 +34,10 @@ export { buildTableSpec, generateQueryPlan, generateMalloyQueries, buildGridSpec
34
34
  export type { TableSpec, QueryPlan, GridSpec, HeaderNode, QueryResults, MalloyQuerySpec, } from './compiler/index.js';
35
35
  export { renderGridToHTML } from './renderer/index.js';
36
36
  export type { GridRenderOptions } from './renderer/index.js';
37
- export { createConnection, createLocalConnection, getConnection, getConnectionType, getDefaultSource, executeMalloy, executeSQL, setPendingConnection, } from './executor/index.js';
38
- export type { ConnectionType, ConnectionOptions, BigQueryConnectionOptions, DuckDBConnectionOptions, ExecuteOptions, } from './executor/index.js';
37
+ export { createConnection, createLocalConnection, getConnection, getConnectionType, getDefaultSource, executeMalloy, executeSQL, setPendingConnection, setConnection, } from './executor/index.js';
38
+ export type { Connection, ConnectionType, ConnectionOptions, BigQueryConnectionOptions, DuckDBConnectionOptions, ExecuteOptions, } from './executor/index.js';
39
39
  import type { TableSpec, QueryPlan, GridSpec, MalloyQuerySpec } from './compiler/index.js';
40
+ import type { Connection } from './executor/index.js';
40
41
  import type { DimensionOrderingProvider } from './compiler/dimension-utils.js';
41
42
  /**
42
43
  * Options for creating a TPL instance
@@ -186,6 +187,33 @@ export declare function fromBigQueryTable(options: {
186
187
  credentialsPath?: string;
187
188
  projectId?: string;
188
189
  } & TPLOptions): EasyTPL;
190
+ /**
191
+ * Query a table using a pre-built Malloy connection.
192
+ * Use this when you have an existing Connection instance (e.g., a
193
+ * BigQueryConnection constructed with in-memory credentials).
194
+ *
195
+ * @example
196
+ * ```typescript
197
+ * import { BigQueryConnection } from '@malloydata/db-bigquery';
198
+ * import { fromConnection } from 'tplm-lang';
199
+ *
200
+ * const connection = new BigQueryConnection('bigquery', undefined, {
201
+ * projectId: 'my-project',
202
+ * credentials: { client_email, private_key },
203
+ * });
204
+ *
205
+ * const tpl = fromConnection({
206
+ * connection,
207
+ * table: 'my-project.my_dataset.sales',
208
+ * });
209
+ * const { html } = await tpl.query('TABLE ROWS region * revenue.sum COLS quarter;');
210
+ * ```
211
+ */
212
+ export declare function fromConnection(options: {
213
+ connection: Connection;
214
+ table: string;
215
+ dialect?: SqlDialect;
216
+ } & TPLOptions): EasyTPL;
189
217
  /**
190
218
  * Extended options for EasyTPL that include percentile support metadata.
191
219
  */
package/dist/index.js CHANGED
@@ -34,11 +34,11 @@ export { buildTableSpec, generateQueryPlan, generateMalloyQueries, buildGridSpec
34
34
  // renderer
35
35
  export { renderGridToHTML } from './renderer/index.js';
36
36
  // executor
37
- export { createConnection, createLocalConnection, getConnection, getConnectionType, getDefaultSource, executeMalloy, executeSQL, setPendingConnection, } from './executor/index.js';
37
+ export { createConnection, createLocalConnection, getConnection, getConnectionType, getDefaultSource, executeMalloy, executeSQL, setPendingConnection, setConnection, } from './executor/index.js';
38
38
  // --- internal imports ---
39
39
  import { parse } from './parser/index.js';
40
40
  import { buildTableSpec, generateQueryPlan, generateMalloyQueries, buildGridSpec } from './compiler/index.js';
41
- import { executeMalloy, setPendingConnection, } from './executor/index.js';
41
+ import { executeMalloy, setPendingConnection, setConnection, } from './executor/index.js';
42
42
  import { renderGridToHTML } from './renderer/index.js';
43
43
  /**
44
44
  * High-level TPL API for parsing, compiling, executing, and rendering.
@@ -185,6 +185,41 @@ export function fromBigQueryTable(options) {
185
185
  dialect: 'bigquery',
186
186
  });
187
187
  }
188
+ /**
189
+ * Query a table using a pre-built Malloy connection.
190
+ * Use this when you have an existing Connection instance (e.g., a
191
+ * BigQueryConnection constructed with in-memory credentials).
192
+ *
193
+ * @example
194
+ * ```typescript
195
+ * import { BigQueryConnection } from '@malloydata/db-bigquery';
196
+ * import { fromConnection } from 'tplm-lang';
197
+ *
198
+ * const connection = new BigQueryConnection('bigquery', undefined, {
199
+ * projectId: 'my-project',
200
+ * credentials: { client_email, private_key },
201
+ * });
202
+ *
203
+ * const tpl = fromConnection({
204
+ * connection,
205
+ * table: 'my-project.my_dataset.sales',
206
+ * });
207
+ * const { html } = await tpl.query('TABLE ROWS region * revenue.sum COLS quarter;');
208
+ * ```
209
+ */
210
+ export function fromConnection(options) {
211
+ const dialect = options.dialect ?? 'bigquery';
212
+ const connType = dialect === 'duckdb' ? 'duckdb' : 'bigquery';
213
+ setConnection(options.connection, connType);
214
+ const sourceName = 'data';
215
+ const prefix = dialect === 'duckdb' ? 'duckdb' : 'bigquery';
216
+ const model = `source: ${sourceName} is ${prefix}.table('${options.table}')`;
217
+ return new EasyTPL(model, sourceName, {
218
+ ...options,
219
+ tablePath: options.table,
220
+ dialect,
221
+ });
222
+ }
188
223
  // Import dimension utilities from compiler
189
224
  import { parseDimensionMappings, detectDimensionOrdering, } from './compiler/dimension-utils.js';
190
225
  /**
package/package.json CHANGED
@@ -1,18 +1,21 @@
1
1
  {
2
2
  "name": "tplm-lang",
3
- "version": "0.3.2",
3
+ "version": "0.3.4",
4
4
  "description": "TPLm - Table Producing Language backed by Malloy.",
5
5
  "type": "module",
6
- "main": "./dist/index.js",
6
+ "main": "./dist/index.cjs",
7
+ "module": "./dist/index.js",
7
8
  "types": "./dist/index.d.ts",
8
9
  "exports": {
9
10
  ".": {
11
+ "types": "./dist/index.d.ts",
10
12
  "import": "./dist/index.js",
11
- "types": "./dist/index.d.ts"
13
+ "require": "./dist/index.cjs"
12
14
  }
13
15
  },
14
16
  "files": [
15
17
  "dist/**/*.js",
18
+ "dist/**/*.cjs",
16
19
  "dist/**/*.d.ts",
17
20
  "packages/parser/tpl.pegjs",
18
21
  "packages/renderer/tpl-table.css",
@@ -21,7 +24,7 @@
21
24
  ],
22
25
  "scripts": {
23
26
  "build:parser": "peggy --format es packages/parser/tpl.pegjs -o packages/parser/parser.js && mkdir -p dist/parser && cp packages/parser/parser.js packages/parser/parser.d.ts dist/parser/",
24
- "build": "npm run build:parser && tsc",
27
+ "build": "npm run build:parser && tsc && tsup",
25
28
  "test": "vitest",
26
29
  "test:run": "vitest run",
27
30
  "docs:dev": "cd docs-site && npm run docs:dev",
@@ -33,6 +36,7 @@
33
36
  "@malloydata/db-duckdb": "^0.0.326",
34
37
  "@types/node": "^20.0.0",
35
38
  "peggy": "^4.0.0",
39
+ "tsup": "^8.0.0",
36
40
  "typescript": "^5.0.0",
37
41
  "vitest": "^4.0.16"
38
42
  },