tylerbarker

tylerbarker

SqlKit - Execute raw SQL in strings or .sql files, get maps and structs back

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

Most Liked

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!

Where Next?

Popular in Announcing Top

josevalim
Hi everyone, We would like to announce that Plataformatec is working on a new MySQL driver called MyXQL. Our goal is to eventually integ...
New
josevalim
Yes, yet another parser combinator library! Most of the parser combinators in the ecosystem are either compile-time, often using AST tra...
159 19228 141
New
gabrielpoca
Hello everyone! I want to share with you something that I’m really proud of: https://stillstatic.io/ Still is a static site builder for...
New
mspanc
I am pleased to announce an initial release of the Membrane Framework - an Elixir-based framework with special focus on processing multim...
New
deadtrickster
I’ve just released stable versions of my Prometheus Elixir libs: Elixir client [docs]; Ecto collector [docs]; Plugs instrumenter/Export...
New
kelvinst
Hey everyone! Well, we made this lib a while ago and now we decided to finally go out and public with it! It’s a tool for creating and m...
New
tmbb
I’ve been working on two packages (not on hex.pm yet) to build admin interfaces for phoenix apps: bureaucrat - which contains a bunch ...
New
kip
Image is an image processing library for Elixir. It is based upon the fabulous vix library that provides a libvips wrapper for Elixir. I...
622 18474 194
New
woylie
I released Doggo, a collection of unstyled Phoenix components. https://github.com/woylie/doggo Features Unstyled Phoenix components....
New
wfgilman
I’ve cleaned up and open sourced three financial libraries I was using for my company. They are bindings for the APIs of these three comp...
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30877 112
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement