Skip to content

Commit c0b87af

Browse files
committed
Add a sample nodejs project for compiling Malloy
1 parent 8506b8c commit c0b87af

4 files changed

Lines changed: 2353 additions & 0 deletions

File tree

sample_compilers/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Sample Scripts
2+
3+
A set of sample scripts for working with Malloy models.
4+
5+
## Setup
6+
7+
First, run `npm install`
8+
9+
## compile_malloy_duckdb.js (node.js)
10+
11+
A script to compile a Malloy model. Returns JSON containing either the compiled model or a list of compilation errors.
12+
13+
```
14+
$ npm run compile ../faa/flights.malloy
15+
```
16+
17+
OR
18+
19+
```
20+
$ node compile_malloy_duckdb.js ../faa/flights.malloy
21+
```
22+
23+
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/**
2+
* compile_duckdb.js
3+
*
4+
* Usage:
5+
* node compile_duckdb.js <path-to-malloy-file>
6+
*
7+
* Arguments:
8+
* <path-to-malloy-file> Path to a .malloy model file to compile.
9+
*
10+
* Example:
11+
* node compile_duckdb.js ../models/shapes.malloy
12+
*
13+
* This script will compile the given Malloy model using a DuckDB connection.
14+
*/
15+
16+
const fs = require("fs").promises;
17+
const path = require("path");
18+
19+
const { API, SingleConnectionRuntime } = require("@malloydata/malloy");
20+
const { DuckDBConnection } = require("@malloydata/db-duckdb");
21+
22+
async function setupDuckDb(workingDirectory) {
23+
// Initialize DuckDB WASM connection
24+
const duckdbConnection = new DuckDBConnection({
25+
workingDirectory,
26+
});
27+
await duckdbConnection.init();
28+
return duckdbConnection;
29+
}
30+
31+
// Compile a Malloy model from text
32+
async function compileMalloyModel(connection, malloyModelText) {
33+
const runtime = new SingleConnectionRuntime({ connection });
34+
// Try to compile the model
35+
try {
36+
const model = await runtime.loadModel(malloyModelText);
37+
// console.log('Malloy model compiled successfully:', model);
38+
39+
const materializedModel = await model.getModel();
40+
// console.log('Materialized model successfully:', materializedModel);
41+
42+
return {
43+
ok: true,
44+
data: model,
45+
};
46+
} catch (e) {
47+
return {
48+
ok: false,
49+
errors: e.problems,
50+
};
51+
}
52+
}
53+
54+
async function run() {
55+
const filePath = process.argv[2];
56+
if (!filePath) {
57+
console.error("Usage: node compile_duckdb.js <path-to-malloy-file>");
58+
process.exit(1);
59+
}
60+
try {
61+
const absolutePath = path.resolve(filePath);
62+
const malloyModelText = await fs.readFile(absolutePath, "utf8");
63+
// Provide the working directory of the model so Malloy will resolve data files
64+
// relative to the model and not to this script.
65+
const workingDirectory = path.dirname(absolutePath);
66+
const duckDbConnection = await setupDuckDb(workingDirectory);
67+
const result = await compileMalloyModel(duckDbConnection, malloyModelText);
68+
if (!result.ok) {
69+
console.error(
70+
JSON.stringify(
71+
{
72+
ok: false,
73+
message: "Malloy model compilation failed.",
74+
errors: result.errors,
75+
},
76+
undefined,
77+
2
78+
)
79+
);
80+
process.exit(1);
81+
}
82+
83+
console.log(
84+
JSON.stringify({
85+
ok: true,
86+
message: "Malloy model compiled successfully.",
87+
model: result.data
88+
}, undefined, 2),
89+
);
90+
} catch (err) {
91+
console.error(
92+
JSON.stringify(
93+
{
94+
ok: false,
95+
message: "Failed to read or compile Malloy model.",
96+
errors: [err.message],
97+
},
98+
undefined,
99+
2
100+
)
101+
);
102+
process.exit(1);
103+
}
104+
}
105+
106+
run();

0 commit comments

Comments
 (0)