Defql: create Elixir functions with SQL as a body

The module that provides macros to create functions with SQL as a body.

Blog post about the library:

Github repo:


Thank you @fazibear for such interesting library.

I have a question: what are cases make the table: :table_name definition worthy to be duplicated?

In your example:

defmodule UserQuery do
  use Defql

  defselect get(conds), table: :users
  definsert add(params), table: :users
  defupdate update(params, conds), table: :users
  defdelete delete(conds), table: :users

  defquery get_by_name(name, limit) do
    "SELECT * FROM users WHERE name = $name LIMIT $limit"

Maybe you consider (architecturally) the possibility to have get/add/etc. queries to different tables within one xQuery module?


The table name is not duplicated. When you write the whole query with defquery you type table name inside a query string. But if you want to use other macros with are some kind of shortcuts for common queries, you have to provide a table name, but you don’t type any SQL.

1 Like

Yes, I’m talking about shortcut-macros. I wonder: what if define table :table_name once in xQuery module, for all shortcut macros? Any downsides?

1 Like

I can see use case where one table name per module has downsides. For example module

defmodule QueryHelpers do
  use Defql
  defselect get_users(conds), table: :users
  defselect get_accounts(conds), table: :accounts
  definsert add_user(params), table: :users
  definsert add_account(params), table: :accounts

Im not saying it’s a good solution, but having one table name per module, you would enforce programmer to write module per database table. I understand convenience, but rather advise against such restriction.

1 Like

Yea! That could be possible. It’s worth considering. Maybe with syntax like this:

defmodule UserQuery do
  use Defql, table: :users

  defselect get(conds)
  definsert add(params)
  defupdate update(params, conds)
  defdelete delete(conds)