Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions sample_compilers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Sample Scripts

A set of sample scripts for working with Malloy models.

## Setup

First, run `npm install`

## compile_malloy_duckdb.js (node.js)

A script to compile a Malloy model. Returns JSON containing either the compiled model or a list of compilation errors.

```
$ npm run compile ../faa/flights.malloy
```

OR

```
$ node compile_malloy_duckdb.js ../faa/flights.malloy
```


85 changes: 85 additions & 0 deletions sample_compilers/compile_malloy_duckdb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**
* compile_duckdb.js
*
* Usage:
* node compile_duckdb.js <path-to-malloy-file>
*
* Arguments:
* <path-to-malloy-file> Path to a .malloy model file to compile.
*
* Example:
* node compile_duckdb.js ../models/shapes.malloy
*
* This script will compile the given Malloy model using a DuckDB connection.
*/
/* eslint-disable no-console, no-process-exit */

const fs = require('fs').promises;
const path = require('path');

const {SingleConnectionRuntime} = require('@malloydata/malloy');
const {DuckDBConnection} = require('@malloydata/db-duckdb');

async function setupDuckDb(workingDirectory) {
// Initialize DuckDB WASM connection
const duckdbConnection = new DuckDBConnection({
workingDirectory,
});
await duckdbConnection.init();
return duckdbConnection;
}

// Compile a Malloy model from text
async function compileMalloyModel(connection, malloyModelText) {
const runtime = new SingleConnectionRuntime({connection});
// Try to compile the model
try {
const model = await runtime.loadModel(malloyModelText);
const materializedModel = await model.getModel();
return {
ok: true,
data: materializedModel,
};
} catch (e) {
return {
ok: false,
errors: e.problems,
};
}
}

async function run() {
const filePath = process.argv[2];
if (!filePath) {
return {
ok: false,
errors: [
'No Malloy file path provided.\n\nUsage: node compile_duckdb.js <path-to-malloy-file>',
],
};
}
try {
const absolutePath = path.resolve(filePath);
const malloyModelText = await fs.readFile(absolutePath, 'utf8');
// Provide the working directory of the model so Malloy will resolve data files
// relative to the model and not to this script.
const workingDirectory = path.dirname(absolutePath);
const duckDbConnection = await setupDuckDb(workingDirectory);
const result = await compileMalloyModel(duckDbConnection, malloyModelText);
return result;
} catch (err) {
return {
ok: false,
errors: [err.message],
};
}
}

run().then(result => {
if (!result.ok) {
console.error(JSON.stringify(result, null, 2));
process.exit(1);
} else {
console.log({...result, message: 'Malloy model compiled successfully.'});
}
});
Loading