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

restlessronin
The repo is at GitHub - cyberchitta/openai_ex: Community maintained Elixir library for OpenAI API · GitHub. Docs are at OpenaiEx User Gu...
152 12366 136
New
type1fool
WebAuthnLiveComponent WebAuthnComponents See this post about renaming the package. Passwordless authentication for Phoenix LiveView app...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
ausimian
Castle is a package I put together to assist with building Elixir releases capable of being live-upgraded via the ‘official’ release hand...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
kip
In 2021 I started a new library called Tempo with the objective of modelling time as a set of intervals - not as instants. In 2022 I gave...
New

Other Trending Topics Top

budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
Null-logic-0
What IDE or editor are you using for Elixir development? Personally, I use Zed, and I really like it, but sometimes I wish there were a ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews