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

readme: document unsafe's parameters #942

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,17 @@ If you know what you're doing, you can use `unsafe` to pass any string you'd lik
sql.unsafe('select ' + danger + ' from users where id = ' + dragons)
```

`unsafe` accepts the following arguments:

- `query` - The query in the form of an ordinary string. It may include positional parameters (e.g. `$1`, `$2`, etc.) for the Postgres database to substitute with arguments.
- `args` - When present, this is an array of the values of the query parameters. The value at index 0 corresponds to argument `$1`. You do not need to escape these values, as the database will safely substitute them into the query.
- `options` - When present, this is an object containing one of the following two key/value pairs:

| Option | Description |
| --- | --- |
| `prepare` | Creates a prepared statement with the query string as key for an automatically generated id. This is `false` by default because we can't know if the user is generating millions of different queries causing the DB to bloat in memory. Hence it's something the user explicitly needs to choose. (Mutually exclusive with `simple`.) |
| `simple` | Changes the query to use the simple protocol which doesn't allow parameters or prepared statements. But it does allow multiple statements. I'm thinking of deprecating this in favor of the `.simple()` chained method which will then work for both tagged queries and unsafe queries. (Mutually exclusive with `prepare`.) |

You can also nest `sql.unsafe` within a safe `sql` expression. This is useful if only part of your fraction has unsafe elements.

```js
Expand Down