Skip to content

Commit cbe7cac

Browse files
committed
Fix porsager#4 - Improve tagged template and sql() info
1 parent 88e4a97 commit cbe7cac

File tree

1 file changed

+51
-10
lines changed

1 file changed

+51
-10
lines changed

README.md

+51-10
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ More info for the `ssl` option can be found in the [Node.js docs for tls connect
6868

6969
## Query ```sql` ` -> Promise```
7070

71-
A query will always return a `Promise` which resolves to either an array `[...]` or `null` depending on the type of query. Destructuring is great to immediately access the first element.
71+
A query will always return a `Promise` which resolves to a results array `[...]{ rows, command }`. Destructuring is great to immediately access the first element.
7272

7373
```js
7474

@@ -108,7 +108,7 @@ const users = await sql`
108108

109109
## Stream ```sql` `.stream(fn) -> Promise```
110110

111-
If you want to handle rows returned by a query one by one you can use `.stream` which returns a promise that resolves once there are no more rows.
111+
If you want to handle rows returned by a query one by one, you can use `.stream` which returns a promise that resolves once there are no more rows.
112112
```js
113113

114114
await sql`
@@ -141,13 +141,17 @@ sql.notify('news', JSON.stringify({ no: 'this', is: 'news' }))
141141

142142
```
143143

144+
## Tagged template function ``` sql`` ```
145+
[Tagged template functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#Tagged_templates) are not just ordinary template literal strings. They allow the function to handle any parameters within before interpolation. This means that they can be used to enforce a safe way of writing queries, which is what Postgres.js does. Any generic value will be serialized according to an inferred type, and replaced by a PostgreSQL protocol placeholders `$1, $2, ...` and then sent to the database as a parameter to let it handle any need for escaping / casting.
146+
147+
This also means you cannot write dynamic queryes or concat queries together by simple string manipulation. To enable dynamic queries in a safe way, the `sql` function doubles as a regular function which escapes any value properly. It also includes overloads for common cases of inserting, selecting, updating and querying.
148+
144149
## Dynamic query helpers `sql() inside tagged template`
145150

146151
Postgres.js has a safe, ergonomic way to aid you in writing queries. This makes it easier to write dynamic inserts, selects, updates and where queries.
147152

148153
#### Insert
149154

150-
151155
```js
152156

153157
const user = {
@@ -157,20 +161,20 @@ const user = {
157161

158162
sql`
159163
insert into users ${
160-
sql(user)
164+
sql(user, 'name', 'age')
161165
}
162166
`
163167

164-
```
165-
166-
Is translated into a safe query like this:
167-
168-
```sql
168+
// Is translated into this query:
169169
insert into users (name, age) values ($1, $2)
170+
170171
```
171172

173+
You can leave out the column names and simply do `sql(user)` if you want to get all fields from the object as columns, but be careful not to allow users to supply columns you don't want.
174+
172175
#### Multiple inserts in one query
173176
If you need to insert multiple rows at the same time it's also much faster to do it with a single `insert`. Simply pass an array of objects to `sql()`.
177+
174178
```js
175179

176180
const users = [{
@@ -190,9 +194,46 @@ sql`
190194

191195
```
192196

197+
#### Update
198+
199+
This is also useful for update queries
200+
```js
201+
202+
const user = {
203+
id: 1,
204+
name: 'Muray'
205+
}
206+
207+
sql`
208+
update users set ${
209+
sql(user, 'name')
210+
} where
211+
id = ${ user.id }
212+
`
213+
214+
// Is translated into this query:
215+
update users set name = $1 where id = $2
216+
```
217+
218+
#### Select
219+
220+
```js
221+
222+
const columns = ['name', 'age']
223+
224+
sql`
225+
select ${
226+
sql(columns)
227+
} from users
228+
`
229+
230+
// Is translated into this query:
231+
select name, age from users
232+
```
233+
193234
#### Arrays `sql.array(Array)`
194235

195-
Postgres has a native array type which is similar to js arrays, but Postgres only allows the same type and shape for nested items. This method automatically infers the item type and translates js arrays into Postgres arrays.
236+
PostgreSQL has a native array type which is similar to js arrays, but only allows the same type and shape for nested items. This method automatically infers the item type and serializes js arrays into PostgreSQL arrays.
196237

197238
```js
198239

0 commit comments

Comments
 (0)