Skip to content
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

How to get actual SQL query text? #8

Open
vsviridov opened this issue Sep 30, 2023 · 7 comments
Open

How to get actual SQL query text? #8

vsviridov opened this issue Sep 30, 2023 · 7 comments

Comments

@vsviridov
Copy link

I don't think the documentation ever mentions how to transform

from(Users).select('*').where(() => Users.fields.id.eq(1));

into

select * from users where id = 1;

to supply to my database driver...

Can you elaborate on this?

And how to get back the type of the resulting data, so I can inform the rest of the application about what's going on...

@vsviridov
Copy link
Author

Btw, I've written a quick-and-dirty codegen for sqlite that produces table definitions compatible with your library...
https://gist.github.com/vsviridov/f09d82768fba96459870b80b9168f914

@ClickerMonkey
Copy link
Owner

That's pretty neat!

You can use exec function which takes a connection and creates a function that is passed a query which returns the desired types.

const name = await from(GroupTable)

@ClickerMonkey
Copy link
Owner

I don't have explicit sqlite support yet but here's an example package that is for postgres:

https://github.com/ClickerMonkey/typed-query-builder/tree/master/packages/sql-pgsql

Essentially each DB only supports certain features, functions are named different things, etc. So these sql- packages is to support the types and functions as best as possible.

@ClickerMonkey
Copy link
Owner

To get the string and type data you use a Dialect (using the base dialect may be good enough for you if you don't want to implement a Sqlite one). There are a few helper types that can be seen here. These exec functions will return different things based on the options you pass:

export function exec<P = any>(access: PgsqlConnection, options?: PgsqlOptions<P>): <R>(expr: Expr<R>) => Promise<any>

@vsviridov
Copy link
Author

Thank you for the fast response. Another question... As I'm planning to use it with Bun's sqlite driver, and it favors prepared statements a lot.

Is there a way to have a query builder produce a query that has named placeholder values within the query text?

Such query is cached by the sqlite engine and can be invoked with parameters in a separate call...

@ClickerMonkey
Copy link
Owner

Yep, that's by default iirc. The base dialect should support named parameters just fine.

@vsviridov
Copy link
Author

So, the exec method seems to exist only on the Postgres package... And creating a new package for sqlite might be a bit beyond me at this stage...

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

No branches or pull requests

2 participants