Skip to content

Commit c9ded40

Browse files
authored
Document sql.unsafe query fragments (#567)
1 parent 4f987d5 commit c9ded40

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,28 @@ If you know what you're doing, you can use `unsafe` to pass any string you'd lik
528528
```js
529529
sql.unsafe('select ' + danger + ' from users where id = ' + dragons)
530530
```
531+
532+
You can also nest `sql.unsafe` within a safe `sql` expression. This is useful if only part of your fraction has unsafe elements.
533+
534+
```js
535+
const triggerName = 'friend_created'
536+
const triggerFnName = 'on_friend_created'
537+
const eventType = 'insert'
538+
const schema_name = 'app'
539+
const table_name = 'friends'
540+
541+
await sql`
542+
create or replace trigger ${sql(triggerName)}
543+
after ${sql.unsafe(eventType)} on ${sql.unsafe(`${schema_name}.${table_name}`)}
544+
for each row
545+
execute function ${sql(triggerFnName)}()
546+
`
547+
548+
await sql`
549+
create role friend_service with login password ${sql.unsafe(`'${password}'`)}
550+
`
551+
```
552+
531553
</details>
532554

533555
## Transactions

0 commit comments

Comments
 (0)