alexdesousa / ayesql

Library for using raw SQL in Elixir
MIT License
138 stars 14 forks source link

How can I pass to AyeSQL multiple sql files at the same time #35

Closed gevera closed 3 months ago

gevera commented 6 months ago

I have multiple entities, in separate folders, with their own sql queries.

I would like to have a single entry for AyeSQL and pass it an array of sql files like so

  defqueries(["sql/orders/reads.sql", "sql/orders/writes .sql", "sql/clients/clients.sql","sql/payments/payments.sql" ])

or better yet, somehow AyeSQL to be able to read all the sql files in subfolders

defqueries("sql/**/*.sql")

Is it even possibe?

alexdesousa commented 4 months ago

@gevera Thanks for your interest in this project and sorry for the late reply to your issue!

The defqueries macro can be called several times in the same file as long as the queries' names do not clash e.g. given the following query files:

-- file `./all.sql`
-- name: get_hostnames
SELECT hostname FROM server;

and

-- file `./by_hostname.sql`
-- name: get_server_by_hostname
-- docs: Simple query
SELECT * FROM server WHERE hostname = :hostname;

then you can define an AyeSQL module as follows:

defmodule MyQueries do
  use AyeSQL

  defqueries("all.sql")
  defqueries("by_hostname.sql")
end

Note: Elixir will fail the compilation if the name: of the queries between the files clash.defqueries("sql/*/.sql")

This approach you've suggested:

defqueries("sql/**/*.sql")

looks like a nice improvement to the macro! :+1:

I hope this answer doesn't come extremely late and it's been useful :grin:

gevera commented 4 months ago

Thanks for the reply. No worries, event if it's late. AyeSQL its a great library that brings me closer to SQL in Elixir. And, yeah, i think calling just once

defqueries("sql/**/*.sql")

instead of multiple

  defqueries("all.sql")
  defqueries("by_hostname.sql")

is cleaner

Anyways, I'm keepimg an eye on the projects and updates. Cheers

alexdesousa commented 3 months ago

@gevera I'll close this issue for now. I'm not going to implement the wildcard definition for now. However, if you or someone else is willing to implement it, I'll be happy to review it and merge it :smile: