You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+51-10
Original file line number
Diff line number
Diff line change
@@ -68,7 +68,7 @@ More info for the `ssl` option can be found in the [Node.js docs for tls connect
68
68
69
69
## Query ```sql` ` -> Promise```
70
70
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.
72
72
73
73
```js
74
74
@@ -108,7 +108,7 @@ const users = await sql`
108
108
109
109
## Stream ```sql` `.stream(fn) -> Promise```
110
110
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.
[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.
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.
147
152
148
153
#### Insert
149
154
150
-
151
155
```js
152
156
153
157
constuser= {
@@ -157,20 +161,20 @@ const user = {
157
161
158
162
sql`
159
163
insert into users ${
160
-
sql(user)
164
+
sql(user, 'name', 'age')
161
165
}
162
166
`
163
167
164
-
```
165
-
166
-
Is translated into a safe query like this:
167
-
168
-
```sql
168
+
// Is translated into this query:
169
169
insert into users (name, age) values ($1, $2)
170
+
170
171
```
171
172
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
+
172
175
#### Multiple inserts in one query
173
176
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
+
174
178
```js
175
179
176
180
constusers= [{
@@ -190,9 +194,46 @@ sql`
190
194
191
195
```
192
196
197
+
#### Update
198
+
199
+
This is also useful for update queries
200
+
```js
201
+
202
+
constuser= {
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
+
constcolumns= ['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
+
193
234
#### Arrays `sql.array(Array)`
194
235
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.
0 commit comments