Skip to content

Commit 9cec5be

Browse files
Readme updates (porsager#223)
* Add warning about quotation marks with interpolation * Improve documentation for custom types Provide explanations for fields and hints as to where to locate the `oid` values.
1 parent a02de67 commit 9cec5be

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

README.md

+17-3
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ All the public API is typed. Also, TypeScript support is still in beta. Feel fre
187187

188188
Parameters are automatically inferred and handled by Postgres so that SQL injection isn't possible. No special handling is necessary, simply use JS tagged template literals as usual.
189189

190+
Be careful with quotation marks here. Because Postgres infers the types, you don't need to wrap your interpolated parameters in quotes like `'${name}'`. In fact, this will cause an error because the tagged template replaces `${name}` with `$1` in the query string, leaving Postgres to do the interpolation. If you wrap that in a string, Postgres will see `'$1'` and interpret it as a string as opposed to a parameter.
191+
190192
```js
191193

192194
let search = 'Mur'
@@ -531,20 +533,32 @@ sql.begin(async sql => {
531533

532534
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.
533535

534-
## Types
536+
## Custom Types
535537

536-
You can add ergonomic support for custom types, or simply pass an object with a `{ type, value }` signature that contains the Postgres `oid` for the type and the correctly serialized value.
538+
You can add ergonomic support for custom types, or simply pass an object with a `{ type, value }` signature that contains the Postgres `oid` for the type and the correctly serialized value. _(`oid` values for types can be found in the `pg_catalog.pg_types` table.)_
537539

538540
Adding Query helpers is the recommended approach which can be done like this:
539541

540542
```js
541543

542-
const sql = sql({
544+
const sql = postgres({
543545
types: {
544546
rect: {
547+
/**
548+
* The pg_types oid to pass to the db along with the serialized value.
549+
*/
545550
to : 1337,
551+
/**
552+
* An array of pg_types oids to handle when parsing values coming from the db.
553+
*/
546554
from : [1337],
555+
/**
556+
* Function that transform values before sending them to the db.
557+
*/
547558
serialize : ({ x, y, width, height }) => [x, y, width, height],
559+
/**
560+
* Function that transforms values coming from the db.
561+
*/
548562
parse : ([x, y, width, height]) => { x, y, width, height }
549563
}
550564
}

0 commit comments

Comments
 (0)