prql-js

    Currently these functions are exposed

    1. function prql_to_pl(prql_query: string): string;
    2. function pl_to_rq(pl_json: string): string;
    3. function rq_to_sql(rq_json: string): string;

    Direct usage

    1. const prqljs = require("prql-js");
    2. const sql = prqljs.compile(`from employees | select first_name`);
    3. console.log(sql);

    Options

    1. const opts = new prql.CompileOptions();
    2. opts.target = "sql.mssql";
    3. opts.format = false;
    4. const sql = prqljs.compile(`from employees | take 10`, opts);
    5. console.log(sql);

    Template literal with newlines

    1. const prqljs = require("prql-js");
    2. const prql = (string) => prqljs.compile(string[0] || "");
    3. const sql = prql`
    4. select first_name
    5. `;
    6. console.log(sql);
    1. <html>
    2. <head>
    3. <script src="./node_modules/prql-js/dist/web/prql_js.js"></script>
    4. <script>
    5. const { compile } = wasm_bindgen;
    6. async function run() {
    7. await wasm_bindgen("./node_modules/prql-js/dist/web/prql_js_bg.wasm");
    8. const sql = compile("from employees | select first_name");
    9. console.log(sql);
    10. run();
    11. </script>
    12. </head>
    13. <body></body>
    1. import compile from "prql-js/dist/bundler";
    2. const sql = compile(`from employees | select first_name`);
    3. console.log(sql);

    Errors are returned as following object, serialized as a JSON array:

    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;
    5. console.log(errorMessages[0].display);
    6. console.log(errorMessages[0].location);
    7. }
    1. npm run build

    This builds Node, bundler and web packages in the dist path.

    Test:

    1. npm test
    • This uses to generate bindings1.

    1