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
* Add first version of nested transforms
* Fix access before initialization
* Add trailing EOL
* Add higher-order function to reduce repetition
* Fix data structures
* Fix data structures
* Format config
* Add first tests for new transform options
* Pass column
* Update documentation
* Update types
* Document undefined transform option
Copy file name to clipboardExpand all lines: README.md
+99-21Lines changed: 99 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -575,32 +575,19 @@ Do note that you can often achieve the same result using [`WITH` queries (Common
575
575
576
576
## Data Transformation
577
577
578
-
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 `transform` option in the `postgres()` function connection options.
578
+
Postgres.js allows for transformation of the data passed to or returned from a query by using the `transform` option.
579
579
580
-
Like - `postgres('connectionURL', { transform: {...} })`
581
-
582
-
### Parameters
583
-
*`to`: The function to transform the outgoing query column name to, i.e `SELECT ${ sql('aName') }` to `SELECT a_name` when using `postgres.toCamel`.
584
-
*`from`: The function to transform the incoming query result column name to, see example below.
580
+
Built in transformation functions are:
585
581
586
-
> Both parameters are optional, if not provided, the default transformation function will be used.
582
+
* For camelCase - `postgres.camel`, `postgres.toCamel`, `postgres.fromCamel`
583
+
* For PascalCase - `postgres.pascal`, `postgres.toPascal`, `postgres.fromPascal`
584
+
* For Kebab-Case - `postgres.kebab`, `postgres.toKebab`, `postgres.fromKebab`
587
585
588
-
Built in transformation functions are:
589
-
* For camelCase - `postgres.toCamel` and `postgres.fromCamel`
590
-
* For PascalCase - `postgres.toPascal` and `postgres.fromPascal`
591
-
* For Kebab-Case - `postgres.toKebab` and `postgres.fromKebab`
586
+
By default, using `postgres.camel`, `postgres.pascal` and `postgres.kebab` will perform a two-way transformation - both the data passed to the query and the data returned by the query will be transformed:
592
587
593
-
These functions can be passed in as options when calling `postgres()`, for example:
594
588
```js
595
589
// Transform the column names to and from camel case
596
-
constsql=postgres('connectionURL', {
597
-
transform: {
598
-
column: {
599
-
to:postgres.fromCamel,
600
-
from:postgres.toCamel,
601
-
},
602
-
},
603
-
})
590
+
constsql=postgres({ transform:postgres.camel })
604
591
605
592
awaitsql`CREATE TABLE IF NOT EXISTS camel_case (a_test INTEGER, b_test TEXT)`
606
593
awaitsql`INSERT INTO camel_case ${sql([{ aTest:1, bTest:1 }]) }`
@@ -609,7 +596,98 @@ const data = await sql`SELECT ${ sql('aTest', 'bTest') } FROM camel_case`
609
596
console.log(data) // [ { aTest: 1, bTest: '1' } ]
610
597
```
611
598
612
-
> 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.
599
+
To only perform half of the transformation (eg. only the transformation **to** or **from** camel case), use the other transformation functions:
600
+
601
+
```js
602
+
// Transform the column names only to camel case
603
+
// (for the results that are returned from the query)
604
+
postgres({ transform:postgres.toCamel })
605
+
606
+
awaitsql`CREATE TABLE IF NOT EXISTS camel_case (a_test INTEGER)`
607
+
awaitsql`INSERT INTO camel_case ${sql([{ a_test:1 }]) }`
608
+
constdata=awaitsql`SELECT a_test FROM camel_case`
609
+
610
+
console.log(data) // [ { aTest: 1 } ]
611
+
```
612
+
613
+
```js
614
+
// Transform the column names only from camel case
615
+
// (for interpolated inserts, updates, and selects)
> Note that Postgres.js does not rewrite the static parts of the tagged template strings. So to transform column names in your queries, the `sql()` helper must be used - eg. `${ sql('columnName') }` as in the examples above.
626
+
627
+
### Transform `undefined` Values
628
+
629
+
By default, Postgres.js will throw the error `UNDEFINED_VALUE: Undefined values are not allowed` when undefined values are passed
630
+
631
+
```js
632
+
// Transform the column names to and from camel case
633
+
constsql=postgres({
634
+
transform: {
635
+
undefined:null
636
+
}
637
+
})
638
+
639
+
awaitsql`CREATE TABLE IF NOT EXISTS transform_undefined (a_test INTEGER)`
640
+
awaitsql`INSERT INTO transform_undefined ${sql([{ a_test:undefined }]) }`
641
+
constdata=awaitsql`SELECT a_test FROM transform_undefined`
642
+
643
+
console.log(data) // [ { a_test: null } ]
644
+
```
645
+
646
+
To combine with the built in transform functions, spread the transform in the `transform` object:
647
+
648
+
```js
649
+
// Transform the column names to and from camel case
650
+
constsql=postgres({
651
+
transform: {
652
+
...postgres.camel,
653
+
undefined:null
654
+
}
655
+
})
656
+
657
+
awaitsql`CREATE TABLE IF NOT EXISTS transform_undefined (a_test INTEGER)`
658
+
awaitsql`INSERT INTO transform_undefined ${sql([{ aTest:undefined }]) }`
To specify your own transformation functions, you can use the `column`, `value` and `row` options inside of `transform`, each an object possibly including `to` and `from` keys:
667
+
668
+
*`to`: The function to transform the outgoing query column name to, i.e `SELECT ${ sql('aName') }` to `SELECT a_name` when using `postgres.toCamel`.
669
+
*`from`: The function to transform the incoming query result column name to, see example below.
670
+
671
+
> Both parameters are optional, if not provided, the default transformation function will be used.
672
+
673
+
```js
674
+
// Implement your own functions, look at postgres.toCamel, etc
0 commit comments