Coder Social home page Coder Social logo

prqlx's Introduction

PRQLX

Combining the query language of PRQL with the macro powers of SQLX.

PRQL is an amazing DSL for sql, but it doesn't have native support in sqlx. This crate bridges the gap by compiling prql in the rust macro itself before it's sent to sqlx (and your database). This means you can use the prql syntax with:

  • All supported sqlx database
  • Nice compiler errors
  • Fully typed rust

Usage

cargo add prqlx
cargo add sqlx # requires sqlx to be installed by you so you can configure it correctly
use prqlx::{query, query_as};

async fn test_query() {
    // get your sqlx pool in whatever way
    let pool = get_database_pool().await;

    // simply use it like a regular sqlx query, except you now use PRQL!
    let val = query!(
        "
        from users
        select { id, name }
        "
    )
    .fetch_all(&pool)
    .await
    .unwrap();

    // Same thing with query_as!
    // Bindings also just work:tm:
    let val2 = query_as!(
        User,
        "
        from users
        select { id, name }
        filter id == $1
        ",
        123
    )
    .fetch_one(&pool)
    .await
    .unwrap();

    println!("{:?}", val2);
}

Caveats

PRQL doesn't natively support ? for bindings. You will have to use s"?" for that. Rather use ${num} for bindings.

Todos

Not all macros have been implemented. The only ones are query and query_as. If you would like another to be added, please file an issue (or make a PR!).

Testing/Contributing

Before running cargo test, the database must first be setup. To do so, run cargo run --example prepare to prepare the sqlite database. After that, you can develop as usual.

prqlx's People

Contributors

suyashtnt avatar

Stargazers

 avatar Adrian Sieber avatar Gabriel Fontes avatar roger avatar Dan Hansen avatar Julien Scholz avatar  avatar Tsiry Sandratraina avatar Andrew Plaza avatar Cédric Eberhardt avatar Kazuki Yawata avatar Joshua Mo avatar  avatar wood avatar .-. avatar  avatar Orvar Segerström avatar

Watchers

Orvar Segerström avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.