From ff472a0e4648f289a7a56a827e8347cf60152908 Mon Sep 17 00:00:00 2001 From: madflow Date: Sun, 23 Feb 2025 17:46:44 +0100 Subject: [PATCH] docs: dynamic ordering --- README.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/README.md b/README.md index b79c207f..6527d98c 100644 --- a/README.md +++ b/README.md @@ -342,6 +342,27 @@ select * from users select * from users where user_id = $1 ``` +### Dynamic ordering + +```js +const id = 1 +const order = { + username: 'asc' + created_at: 'desc' +} +await sql` + select + * + from ticket + where account = ${ id } + order by ${ + Object.entries(order).flatMap(([column, order], i) => + [i ? sql`,` : sql``, sql`${ sql(column) } ${ order === 'desc' ? sql`desc` : sql`asc` }`] + ) + } +` +``` + ### SQL functions Using keywords or calling functions dynamically is also possible by using ``` sql`` ``` fragments. ```js