@malloydata/malloy 0.0.364 → 0.0.366

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,13 +1,15 @@
1
1
  import type { URLReader } from '../../runtime_types';
2
2
  import type { Connection, LookupConnection } from '../../connection/types';
3
3
  import type { ConnectionConfigEntry } from '../../connection/registry';
4
- import type { BuildID, BuildManifest, BuildManifestEntry } from '../../model/malloy_types';
4
+ import type { LogMessage } from '../../lang/parse-log';
5
+ import type { BuildID, BuildManifest, BuildManifestEntry, VirtualMap } from '../../model/malloy_types';
5
6
  /**
6
7
  * The parsed contents of a malloy-config.json file.
7
8
  */
8
9
  export interface MalloyProjectConfig {
9
10
  connections?: Record<string, ConnectionConfigEntry>;
10
11
  manifestPath?: string;
12
+ virtualMap?: VirtualMap;
11
13
  }
12
14
  /**
13
15
  * In-memory manifest store. Reads, updates, and serializes manifest data.
@@ -71,9 +73,8 @@ export declare class Manifest {
71
73
  private _loadParsed;
72
74
  }
73
75
  /**
74
- * Loads and holds a Malloy project configuration (connections + manifest).
75
- *
76
- * Two construction modes:
76
+ * Loads and holds a Malloy project configuration (connections + manifest +
77
+ * virtualMap). Pass directly to Runtime — everything flows automatically.
77
78
  *
78
79
  * // From a URL — reads config + manifest via URLReader
79
80
  * const config = new MalloyConfig(urlReader, configURL);
@@ -82,19 +83,23 @@ export declare class Manifest {
82
83
  * // From text you already have
83
84
  * const config = new MalloyConfig(configText);
84
85
  *
85
- * // Either way, construct Runtime the same way
86
- * const runtime = new Runtime({
87
- * urlReader,
88
- * connections: config.connections,
89
- * buildManifest: config.manifest.buildManifest,
90
- * });
86
+ * // Either way, pass to Runtime connections, buildManifest, virtualMap
87
+ * // all come from the config. No manual wiring needed.
88
+ * const runtime = new Runtime({config, urlReader});
89
+ *
90
+ * To override specific fields, mutate the config before passing:
91
+ * config.connectionMap = myCustomConnections;
91
92
  */
92
93
  export declare class MalloyConfig {
93
94
  private readonly _urlReader?;
94
95
  private readonly _configURL?;
95
96
  private _data;
97
+ private _log;
96
98
  private _connectionMap;
97
99
  private readonly _manifest;
100
+ private _managedLookup;
101
+ private _connectionLookupOverride;
102
+ private _onConnectionCreated;
98
103
  constructor(configText: string);
99
104
  constructor(urlReader: URLReader, configURL: string);
100
105
  /**
@@ -119,13 +124,46 @@ export declare class MalloyConfig {
119
124
  set connectionMap(map: Record<string, ConnectionConfigEntry>);
120
125
  /**
121
126
  * A LookupConnection built from the current connectionMap via the
122
- * connection type registry. Each access creates a fresh snapshot
123
- * changing connectionMap after this does not affect previously-returned
124
- * LookupConnections.
127
+ * connection type registry. The result is cached repeated access
128
+ * returns the same object (and the same underlying connections).
129
+ *
130
+ * If `connectionLookup` has been set, returns the override instead.
131
+ *
132
+ * Changing `connectionMap` invalidates the cache; the next access
133
+ * creates a fresh ManagedConnectionLookup.
125
134
  */
126
135
  get connections(): LookupConnection<Connection>;
136
+ /**
137
+ * Override the connection lookup entirely. When set, the `connections`
138
+ * getter returns this instead of building from `connectionMap`.
139
+ * Use this when you need to merge config connections with other sources.
140
+ */
141
+ get connectionLookup(): LookupConnection<Connection> | undefined;
142
+ set connectionLookup(lookup: LookupConnection<Connection> | undefined);
143
+ /**
144
+ * Callback invoked once per connection immediately after factory creation.
145
+ * Use for post-creation setup (e.g., registering WASM file handlers).
146
+ * Must be set before the first connection is looked up.
147
+ */
148
+ set onConnectionCreated(cb: ((name: string, connection: Connection) => void) | undefined);
149
+ /**
150
+ * Close all connections created by this config's internal managed lookup.
151
+ * Does nothing if connections were overridden via `connectionLookup`.
152
+ */
153
+ close(): Promise<void>;
127
154
  /**
128
155
  * The Manifest object. Always exists, may be empty if not yet loaded.
129
156
  */
130
157
  get manifest(): Manifest;
158
+ /**
159
+ * The VirtualMap parsed from config, if present.
160
+ */
161
+ get virtualMap(): VirtualMap | undefined;
162
+ /**
163
+ * Errors and warnings from parsing and validating the config.
164
+ * Includes JSON parse errors (severity 'error') and schema validation
165
+ * warnings (severity 'warn') such as unknown keys, unknown connection
166
+ * types, wrong value types, and missing environment variables.
167
+ */
168
+ get log(): LogMessage[];
131
169
  }
@@ -123,30 +123,18 @@ class Manifest {
123
123
  }
124
124
  // New format: {entries: {...}, strict?: boolean}
125
125
  // Old format: {buildId: {tableName}, ...} (flat record, no "entries" key)
126
- let entries;
127
- if (parsed['entries'] && typeof parsed['entries'] === 'object') {
128
- entries = parsed['entries'];
129
- }
130
- else {
131
- // Treat as old flat-record format: every key with a tableName is an entry
132
- entries = {};
133
- for (const [key, val] of Object.entries(parsed)) {
134
- if (key !== 'strict' &&
135
- val &&
136
- typeof val === 'object' &&
137
- 'tableName' in val) {
138
- entries[key] = val;
139
- }
126
+ const rawEntries = isRecord(parsed['entries']) ? parsed['entries'] : parsed;
127
+ for (const [key, val] of Object.entries(rawEntries)) {
128
+ if (key !== 'strict' && isBuildManifestEntry(val)) {
129
+ this._manifest.entries[key] = val;
140
130
  }
141
131
  }
142
- Object.assign(this._manifest.entries, entries);
143
132
  }
144
133
  }
145
134
  exports.Manifest = Manifest;
146
135
  /**
147
- * Loads and holds a Malloy project configuration (connections + manifest).
148
- *
149
- * Two construction modes:
136
+ * Loads and holds a Malloy project configuration (connections + manifest +
137
+ * virtualMap). Pass directly to Runtime — everything flows automatically.
150
138
  *
151
139
  * // From a URL — reads config + manifest via URLReader
152
140
  * const config = new MalloyConfig(urlReader, configURL);
@@ -155,17 +143,20 @@ exports.Manifest = Manifest;
155
143
  * // From text you already have
156
144
  * const config = new MalloyConfig(configText);
157
145
  *
158
- * // Either way, construct Runtime the same way
159
- * const runtime = new Runtime({
160
- * urlReader,
161
- * connections: config.connections,
162
- * buildManifest: config.manifest.buildManifest,
163
- * });
146
+ * // Either way, pass to Runtime connections, buildManifest, virtualMap
147
+ * // all come from the config. No manual wiring needed.
148
+ * const runtime = new Runtime({config, urlReader});
149
+ *
150
+ * To override specific fields, mutate the config before passing:
151
+ * config.connectionMap = myCustomConnections;
164
152
  */
165
153
  class MalloyConfig {
166
154
  constructor(urlReaderOrText, configURL) {
155
+ this._log = [];
167
156
  if (typeof urlReaderOrText === 'string') {
168
- this._data = parseConfigText(urlReaderOrText);
157
+ const { config, log } = parseConfigText(urlReaderOrText);
158
+ this._data = config;
159
+ this._log = log;
169
160
  this._connectionMap = this._data.connections
170
161
  ? { ...this._data.connections }
171
162
  : undefined;
@@ -199,7 +190,9 @@ class MalloyConfig {
199
190
  return;
200
191
  const result = await this._urlReader.readURL(new URL(this._configURL));
201
192
  const contents = typeof result === 'string' ? result : result.contents;
202
- this._data = parseConfigText(contents);
193
+ const parsed = parseConfigText(contents);
194
+ this._data = parsed.config;
195
+ this._log = parsed.log;
203
196
  this._connectionMap = this._data.connections
204
197
  ? { ...this._data.connections }
205
198
  : undefined;
@@ -219,18 +212,59 @@ class MalloyConfig {
219
212
  }
220
213
  set connectionMap(map) {
221
214
  this._connectionMap = map;
215
+ // Invalidate cached managed lookup — new map means new connections
216
+ this._managedLookup = undefined;
222
217
  }
223
218
  /**
224
219
  * A LookupConnection built from the current connectionMap via the
225
- * connection type registry. Each access creates a fresh snapshot
226
- * changing connectionMap after this does not affect previously-returned
227
- * LookupConnections.
220
+ * connection type registry. The result is cached repeated access
221
+ * returns the same object (and the same underlying connections).
222
+ *
223
+ * If `connectionLookup` has been set, returns the override instead.
224
+ *
225
+ * Changing `connectionMap` invalidates the cache; the next access
226
+ * creates a fresh ManagedConnectionLookup.
228
227
  */
229
228
  get connections() {
229
+ if (this._connectionLookupOverride) {
230
+ return this._connectionLookupOverride;
231
+ }
230
232
  if (!this._connectionMap) {
231
233
  throw new Error('Config not loaded. Call load() or loadConfig() first.');
232
234
  }
233
- return (0, registry_1.createConnectionsFromConfig)({ connections: this._connectionMap });
235
+ if (!this._managedLookup) {
236
+ this._managedLookup = (0, registry_1.createConnectionsFromConfig)({ connections: this._connectionMap }, this._onConnectionCreated);
237
+ }
238
+ return this._managedLookup;
239
+ }
240
+ /**
241
+ * Override the connection lookup entirely. When set, the `connections`
242
+ * getter returns this instead of building from `connectionMap`.
243
+ * Use this when you need to merge config connections with other sources.
244
+ */
245
+ get connectionLookup() {
246
+ return this._connectionLookupOverride;
247
+ }
248
+ set connectionLookup(lookup) {
249
+ this._connectionLookupOverride = lookup;
250
+ }
251
+ /**
252
+ * Callback invoked once per connection immediately after factory creation.
253
+ * Use for post-creation setup (e.g., registering WASM file handlers).
254
+ * Must be set before the first connection is looked up.
255
+ */
256
+ set onConnectionCreated(cb) {
257
+ this._onConnectionCreated = cb;
258
+ }
259
+ /**
260
+ * Close all connections created by this config's internal managed lookup.
261
+ * Does nothing if connections were overridden via `connectionLookup`.
262
+ */
263
+ async close() {
264
+ if (this._managedLookup) {
265
+ await this._managedLookup.close();
266
+ this._managedLookup = undefined;
267
+ }
234
268
  }
235
269
  /**
236
270
  * The Manifest object. Always exists, may be empty if not yet loaded.
@@ -238,16 +272,230 @@ class MalloyConfig {
238
272
  get manifest() {
239
273
  return this._manifest;
240
274
  }
275
+ /**
276
+ * The VirtualMap parsed from config, if present.
277
+ */
278
+ get virtualMap() {
279
+ var _a;
280
+ return (_a = this._data) === null || _a === void 0 ? void 0 : _a.virtualMap;
281
+ }
282
+ /**
283
+ * Errors and warnings from parsing and validating the config.
284
+ * Includes JSON parse errors (severity 'error') and schema validation
285
+ * warnings (severity 'warn') such as unknown keys, unknown connection
286
+ * types, wrong value types, and missing environment variables.
287
+ */
288
+ get log() {
289
+ return this._log;
290
+ }
241
291
  }
242
292
  exports.MalloyConfig = MalloyConfig;
243
293
  /**
244
294
  * Parse a config JSON string into a MalloyProjectConfig.
245
295
  * Invalid connection entries (missing `is`) are silently dropped.
296
+ * Returns the processed config and validation log.
246
297
  */
247
298
  function parseConfigText(jsonText) {
248
- var _a;
249
- const parsed = JSON.parse(jsonText);
250
- const connections = Object.fromEntries(Object.entries((_a = parsed.connections) !== null && _a !== void 0 ? _a : {}).filter(([, v]) => (0, registry_1.isConnectionConfigEntry)(v)));
251
- return { ...parsed, connections };
299
+ let parsed;
300
+ try {
301
+ parsed = JSON.parse(jsonText);
302
+ }
303
+ catch (e) {
304
+ return {
305
+ config: {},
306
+ log: [
307
+ {
308
+ message: `Invalid JSON: ${e instanceof Error ? e.message : String(e)}`,
309
+ severity: 'error',
310
+ code: 'config-validation',
311
+ },
312
+ ],
313
+ };
314
+ }
315
+ if (!isRecord(parsed)) {
316
+ return {
317
+ config: {},
318
+ log: [
319
+ {
320
+ message: 'config is not a JSON object',
321
+ severity: 'error',
322
+ code: 'config-validation',
323
+ },
324
+ ],
325
+ };
326
+ }
327
+ const rawConnections = parsed['connections'];
328
+ const connectionEntries = Object.entries(isRecord(rawConnections) ? rawConnections : {}).filter((entry) => (0, registry_1.isConnectionConfigEntry)(entry[1]));
329
+ const connections = Object.fromEntries(connectionEntries);
330
+ const result = { ...parsed, connections };
331
+ const virtualMap = parsed['virtualMap'];
332
+ if (virtualMap &&
333
+ typeof virtualMap === 'object' &&
334
+ !Array.isArray(virtualMap)) {
335
+ const outer = new Map();
336
+ for (const [connName, inner] of Object.entries(virtualMap)) {
337
+ if (inner && typeof inner === 'object' && !Array.isArray(inner)) {
338
+ const innerMap = new Map();
339
+ for (const [virtualName, tablePath] of Object.entries(inner)) {
340
+ if (typeof tablePath === 'string') {
341
+ innerMap.set(virtualName, tablePath);
342
+ }
343
+ }
344
+ outer.set(connName, innerMap);
345
+ }
346
+ }
347
+ result.virtualMap = outer;
348
+ }
349
+ return { config: result, log: validateConfig(parsed) };
350
+ }
351
+ const KNOWN_TOP_LEVEL_KEYS = new Set([
352
+ 'connections',
353
+ 'manifestPath',
354
+ 'virtualMap',
355
+ ]);
356
+ function isRecord(value) {
357
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
358
+ }
359
+ function isBuildManifestEntry(value) {
360
+ return isRecord(value) && typeof value['tableName'] === 'string';
361
+ }
362
+ function makeWarning(path, message) {
363
+ return {
364
+ message: `${path}: ${message}`,
365
+ severity: 'warn',
366
+ code: 'config-validation',
367
+ };
368
+ }
369
+ /**
370
+ * Validate a parsed config object against the connection type registry.
371
+ * Returns LogMessage[] — does not throw.
372
+ */
373
+ function validateConfig(data) {
374
+ const log = [];
375
+ for (const key of Object.keys(data)) {
376
+ if (!KNOWN_TOP_LEVEL_KEYS.has(key)) {
377
+ const suggestion = closestMatch(key, [...KNOWN_TOP_LEVEL_KEYS]);
378
+ const hint = suggestion ? `. Did you mean "${suggestion}"?` : '';
379
+ log.push(makeWarning(key, `unknown config key "${key}"${hint}`));
380
+ }
381
+ }
382
+ if (data['manifestPath'] !== undefined &&
383
+ typeof data['manifestPath'] !== 'string') {
384
+ log.push(makeWarning('manifestPath', '"manifestPath" should be a string'));
385
+ }
386
+ const connections = data['connections'];
387
+ if (connections === undefined)
388
+ return log;
389
+ if (!isRecord(connections)) {
390
+ log.push(makeWarning('connections', '"connections" should be an object'));
391
+ return log;
392
+ }
393
+ const registeredTypes = new Set((0, registry_1.getRegisteredConnectionTypes)());
394
+ for (const [name, rawEntry] of Object.entries(connections)) {
395
+ const prefix = `connections.${name}`;
396
+ if (!isRecord(rawEntry)) {
397
+ log.push(makeWarning(prefix, 'should be an object'));
398
+ continue;
399
+ }
400
+ if (!rawEntry['is']) {
401
+ log.push(makeWarning(prefix, 'missing required "is" field (connection type)'));
402
+ continue;
403
+ }
404
+ if (typeof rawEntry['is'] !== 'string') {
405
+ log.push(makeWarning(`${prefix}.is`, '"is" should be a string'));
406
+ continue;
407
+ }
408
+ const typeName = rawEntry['is'];
409
+ if (!registeredTypes.has(typeName)) {
410
+ const suggestion = closestMatch(typeName, [...registeredTypes]);
411
+ const hint = suggestion ? ` Did you mean "${suggestion}"?` : '';
412
+ log.push(makeWarning(`${prefix}.is`, `unknown connection type "${typeName}".${hint} Available types: ${[...registeredTypes].join(', ')}`));
413
+ continue;
414
+ }
415
+ const props = (0, registry_1.getConnectionProperties)(typeName);
416
+ if (!props)
417
+ continue;
418
+ const knownProps = new Map(props.map(p => [p.name, p]));
419
+ for (const [key, value] of Object.entries(rawEntry)) {
420
+ if (key === 'is')
421
+ continue;
422
+ const propDef = knownProps.get(key);
423
+ if (!propDef) {
424
+ const suggestion = closestMatch(key, [...knownProps.keys()]);
425
+ const hint = suggestion ? `. Did you mean "${suggestion}"?` : '';
426
+ log.push(makeWarning(`${prefix}.${key}`, `unknown property "${key}" for connection type "${typeName}"${hint}`));
427
+ continue;
428
+ }
429
+ // For env var references on non-json props, check the variable exists.
430
+ if (propDef.type !== 'json' && (0, registry_1.isValueRef)(value)) {
431
+ const env = value.env;
432
+ if (process.env[env] === undefined) {
433
+ log.push(makeWarning(`${prefix}.${key}`, `environment variable "${env}" is not set`));
434
+ }
435
+ continue;
436
+ }
437
+ const typeError = checkValueType(value, propDef.type);
438
+ if (typeError) {
439
+ log.push(makeWarning(`${prefix}.${key}`, `${typeError} (expected ${propDef.type})`));
440
+ }
441
+ }
442
+ }
443
+ return log;
444
+ }
445
+ function levenshtein(a, b) {
446
+ const m = a.length;
447
+ const n = b.length;
448
+ const dp = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0));
449
+ for (let i = 0; i <= m; i++)
450
+ dp[i][0] = i;
451
+ for (let j = 0; j <= n; j++)
452
+ dp[0][j] = j;
453
+ for (let i = 1; i <= m; i++) {
454
+ for (let j = 1; j <= n; j++) {
455
+ const cost = a[i - 1] === b[j - 1] ? 0 : 1;
456
+ dp[i][j] = Math.min(dp[i - 1][j] + 1, dp[i][j - 1] + 1, dp[i - 1][j - 1] + cost);
457
+ }
458
+ }
459
+ return dp[m][n];
460
+ }
461
+ function closestMatch(input, candidates) {
462
+ if (candidates.length === 0)
463
+ return undefined;
464
+ let best = candidates[0];
465
+ let bestDist = Infinity;
466
+ for (const c of candidates) {
467
+ const dist = levenshtein(input.toLowerCase(), c.toLowerCase());
468
+ if (dist < bestDist) {
469
+ bestDist = dist;
470
+ best = c;
471
+ }
472
+ }
473
+ const maxDist = Math.max(1, Math.floor(Math.max(input.length, best.length) / 3));
474
+ return bestDist <= maxDist ? best : undefined;
475
+ }
476
+ function checkValueType(value, expectedType) {
477
+ if (value === undefined || value === null)
478
+ return undefined;
479
+ switch (expectedType) {
480
+ case 'number':
481
+ if (typeof value !== 'number')
482
+ return `should be a number, got ${typeof value}`;
483
+ break;
484
+ case 'boolean':
485
+ if (typeof value !== 'boolean')
486
+ return `should be a boolean, got ${typeof value}`;
487
+ break;
488
+ case 'string':
489
+ case 'password':
490
+ case 'secret':
491
+ case 'file':
492
+ case 'text':
493
+ if (typeof value !== 'string')
494
+ return `should be a string, got ${typeof value}`;
495
+ break;
496
+ case 'json':
497
+ break;
498
+ }
499
+ return undefined;
252
500
  }
253
501
  //# sourceMappingURL=config.js.map
@@ -4,6 +4,7 @@ import type { ModelDef, Query as InternalQuery, SearchIndexResult, SearchValueMa
4
4
  import type { Dialect } from '../../dialect';
5
5
  import type { RunSQLOptions } from '../../run_sql_options';
6
6
  import type { CacheManager } from './cache';
7
+ import type { MalloyConfig } from './config';
7
8
  import type { ParseOptions, CompileOptions, CompileQueryOptions } from './types';
8
9
  import type { PreparedResult, Explore } from './core';
9
10
  import { Model, PreparedQuery } from './core';
@@ -13,11 +14,17 @@ type ModelString = string;
13
14
  type QueryURL = URL;
14
15
  type QueryString = string;
15
16
  type Connectionable = {
17
+ config: MalloyConfig;
18
+ connection?: undefined;
19
+ connections?: LookupConnection<Connection>;
20
+ } | {
16
21
  connection: Connection;
17
22
  connections?: undefined;
23
+ config?: undefined;
18
24
  } | {
19
25
  connections: LookupConnection<Connection>;
20
26
  connection?: undefined;
27
+ config?: undefined;
21
28
  };
22
29
  declare class FluentState<T> {
23
30
  protected runtime: Runtime;
@@ -39,14 +46,13 @@ export declare class Runtime {
39
46
  private _connections;
40
47
  private _eventStream;
41
48
  private _cacheManager;
49
+ private _config;
42
50
  private _buildManifest;
43
51
  private _virtualMap;
44
- constructor({ urlReader, connections, connection, eventStream, cacheManager, buildManifest, virtualMap, }: {
52
+ constructor({ urlReader, connections, connection, config, eventStream, cacheManager, }: {
45
53
  urlReader?: URLReader;
46
54
  eventStream?: EventStream;
47
55
  cacheManager?: CacheManager;
48
- buildManifest?: BuildManifest;
49
- virtualMap?: VirtualMap;
50
56
  } & Connectionable);
51
57
  /**
52
58
  * @return The `CacheManager` for this runtime instance.
@@ -69,6 +75,10 @@ export declare class Runtime {
69
75
  * When set, compiled queries automatically resolve persist sources
70
76
  * against this manifest. Can be overridden per-query via
71
77
  * CompileQueryOptions.buildManifest.
78
+ *
79
+ * When constructed with a MalloyConfig, falls through to
80
+ * config.manifest.buildManifest (a live reference — builder
81
+ * mutations are visible automatically).
72
82
  */
73
83
  get buildManifest(): BuildManifest | undefined;
74
84
  set buildManifest(manifest: BuildManifest | undefined);
@@ -77,6 +87,9 @@ export declare class Runtime {
77
87
  * When set, compiled queries automatically resolve virtual sources
78
88
  * against this map. Can be overridden per-query via
79
89
  * CompileQueryOptions.virtualMap.
90
+ *
91
+ * When constructed with a MalloyConfig, falls through to
92
+ * config.virtualMap.
80
93
  */
81
94
  get virtualMap(): VirtualMap | undefined;
82
95
  set virtualMap(map: VirtualMap | undefined);
@@ -152,21 +165,17 @@ export declare class Runtime {
152
165
  }
153
166
  export declare class ConnectionRuntime extends Runtime {
154
167
  readonly rawConnections: Connection[];
155
- constructor({ urlReader, connections, buildManifest, virtualMap, }: {
168
+ constructor({ urlReader, connections, }: {
156
169
  urlReader?: URLReader;
157
170
  connections: Connection[];
158
- buildManifest?: BuildManifest;
159
- virtualMap?: VirtualMap;
160
171
  });
161
172
  }
162
173
  export declare class SingleConnectionRuntime<T extends Connection = Connection> extends Runtime {
163
174
  readonly connection: T;
164
- constructor({ urlReader, connection, eventStream, cacheManager, buildManifest, virtualMap, }: {
175
+ constructor({ urlReader, connection, eventStream, cacheManager, }: {
165
176
  urlReader?: URLReader;
166
177
  eventStream?: EventStream;
167
178
  cacheManager?: CacheManager;
168
- buildManifest?: BuildManifest;
169
- virtualMap?: VirtualMap;
170
179
  connection: T;
171
180
  });
172
181
  get supportsNesting(): boolean;
@@ -46,11 +46,15 @@ class FluentState {
46
46
  * An environment for compiling and running Malloy queries.
47
47
  */
48
48
  class Runtime {
49
- constructor({ urlReader, connections, connection, eventStream, cacheManager, buildManifest, virtualMap, }) {
49
+ constructor({ urlReader, connections, connection, config, eventStream, cacheManager, }) {
50
50
  this.isTestRuntime = false;
51
- if (connections === undefined) {
51
+ if (config !== undefined) {
52
+ this._config = config;
53
+ connections = connections !== null && connections !== void 0 ? connections : config.connections;
54
+ }
55
+ else if (connections === undefined) {
52
56
  if (connection === undefined) {
53
- throw new Error('A LookupConnection<Connection> or Connection is required.');
57
+ throw new Error('A MalloyConfig, LookupConnection<Connection>, or Connection is required.');
54
58
  }
55
59
  connections = {
56
60
  lookupConnection: () => Promise.resolve(connection),
@@ -63,8 +67,6 @@ class Runtime {
63
67
  this._connections = connections;
64
68
  this._eventStream = eventStream;
65
69
  this._cacheManager = cacheManager;
66
- this._buildManifest = buildManifest;
67
- this._virtualMap = virtualMap;
68
70
  }
69
71
  /**
70
72
  * @return The `CacheManager` for this runtime instance.
@@ -95,9 +97,14 @@ class Runtime {
95
97
  * When set, compiled queries automatically resolve persist sources
96
98
  * against this manifest. Can be overridden per-query via
97
99
  * CompileQueryOptions.buildManifest.
100
+ *
101
+ * When constructed with a MalloyConfig, falls through to
102
+ * config.manifest.buildManifest (a live reference — builder
103
+ * mutations are visible automatically).
98
104
  */
99
105
  get buildManifest() {
100
- return this._buildManifest;
106
+ var _a, _b;
107
+ return (_a = this._buildManifest) !== null && _a !== void 0 ? _a : (_b = this._config) === null || _b === void 0 ? void 0 : _b.manifest.buildManifest;
101
108
  }
102
109
  set buildManifest(manifest) {
103
110
  this._buildManifest = manifest;
@@ -107,9 +114,13 @@ class Runtime {
107
114
  * When set, compiled queries automatically resolve virtual sources
108
115
  * against this map. Can be overridden per-query via
109
116
  * CompileQueryOptions.virtualMap.
117
+ *
118
+ * When constructed with a MalloyConfig, falls through to
119
+ * config.virtualMap.
110
120
  */
111
121
  get virtualMap() {
112
- return this._virtualMap;
122
+ var _a, _b;
123
+ return (_a = this._virtualMap) !== null && _a !== void 0 ? _a : (_b = this._config) === null || _b === void 0 ? void 0 : _b.virtualMap;
113
124
  }
114
125
  set virtualMap(map) {
115
126
  this._virtualMap = map;
@@ -236,25 +247,21 @@ exports.Runtime = Runtime;
236
247
  // ConnectionRuntime and SingleConnectionRuntime
237
248
  // =============================================================================
238
249
  class ConnectionRuntime extends Runtime {
239
- constructor({ urlReader, connections, buildManifest, virtualMap, }) {
250
+ constructor({ urlReader, connections, }) {
240
251
  super({
241
252
  connections: readers_1.FixedConnectionMap.fromArray(connections),
242
253
  urlReader,
243
- buildManifest,
244
- virtualMap,
245
254
  });
246
255
  this.rawConnections = connections;
247
256
  }
248
257
  }
249
258
  exports.ConnectionRuntime = ConnectionRuntime;
250
259
  class SingleConnectionRuntime extends Runtime {
251
- constructor({ urlReader, connection, eventStream, cacheManager, buildManifest, virtualMap, }) {
260
+ constructor({ urlReader, connection, eventStream, cacheManager, }) {
252
261
  super({
253
262
  urlReader,
254
263
  eventStream,
255
264
  cacheManager,
256
- buildManifest,
257
- virtualMap,
258
265
  connection,
259
266
  });
260
267
  this.connection = connection;
@@ -48,7 +48,7 @@ export type ConfigValue = string | number | boolean | ValueRef | JsonConfigValue
48
48
  /**
49
49
  * Type guard for ValueRef.
50
50
  */
51
- export declare function isValueRef(value: ConfigValue): value is ValueRef;
51
+ export declare function isValueRef(value: unknown): value is ValueRef;
52
52
  /**
53
53
  * Resolve a ValueRef to a string by looking up the environment variable.
54
54
  * Returns undefined if the env var is not set.
@@ -106,6 +106,20 @@ export declare function readConnectionsConfig(jsonText: string): ConnectionsConf
106
106
  */
107
107
  export declare function writeConnectionsConfig(config: ConnectionsConfig): string;
108
108
  /**
109
- * Create a LookupConnection from a ConnectionsConfig using registered factories.
109
+ * A LookupConnection with lifecycle management: close() shuts down all
110
+ * cached connections, and an optional onConnectionCreated callback fires
111
+ * once per connection after factory creation (before caching).
110
112
  */
111
- export declare function createConnectionsFromConfig(config: ConnectionsConfig): LookupConnection<Connection>;
113
+ export interface ManagedConnectionLookup extends LookupConnection<Connection> {
114
+ close(): Promise<void>;
115
+ }
116
+ /**
117
+ * Create a ManagedConnectionLookup from a ConnectionsConfig using registered
118
+ * factories. Connections are cached per name for the lifetime of the returned
119
+ * object. Call close() to shut down all cached connections.
120
+ *
121
+ * @param onConnectionCreated Optional callback invoked once per connection
122
+ * immediately after factory creation. Use this for post-creation setup
123
+ * (e.g., registering WASM file handlers).
124
+ */
125
+ export declare function createConnectionsFromConfig(config: ConnectionsConfig, onConnectionCreated?: (name: string, connection: Connection) => void): ManagedConnectionLookup;
@@ -22,7 +22,8 @@ function isValueRef(value) {
22
22
  value !== null &&
23
23
  !Array.isArray(value) &&
24
24
  Object.keys(value).length === 1 &&
25
- 'env' in value);
25
+ 'env' in value &&
26
+ typeof value.env === 'string');
26
27
  }
27
28
  /**
28
29
  * Resolve a ValueRef to a string by looking up the environment variable.
@@ -93,9 +94,15 @@ function writeConnectionsConfig(config) {
93
94
  return JSON.stringify(config, null, 2);
94
95
  }
95
96
  /**
96
- * Create a LookupConnection from a ConnectionsConfig using registered factories.
97
+ * Create a ManagedConnectionLookup from a ConnectionsConfig using registered
98
+ * factories. Connections are cached per name for the lifetime of the returned
99
+ * object. Call close() to shut down all cached connections.
100
+ *
101
+ * @param onConnectionCreated Optional callback invoked once per connection
102
+ * immediately after factory creation. Use this for post-creation setup
103
+ * (e.g., registering WASM file handlers).
97
104
  */
98
- function createConnectionsFromConfig(config) {
105
+ function createConnectionsFromConfig(config, onConnectionCreated) {
99
106
  const entries = Object.entries(config.connections);
100
107
  const firstConnectionName = entries.length > 0 ? entries[0][0] : undefined;
101
108
  const cache = new Map();
@@ -137,9 +144,19 @@ function createConnectionsFromConfig(config) {
137
144
  }
138
145
  }
139
146
  const connection = await typeDef.factory(connConfig);
147
+ if (onConnectionCreated) {
148
+ onConnectionCreated(connectionName, connection);
149
+ }
140
150
  cache.set(connectionName, connection);
141
151
  return connection;
142
152
  },
153
+ async close() {
154
+ const connections = [...cache.values()];
155
+ cache.clear();
156
+ for (const conn of connections) {
157
+ await conn.close();
158
+ }
159
+ },
143
160
  };
144
161
  }
145
162
  //# sourceMappingURL=registry.js.map
package/dist/index.d.ts CHANGED
@@ -9,8 +9,8 @@ export type { PreparedQuery, Field, AtomicField, ExploreField, QueryField, Sorta
9
9
  export type { QueryOptionsReader, RunSQLOptions } from './run_sql_options';
10
10
  export type { EventStream, ModelString, ModelURL, QueryString, QueryURL, URLReader, InvalidationKey, } from './runtime_types';
11
11
  export type { Connection, ConnectionConfig, ConnectionParameterValue, FetchSchemaOptions, InfoConnection, LookupConnection, PersistSQLResults, PooledConnection, TestableConnection, StreamingConnection, } from './connection/types';
12
- export { registerConnectionType, getConnectionProperties, getConnectionTypeDisplayName, getRegisteredConnectionTypes, isValueRef, resolveValue, } from './connection/registry';
13
- export type { ConnectionTypeFactory, ConnectionPropertyType, ConnectionPropertyDefinition, ConnectionTypeDef, ConnectionConfigEntry, ConnectionsConfig, ConfigValue, JsonConfigValue, ValueRef, } from './connection/registry';
12
+ export { registerConnectionType, getConnectionProperties, getConnectionTypeDisplayName, getRegisteredConnectionTypes, createConnectionsFromConfig, isValueRef, resolveValue, } from './connection/registry';
13
+ export type { ConnectionTypeFactory, ConnectionPropertyType, ConnectionPropertyDefinition, ConnectionTypeDef, ConnectionConfigEntry, ConnectionsConfig, ConfigValue, JsonConfigValue, ValueRef, ManagedConnectionLookup, } from './connection/registry';
14
14
  export { toAsyncGenerator } from './connection_utils';
15
15
  export { modelDefToModelInfo, sourceDefToSourceInfo } from './to_stable';
16
16
  export * as API from './api';
package/dist/index.js CHANGED
@@ -34,7 +34,7 @@ var __importStar = (this && this.__importStar) || (function () {
34
34
  })();
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
36
  exports.Malloy = exports.Model = exports.MalloyTranslator = exports.malloyToQuery = exports.constantExprToSQL = exports.isDateUnit = exports.isTimestampUnit = exports.composeSQLExpr = exports.indent = exports.expressionIsUngroupedAggregate = exports.expressionIsScalar = exports.expressionIsCalculation = exports.expressionIsAnalytic = exports.expressionIsAggregate = exports.mkFieldDef = exports.mkArrayDef = exports.isBasicArray = exports.isRepeatedRecord = exports.isSamplingRows = exports.isSamplingPercent = exports.isSamplingEnable = exports.isJoinedSource = exports.isJoined = exports.isCompoundArrayData = exports.isBasicAtomic = exports.isAtomic = exports.isSourceDef = exports.TinyParser = exports.Dialect = exports.spread = exports.literal = exports.variadicParam = exports.param = exports.makeParam = exports.sql = exports.maxScalar = exports.minAggregate = exports.anyExprType = exports.minScalar = exports.overload = exports.qtz = exports.arg = exports.registerDialect = exports.DatabricksDialect = exports.MySQLDialect = exports.SnowflakeDialect = exports.PostgresDialect = exports.TrinoDialect = exports.StandardSQLDialect = exports.DuckDBDialect = void 0;
37
- exports.makeDigest = exports.EMPTY_BUILD_MANIFEST = exports.PersistSource = exports.annotationToTaglines = exports.annotationToTag = exports.sqlKey = exports.API = exports.sourceDefToSourceInfo = exports.modelDefToModelInfo = exports.toAsyncGenerator = exports.resolveValue = exports.isValueRef = exports.getRegisteredConnectionTypes = exports.getConnectionTypeDisplayName = exports.getConnectionProperties = exports.registerConnectionType = exports.MalloyConfig = exports.Manifest = exports.CacheManager = exports.InMemoryModelCache = exports.Explore = exports.DataWriter = exports.Parse = exports.JSONWriter = exports.CSVWriter = exports.QueryMaterializer = exports.Result = exports.PreparedResult = exports.TimestampTimeframe = exports.DateTimeframe = exports.SourceRelationship = exports.JoinRelationship = exports.MalloyError = exports.FixedConnectionMap = exports.InMemoryURLReader = exports.EmptyURLReader = exports.SingleConnectionRuntime = exports.ConnectionRuntime = exports.AtomicFieldType = exports.Runtime = void 0;
37
+ exports.makeDigest = exports.EMPTY_BUILD_MANIFEST = exports.PersistSource = exports.annotationToTaglines = exports.annotationToTag = exports.sqlKey = exports.API = exports.sourceDefToSourceInfo = exports.modelDefToModelInfo = exports.toAsyncGenerator = exports.resolveValue = exports.isValueRef = exports.createConnectionsFromConfig = exports.getRegisteredConnectionTypes = exports.getConnectionTypeDisplayName = exports.getConnectionProperties = exports.registerConnectionType = exports.MalloyConfig = exports.Manifest = exports.CacheManager = exports.InMemoryModelCache = exports.Explore = exports.DataWriter = exports.Parse = exports.JSONWriter = exports.CSVWriter = exports.QueryMaterializer = exports.Result = exports.PreparedResult = exports.TimestampTimeframe = exports.DateTimeframe = exports.SourceRelationship = exports.JoinRelationship = exports.MalloyError = exports.FixedConnectionMap = exports.InMemoryURLReader = exports.EmptyURLReader = exports.SingleConnectionRuntime = exports.ConnectionRuntime = exports.AtomicFieldType = exports.Runtime = void 0;
38
38
  /*
39
39
  * Copyright 2023 Google LLC
40
40
  *
@@ -143,6 +143,7 @@ Object.defineProperty(exports, "registerConnectionType", { enumerable: true, get
143
143
  Object.defineProperty(exports, "getConnectionProperties", { enumerable: true, get: function () { return registry_1.getConnectionProperties; } });
144
144
  Object.defineProperty(exports, "getConnectionTypeDisplayName", { enumerable: true, get: function () { return registry_1.getConnectionTypeDisplayName; } });
145
145
  Object.defineProperty(exports, "getRegisteredConnectionTypes", { enumerable: true, get: function () { return registry_1.getRegisteredConnectionTypes; } });
146
+ Object.defineProperty(exports, "createConnectionsFromConfig", { enumerable: true, get: function () { return registry_1.createConnectionsFromConfig; } });
146
147
  Object.defineProperty(exports, "isValueRef", { enumerable: true, get: function () { return registry_1.isValueRef; } });
147
148
  Object.defineProperty(exports, "resolveValue", { enumerable: true, get: function () { return registry_1.resolveValue; } });
148
149
  var connection_utils_1 = require("./connection_utils");
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const MALLOY_VERSION = "0.0.364";
1
+ export declare const MALLOY_VERSION = "0.0.366";
package/dist/version.js CHANGED
@@ -2,5 +2,5 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.MALLOY_VERSION = void 0;
4
4
  // generated with 'generate-version-file' script; do not edit manually
5
- exports.MALLOY_VERSION = '0.0.364';
5
+ exports.MALLOY_VERSION = '0.0.366';
6
6
  //# sourceMappingURL=version.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@malloydata/malloy",
3
- "version": "0.0.364",
3
+ "version": "0.0.366",
4
4
  "license": "MIT",
5
5
  "exports": {
6
6
  ".": "./dist/index.js",
@@ -47,9 +47,9 @@
47
47
  "generate-version-file": "VERSION=$(npm pkg get version --workspaces=false | tr -d \\\")\necho \"// generated with 'generate-version-file' script; do not edit manually\\nexport const MALLOY_VERSION = '$VERSION';\" > src/version.ts"
48
48
  },
49
49
  "dependencies": {
50
- "@malloydata/malloy-filter": "0.0.364",
51
- "@malloydata/malloy-interfaces": "0.0.364",
52
- "@malloydata/malloy-tag": "0.0.364",
50
+ "@malloydata/malloy-filter": "0.0.366",
51
+ "@malloydata/malloy-interfaces": "0.0.366",
52
+ "@malloydata/malloy-tag": "0.0.366",
53
53
  "@noble/hashes": "^1.8.0",
54
54
  "antlr4ts": "^0.5.0-alpha.4",
55
55
  "assert": "^2.0.0",