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: deno/README.md
+46-8
Original file line number
Diff line number
Diff line change
@@ -75,6 +75,7 @@ async function insertUser({ name, age }) {
75
75
*[Teardown / Cleanup](#teardown--cleanup)
76
76
*[Error handling](#error-handling)
77
77
*[TypeScript support](#typescript-support)
78
+
*[Reserving connections](#reserving-connections)
78
79
*[Changelog](./CHANGELOG.md)
79
80
80
81
@@ -171,14 +172,23 @@ const user = {
171
172
age:68
172
173
}
173
174
174
-
sql`
175
+
awaitsql`
175
176
insert into users ${
176
177
sql(user, 'name', 'age')
177
178
}
178
179
`
179
180
180
181
// Which results in:
181
182
insert into users ("name", "age") values ($1, $2)
183
+
184
+
// The columns can also be given with an array
185
+
constcolumns= ['name', 'age']
186
+
187
+
awaitsql`
188
+
insert into users ${
189
+
sql(user, columns)
190
+
}
191
+
`
182
192
```
183
193
184
194
**You can omit column names and simply execute `sql(user)` to get all the fields from the object as columns**. Be careful not to allow users to supply columns that you do not want to be inserted.
@@ -218,7 +228,7 @@ const user = {
218
228
age:68
219
229
}
220
230
221
-
sql`
231
+
awaitsql`
222
232
update users set${
223
233
sql(user, 'name', 'age')
224
234
}
@@ -227,10 +237,20 @@ sql`
227
237
228
238
// Which results in:
229
239
update users set "name"= $1, "age"= $2 where user_id = $3
240
+
241
+
// The columns can also be given with an array
242
+
constcolumns= ['name', 'age']
243
+
244
+
awaitsql`
245
+
update users set${
246
+
sql(user, columns)
247
+
}
248
+
where user_id =${user.id}
249
+
`
230
250
```
231
251
232
252
### Multiple updates in one query
233
-
It's possible to create multiple udpates in a single query. It's necessary to use arrays intead of objects to ensure the order of the items so that these correspond with the column names.
253
+
To create multiple updates in a single query, it is necessary to use arrays instead of objects to ensure that the order of the items correspond with the column names.
Do note that you can often achieve the same result using [`WITH` queries (Common Table Expressions)](https://www.postgresql.org/docs/current/queries-with.html) instead of using transactions.
615
+
592
616
It's also possible to pipeline the requests in a transaction if needed by returning an array with queries from the callback function like this:
Indicates that the transactions should be prepared using the `PREPARED TRANASCTION [NAME]` statement
663
+
Indicates that the transactions should be prepared using the [`PREPARE TRANSACTION [NAME]`](https://www.postgresql.org/docs/current/sql-prepare-transaction.html) statement
Do note that you can often achieve the same result using [`WITH` queries (Common Table Expressions)](https://www.postgresql.org/docs/current/queries-with.html) instead of using transactions.
657
-
658
680
## Data Transformation
659
681
660
682
Postgres.js allows for transformation of the data passed to or returned from a query by using the `transform` option.
connect_timeout :30, // Connect timeout in seconds
938
960
prepare :true, // Automatic creation of prepared statements
939
961
types : [], // Array of custom types, see more below
940
-
onnotice : fn, //Defaults to console.log
962
+
onnotice : fn, //Default console.log, set false to silence NOTICE
941
963
onparameter : fn, // (key, value) when server param change
942
964
debug : fn, // Is called with (connection, query, params, types)
943
965
socket : fn, // fn returning custom socket to use
@@ -1147,6 +1169,22 @@ prexit(async () => {
1147
1169
})
1148
1170
```
1149
1171
1172
+
## Reserving connections
1173
+
1174
+
### `await sql.reserve()`
1175
+
1176
+
The `reserve` method pulls out a connection from the pool, and returns a client that wraps the single connection. This can be used for running queries on an isolated connection.
1177
+
1178
+
```ts
1179
+
const reserved =awaitsql.reserve()
1180
+
awaitreserved`select * from users`
1181
+
awaitreserved.release()
1182
+
```
1183
+
1184
+
### `reserved.release()`
1185
+
1186
+
Once you have finished with the reserved connection, call `release` to add it back to the pool.
1187
+
1150
1188
## Error handling
1151
1189
1152
1190
Errors are all thrown to related queries and never globally. Errors coming from database itself are always in the [native Postgres format](https://www.postgresql.org/docs/current/errcodes-appendix.html), and the same goes for any [Node.js errors](https://nodejs.org/api/errors.html#errors_common_system_errors) eg. coming from the underlying connection.
0 commit comments