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
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.
515
516
517
+
## Data Transformation
518
+
519
+
`postgres.js` comes with a number of built-in data transformation functions that can be used to transform the data returned from a query or when inserting data. They are available under `transformation` option in the `postgres()` function connection options.
520
+
521
+
Like - `postgres('connectionURL', { transformation: {...} })`
522
+
523
+
### Parameters
524
+
* `to`: The function to transform the outgoing query column name to, i.e ``SELECT ${sql('aName') }` to `SELECT a_name` when using `postgres.toCamel`.
525
+
*`from`: The function to transform the incoming query result column name to, see example below.
526
+
527
+
> Both parameters are optional, if not provided, the default transformation function will be used.
528
+
529
+
Built in transformation functions are:
530
+
* For camelCase - `postgres.toCamel` and `postgres.fromCamel`
531
+
* For PascalCase - `postgres.toPascal` and `postgres.fromPascal`
532
+
* For Kebab-Case - `postgres.toKebab` and `postgres.fromKebab`
533
+
534
+
These functions can be passed in as options when calling `postgres()`. For example -
535
+
```js
536
+
// this will tranform the column names to camel case back and forth
> Note that if a column name is originally registered as snake_case in the database then to tranform it from camelCase to snake_case when querying or inserting, the column camelCase name must be put in `sql('columnName')` as it's done in the above example.
548
+
516
549
## Listen & notify
517
550
518
551
When you call `.listen`, a dedicated connection will be created to ensure that you receive notifications instantly. This connection will be used for any further calls to `.listen`. The connection will automatically reconnect according to a backoff reconnection pattern to not overload the database server.
@@ -682,6 +715,23 @@ Connections are created lazily once a query is created. This means that simply d
682
715
683
716
> No connection will be made until a query is made.
684
717
718
+
For example:
719
+
720
+
```js
721
+
constsql=postgres() // no connections are opened
722
+
723
+
awaitsql`...`// one connection is now opened
724
+
awaitsql`...`// previous opened connection is reused
725
+
726
+
// two connections are opened now
727
+
awaitPromise.all([
728
+
sql`...`,
729
+
sql`...`
730
+
])
731
+
```
732
+
733
+
> When there are high amount of concurrent queries, `postgres` will open as many connections as needed up until `max` number of connections is reached. By default `max` is 10. This can be changed by setting `max` in the `postgres()` call. Example - `postgres('connectionURL', { max:20 })`.
734
+
685
735
This means that we get a much simpler story for error handling and reconnections. Queries will be sent over the wire immediately on the next available connection in the pool. Connections are automatically taken out of the pool if you start a transaction using `sql.begin()`, and automatically returned to the pool once your transaction is done.
686
736
687
737
Any query which was already sent over the wire will be rejected if the connection is lost. It'll automatically defer to the error handling you have for that query, and since connections are lazy it'll automatically try to reconnect the next time a query is made. The benefit of this is no weird generic "onerror" handler that tries to get things back to normal, and also simpler application code since you don't have to handle errors out of context.
0 commit comments