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

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more