Skip to content

Wrapping the SQL Tag Function #1059

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
danhalliday opened this issue Apr 13, 2025 · 0 comments
Open

Wrapping the SQL Tag Function #1059

danhalliday opened this issue Apr 13, 2025 · 0 comments

Comments

@danhalliday
Copy link

I have a load of simple queries to define, each returning one or many models which I have Typescript types for. At present I’m doing something like:

findById: async (id) => {
  const [user] = await sql<User[]>`
    SELECT *
    FROM users
    WHERE id = ${id}
  `
  return user ?? null
},

I would like to clean up the call sites to make the queries a bit more readable by creating a couple of helpers that do the whole thing inline — something like the following:

findById: async (id) => 
  await one`
    SELECT *
    FROM users
    WHERE id = ${id}
  `,

findMany: async (type) => 
  await many`
    SELECT *
    FROM users
    WHERE type = ${type}
  `,

But figuring out what types the wrapper tag functions need to have is a bit beyond me after looking through the Sql type’s definition.

All I really want to do is for many, pass through the result and cast it to the caller’s desired type, and for one, unwrap the first result and cast that to the caller’s desired type, which would be T | null.

Has anyone wrapped the sql tag function before and has the types handy, or knows how to do this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants