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 put function to select? #169
Comments
You can just pass in a
|
Hi, thanks! I found this solution myself, although it took me some time before I tried to use raw here. Anyway I think that it would be nice to have direct support for having functions in select, so knex can autoescape field/table names there. I think that this can be closed, thanks! |
@kobalicekp Certainly something to consider. But it is worth noting that knex doesn't support arbitrary SQL functions anywhere. It's either a supported command or you use |
@bendrucker I meant escaping just to make the queries more portable. My query from the example above would look like: knex.column(knex.raw('to_char("dateCreated", \'YYYY-MM-DD\') as "dateCreated"')); I'm not sure how portable it would be outside of postgres. But if I think about it it would be difficult to write a good wrapper for this, covering all possible functions and their arguments. |
You hit on the exact problem. RDBMSes have 100s of functions and even where there's overlap the syntaxes are usually different. But by all means you should monkey patch functions you need often to keep things clean. |
Updated the set function
It would be good to have wrappers to the most common functions like COALESCE / NVL, UPPER, LOWER, etc, and also CASE/WHEN/ELSE |
@eduardobcastro please do those if you wish. knex.fn.* is place to implement them and remember to add also tests + docs. And remember to test also that they works in a nested manner. To be honest I don't see much use to have them as own functions, since many times one still needs to add casting etc. to when used and using knex not that complicated to use, unless there are subtle differences between how those functions work between db dialects . |
Hi,
How can I use function in select. For example I would like to do something like this:
...
knex.column("to_char(dateCreated) as dateCreated");
...
without having to write the whole select as a raw select.
Thanks
The text was updated successfully, but these errors were encountered: