Skip to content

Commit 750b1d7

Browse files
authored
document transform option (porsager#309)
* document built in transform * add inserting in note too * quick tweaks * fix extra blank lines * fix code indent and format
1 parent 44e9fbe commit 750b1d7

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

README.md

+33
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ async function insertUser({ name, age }) {
6868
* [Building queries](#building-queries)
6969
* [Advanced query methods](#advanced-query-methods)
7070
* [Transactions](#transactions)
71+
* [Data Transformation](#data-transformation)
7172
* [Listen & notify](#listen--notify)
7273
* [Realtime subscribe](#realtime-subscribe)
7374
* [Numbers, bigint, numeric](#numbers-bigint-numeric)
@@ -517,6 +518,38 @@ sql.begin('read write', async sql => {
517518
518519
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.
519520
521+
## Data Transformation
522+
523+
`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.
524+
525+
Like - `postgres('connectionURL', { transformation: {...} })`
526+
527+
### Parameters
528+
* `to`: The function to transform the outgoing query column name to, i.e ``SELECT ${ sql('aName') }` to `SELECT a_name` when using `postgres.toCamel`.
529+
* `from`: The function to transform the incoming query result column name to, see example below.
530+
531+
> Both parameters are optional, if not provided, the default transformation function will be used.
532+
533+
Built in transformation functions are:
534+
* For camelCase - `postgres.toCamel` and `postgres.fromCamel`
535+
* For PascalCase - `postgres.toPascal` and `postgres.fromPascal`
536+
* For Kebab-Case - `postgres.toKebab` and `postgres.fromKebab`
537+
538+
These functions can be passed in as options when calling `postgres()`. For example -
539+
```js
540+
// this will tranform the column names to camel case back and forth
541+
(async function () {
542+
const sql = postgres('connectionURL', { transform: { column: { to: postgres.fromCamel, from: postgres.toCamel } }});
543+
await sql`CREATE TABLE IF NOT EXISTS camel_case (a_test INTEGER, b_test TEXT)`;
544+
await sql`INSERT INTO camel_case ${ sql([{ aTest: 1, bTest: 1 }]) }`
545+
const data = await sql`SELECT ${ sql('aTest', 'bTest') } FROM camel_case`;
546+
console.log(data) // [ { aTest: 1, bTest: '1' } ]
547+
process.exit(1)
548+
})();
549+
```
550+
551+
> 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.
552+
520553
## Listen & notify
521554
522555
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.

0 commit comments

Comments
 (0)