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 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.
Copy file name to clipboardExpand all lines: README.md
+17-3
Original file line number
Diff line number
Diff line change
@@ -187,6 +187,8 @@ All the public API is typed. Also, TypeScript support is still in beta. Feel fre
187
187
188
188
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.
189
189
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
+
190
192
```js
191
193
192
194
let search ='Mur'
@@ -531,20 +533,32 @@ sql.begin(async sql => {
531
533
532
534
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.
533
535
534
-
## Types
536
+
## Custom Types
535
537
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.)_
537
539
538
540
Adding Query helpers is the recommended approach which can be done like this:
539
541
540
542
```js
541
543
542
-
constsql=sql({
544
+
constsql=postgres({
543
545
types: {
544
546
rect: {
547
+
/**
548
+
* The pg_types oid to pass to the db along with the serialized value.
549
+
*/
545
550
to :1337,
551
+
/**
552
+
* An array of pg_types oids to handle when parsing values coming from the db.
553
+
*/
546
554
from : [1337],
555
+
/**
556
+
* Function that transform values before sending them to the db.
557
+
*/
547
558
serialize: ({ x, y, width, height }) => [x, y, width, height],
559
+
/**
560
+
* Function that transforms values coming from the db.
561
+
*/
548
562
parse: ([x, y, width, height]) => { x, y, width, height }
0 commit comments