Skip to content

Commit 4265251

Browse files
authored
Update README.md (prepared transactions) (porsager#637)
* Update README.md (prepared statements) - correct typo - add link to the official docs - change the subsection name to "PREPARE TRANSACTION" instead of "PREPARE" (because "PREPARE" is more associated with "prepared statements") One thing that is still a bit confusing in this section is the final sentence "Do note that you can often achieve...". It seems like it is referring to the "PREPARE" subsection, but in reality it is referring to the initial "BEGIN / COMMIT" subsection, no? * Update README.md - moved "Do note that you can often achieve the same result" to the first subsection - added a link to a question in dba.stackexchange.com that shows how to do it - in the insert and update example with dynamic columns, clarify that the columns can be given as an array * Update README.md - remove example in stackoverflow
1 parent 544f58b commit 4265251

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

README.md

+25-6
Original file line numberDiff line numberDiff line change
@@ -176,14 +176,23 @@ const user = {
176176
age: 68
177177
}
178178

179-
sql`
179+
await sql`
180180
insert into users ${
181181
sql(user, 'name', 'age')
182182
}
183183
`
184184

185185
// Which results in:
186186
insert into users ("name", "age") values ($1, $2)
187+
188+
// The columns can also be given with an array
189+
const columns = ['name', 'age']
190+
191+
await sql`
192+
insert into users ${
193+
sql(user, columns)
194+
}
195+
`
187196
```
188197

189198
**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.
@@ -223,7 +232,7 @@ const user = {
223232
age: 68
224233
}
225234

226-
sql`
235+
await sql`
227236
update users set ${
228237
sql(user, 'name', 'age')
229238
}
@@ -232,6 +241,16 @@ sql`
232241

233242
// Which results in:
234243
update users set "name" = $1, "age" = $2 where user_id = $3
244+
245+
// The columns can also be given with an array
246+
const columns = ['name', 'age']
247+
248+
await sql`
249+
update users set ${
250+
sql(user, columns)
251+
}
252+
where user_id = ${ user.id }
253+
`
235254
```
236255

237256
### Multiple updates in one query
@@ -596,6 +615,8 @@ const [user, account] = await sql.begin(async sql => {
596615
})
597616
```
598617

618+
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.
619+
599620
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:
600621

601622
```js
@@ -641,9 +662,9 @@ sql.begin('read write', async sql => {
641662
```
642663

643664

644-
#### PREPARE `await sql.prepare([name]) -> fn()`
665+
#### PREPARE TRANSACTION `await sql.prepare([name]) -> fn()`
645666

646-
Indicates that the transactions should be prepared using the `PREPARED TRANASCTION [NAME]` statement
667+
Indicates that the transactions should be prepared using the [`PREPARE TRANSACTION [NAME]`](https://www.postgresql.org/docs/current/sql-prepare-transaction.html) statement
647668
instead of being committed.
648669

649670
```js
@@ -660,8 +681,6 @@ sql.begin('read write', async sql => {
660681
})
661682
```
662683

663-
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.
664-
665684
## Data Transformation
666685

667686
Postgres.js allows for transformation of the data passed to or returned from a query by using the `transform` option.

0 commit comments

Comments
 (0)