Skip to content

Commit 4241824

Browse files
authored
Format code example for transform options (#459)
* Format code example for transform options * Remove extra code, edit for flow and typos
1 parent 097d272 commit 4241824

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

README.md

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -590,17 +590,23 @@ Built in transformation functions are:
590590
* For PascalCase - `postgres.toPascal` and `postgres.fromPascal`
591591
* For Kebab-Case - `postgres.toKebab` and `postgres.fromKebab`
592592

593-
These functions can be passed in as options when calling `postgres()`. For example -
593+
These functions can be passed in as options when calling `postgres()`, for example:
594594
```js
595-
// this will tranform the column names to camel case back and forth
596-
(async function () {
597-
const sql = postgres('connectionURL', { transform: { column: { to: postgres.fromCamel, from: postgres.toCamel } }});
598-
await sql`CREATE TABLE IF NOT EXISTS camel_case (a_test INTEGER, b_test TEXT)`;
599-
await sql`INSERT INTO camel_case ${ sql([{ aTest: 1, bTest: 1 }]) }`
600-
const data = await sql`SELECT ${ sql('aTest', 'bTest') } FROM camel_case`;
601-
console.log(data) // [ { aTest: 1, bTest: '1' } ]
602-
process.exit(1)
603-
})();
595+
// Transform the column names to and from camel case
596+
const sql = postgres('connectionURL', {
597+
transform: {
598+
column: {
599+
to: postgres.fromCamel,
600+
from: postgres.toCamel,
601+
},
602+
},
603+
})
604+
605+
await sql`CREATE TABLE IF NOT EXISTS camel_case (a_test INTEGER, b_test TEXT)`
606+
await sql`INSERT INTO camel_case ${ sql([{ aTest: 1, bTest: 1 }]) }`
607+
const data = await sql`SELECT ${ sql('aTest', 'bTest') } FROM camel_case`
608+
609+
console.log(data) // [ { aTest: 1, bTest: '1' } ]
604610
```
605611

606612
> 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, Postgres.js does not rewrite anything inside the static parts of the tagged templates.

0 commit comments

Comments
 (0)