prql-js
JavaScript bindings for prql-compiler.
Installation
1. npm install prql-js
Usage
Currently these functions are exposed
1. function compile(prql_query: string, options?: CompileOptions): string;
3. function prql_to_pl(prql_query: string): string;
5. function pl_to_rq(pl_json: string): string;
7. function rq_to_sql(rq_json: string): string;
From Node.js
Direct usage
1. const prqljs = require("prql-js");
3. const sql = prqljs.compile(`from employees | select first_name`);
4. console.log(sql);
Options
1. const opts = new prql.CompileOptions();
2. opts.target = "sql.mssql";
3. opts.format = false;
4. opts.signature_comment = false;
6. const sql = prqljs.compile(`from employees | take 10`, opts);
7. console.log(sql);
Template literal
1. const prqljs = require("prql-js");
2. const prql = (string) => prqljs.compile(string[0] || "");
4. const sql = prql`from employees | select first_name`;
5. console.log(sql);
Template literal with newlines
1. const prqljs = require("prql-js");
2. const prql = (string) => prqljs.compile(string[0] || "");
4. const sql = prql`
5. from employees
6. select first_name
7. `;
8. console.log(sql);
From a browser
1. <html>
2. <head>
3. <script src="./node_modules/prql-js/dist/web/prql_js.js"></script>
4. <script>
5. const { compile } = wasm_bindgen;
7. async function run() {
8. await wasm_bindgen("./node_modules/prql-js/dist/web/prql_js_bg.wasm");
9. const sql = compile("from employees | select first_name");
11. console.log(sql);
12. }
14. run();
15. </script>
16. </head>
18. <body></body>
19. </html>
From a framework or a bundler
1. import compile from "prql-js/dist/bundler";
3. const sql = compile(`from employees | select first_name`);
4. console.log(sql);
Errors
Errors are returned as following object, serialized as a JSON array:
1. interface ErrorMessage {
2. /// Plain text of the error
3. reason: string;
4. /// A list of suggestions of how to fix the error
5. hint: string | null;
6. /// Character offset of error origin within a source file
7. span: [number, number] | null;
9. /// Annotated code, containing cause and hints.
10. display: string | null;
11. /// Line and column number of error origin within a source file
12. location: SourceLocation | null;
13. }
15. /// Location within the source file.
16. /// Tuples contain:
17. /// - line number (0-based),
18. /// - column number within that line (0-based),
19. interface SourceLocation {
20. start: [number, number];
22. end: [number, number];
23. }
These errors can be caught as such:
1. try {
2. const sql = prqlJs.compile(`from employees | foo first_name`);
3. } catch (error) {
4. const errorMessages = JSON.parse(error.message).inner;
6. console.log(errorMessages[0].display);
7. console.log(errorMessages[0].location);
8. }
Development
Build:
1. npm run build
This builds Node, bundler and web packages in the dist path.
Test:
1. npm test
Notes
- This uses wasm-pack to generate bindings1.
- We’ve added an
npmlayer on top of the usual approach of just usingwasm-pack, so we can distribute a single package with targets ofnode,bundlerandno-modules— somewhat inverting the approach recommended bywasm-pack. The build instruction goes in abuildscript, rather than apackscript.
1
1. Though we would be very open to other approaches, given wasm-pack does not
2. seem maintained, and we're eliding many of its features to build for three
3. targets. See <https://github.com/PRQL/prql/issues/1836> for more details.
