tylerbarker

tylerbarker

Overview

SqlKit came about when I wanted to execute SQL inside .sql files at work without having to pollute my context functions with File I/O and Repo.query result transformation.

In making it into a library I’ve expanded it’s usefulness to accommodate raw SQL strings, and support a wide array of databases:

  • PostgreSQL
  • MySQL/MariaDB
  • SQLite
  • SQL Server
  • ClickHouse
  • DuckDB via duckdbex.

With the exception of DuckDB, this support comes via ecto_sql and the relevant drivers.

Some example code from the README:

# Direct SQL execution
defmodule MyApp.Accounts do
  alias MyApp.Accounts.User

  def get_active_users(company_id, min_age) do
    SqlKit.query_all(MyApp.Repo, """
      SELECT id, name, email, age
      FROM users
      WHERE company_id = $1
        AND age >= $2
        AND active = true
      ORDER BY name
    """, [company_id, min_age], as: User)
  end
end

# File-based SQL
defmodule MyApp.Accounts.SQL do
  use SqlKit,
    otp_app: :my_app,
    repo: MyApp.Repo,
    dirname: "accounts",
    files: ["active_users.sql", "another_query.sql"]
end

defmodule MyApp.Accounts do
  alias MyApp.Accounts.SQL # `use SqlKit` module
  alias MyApp.Accounts.User

  def get_active_users(company_id, min_age) do
    SQL.query_all("active_users.sql", [company_id, min_age], as: User)
  end
end

# Usage
MyApp.Accounts.get_active_users(123, 21)
# => [%User{id: 1, name: "Alice", email: "alice@example.com", age: 30}, ...]

Differences to other libraries

SQLx

SQLx is a very popular database driver in the Rust ecosystem which definitely served as inspiration for SqlKit, however at this stage it is much more sophisticated with things compile-time guarantees of your queries. Bringing SqlKit closer to SQLx in features is something I’d be interested in exploring eventually if it proves useful to enough people.

AyeSQL

AyeSQL is an excellent library with similar goals, which truthfully I wasn’t aware of until I was about to release v0.1.0 of sql_kit. The primary difference is SqlKit just works with plain SQL and does less function generation - it doesn’t do any parsing of your queries to facilitate named parameters or utilise magic comments to house multiple queries in a single file.

On the other hand, using plain SQL and leaning on Ecto means SqlKit supports more databases, and IMO is a bit simpler to adopt.

SQL

SQL is another great library focusing on bringing compile-time guarantees to raw SQL queries similar to SQLx via an ~SQL"" sigil. AFAIK it doesn’t lean on Ecto at all, and apparently gets a huge performance/concurrency boost as a result. Definitely one to watch.

Other Notes

This is early in development. It’s well tested across all the databases it claims to support, but I’ve only used this myself in production on Postgres.

Though I wrote the original use macro entirely by hand, in making this into a library I’ve made significant use of Claude Code using Opus 4.5 which has been great, in particular for lightening the load ensuring this has high quality docs and tests. I’m calling it out because I know for some that’s a deal breaker, but FWIW I’ve been reviewing everything along the way.

On DuckDB

I’ve not used DuckDB a great deal myself, so I’m very open to feedback regarding how SqlKit supports it with the connection pooling etc and how that could be better. Just raise an issue with your thoughts.

Links

https://github.com/tylerbarker/sql_kit

Showing Posts 1 to 5

mayel

mayel

Looks interesting! Does the mapping to structs handle joins/associations as well?

Schultzer

Schultzer

Great to see more people lean into SQL. To paraphrase what Andy Pavlo once said: what goes around comes around.

garrison

garrison

It was actually Stonebraker who said this! The later paper with Andy and Stonebraker revisited the inevitability of the relational model in the context of MongoDB and company, riffing on the original.

The irony, of course, is that Stonebraker hated SQL, favoring his own (superior) QUEL language, and was quite bitter that SQL had won due to IBM’s backing.

martosaur

martosaur

thank you for this note, and for human-written announcement too. This is very helpful!

tylerbarker

tylerbarker OP

Thanks! At this stage I’ve not made any efforts to replicating Ecto’s preloading of associations, but I do intend to look into it. For now, I think you can probably get a long way with doing it yourself in SQL but tbh I’ve not done a lot of testing with nested structures in general so that’s a good callout for me to look into.

— All posts loaded —

Where Next? Top

Trending in Announcing Top

wojtekmach
Hey everyone! Req is an HTTP client for Elixir that I’ve been working on for quite some time. There is already a lot of HTTP clients out...
New
handnot2
Samly can be used to enable SAML 2.0 Single Sign On in a Plug/Phoenix application. This library uses Erlang esaml to provide plug enabl...
New
woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New
restlessronin
The repo is at GitHub - cyberchitta/openai_ex: Community maintained Elixir library for OpenAI API · GitHub. Docs are at OpenaiEx User Gu...
152 11030 135
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
fuelen
Hi all! I want to present a small library which provides a mix task for generating an Entity-Relationship Diagram for Ecto schemas. You...
New
anuaralfetahe
Hello Published a new library - ProcessHub! ProcessHub is a library designed to manage process distribution within the Elixir cluster. ...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
mudasobwa
I am seeing a lot of aplications of Argumentum ad Vericundiam in software discussions. They do link some piece of writing and point us to...
New
bartblast
Hey folks, I just published a post about Hologram’s funding and where the project goes next - the short version: Curiosum as Main Spons...
New
sorenone
Today we’re releasing Oban for Python. Not an Oban client in Python. Not a pythonx wrapper embedded in Elixir. Nope, it’s a fully operati...
New
Herve37
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using. We’re particularly inte...
New
sergio
It’s not that it’s vocabulary is too advanced. It’s something worse. I get lost trying to follow even a paragraph written by Claude. It’...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews