Published
Edited
Aug 5, 2022
Importers
4 stars
Insert cell
Insert cell
Insert cell
prql`
from table
take 10
`
Insert cell
Insert cell
prql`
from table
take ${10}
`
Insert cell
Insert cell
prql`select * from table limit 10`
Insert cell
Insert cell
Insert cell
Insert cell
prql = {
// Import the JS bindings module.
const prql = await import(await FileAttachment("prql_js@5.js").url());

// Import the WASM module as bytes.
const wasmBytes = (
await FileAttachment("prql_js_bg@5.wasm").blob()
).arrayBuffer();

// Initialise the bindings module with the bytes.
await prql.default(wasmBytes);

// Return a function that passes its input to `compile` and pulls out the SQL.
// To make it an empty tagged template literal, we pass to `String.raw` with the `raw` parameter in the first argument, and spread out `params ?? []` to avoid a null spread.
// Then we check for errors, and if there aren't any, return the SQL.
return (query, ...params) => {
const result = prql.compile(String.raw({ raw: query }, ...(params ?? [])));

if (result.error) {
throw `PRQL encountered an error:\n${result.error.message}`;
}

return result.sql;
};
}
Insert cell
// Spec: https://observablehq.com/@observablehq/database-client-specification
class PRQLDatabaseClientWrapper {
constructor(db) {
this.db = db;
}

async queryStream(query, params, options) {
return this.db.queryStream(prql([query], params), options);
}
async query(query, params, options) {
return this.db.query(prql([query], params), options);
}
async describeTables({ schema } = {}) {
return this.db.describeTables({ schema });
}
async describeColumns({ schema, table } = {}) {
return this.db.describeColumns({ schema, table });
}
async sql(strings, ...params) {
return this.db.sql([prql(strings, params)]);
}

queryTag(strings, ...params) {
return [String.raw(strings, ...(params || [])), []];
}
}
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more