Skip to content

Commit 2ea8204

Browse files
committed
Improve README.md
1 parent 9cec5be commit 2ea8204

File tree

1 file changed

+45
-49
lines changed

1 file changed

+45
-49
lines changed

README.md

+45-49
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,44 @@ const [new_user] = await sql`
143143
// new_user = { user_id: 1, name: 'Murray', age: 68 }
144144
```
145145

146+
#### Query parameters
147+
148+
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.
149+
150+
```js
151+
152+
let search = 'Mur'
153+
154+
const users = await sql`
155+
select
156+
name,
157+
age
158+
from users
159+
where
160+
name like ${ search + '%' }
161+
`
162+
163+
// users = [{ name: 'Murray', age: 68 }]
146164

147-
#### TypeScript support
165+
```
166+
167+
> 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.
168+
169+
#### Arrays
170+
Arrays will be handled by replacement parameters too, so `where in` queries are also simple.
171+
172+
```js
173+
174+
const users = await sql`
175+
select
176+
*
177+
from users
178+
where age in (${ [68, 75, 23] })
179+
`
180+
181+
```
182+
183+
### TypeScript support
148184

149185
`postgres` has TypeScript support. You can pass a row list type for your queries in this way:
150186
```ts
@@ -183,42 +219,6 @@ const [first, second] = await sql<[User?]>`SELECT * FROM users WHERE id = ${id}`
183219

184220
All the public API is typed. Also, TypeScript support is still in beta. Feel free to open an issue if you have trouble with types.
185221

186-
#### Query parameters
187-
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-
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-
192-
```js
193-
194-
let search = 'Mur'
195-
196-
const users = await sql`
197-
select
198-
name,
199-
age
200-
from users
201-
where
202-
name like ${ search + '%' }
203-
`
204-
205-
// users = [{ name: 'Murray', age: 68 }]
206-
207-
```
208-
209-
Arrays will be handled by replacement parameters too, so `where in` queries are also simple.
210-
211-
```js
212-
213-
const users = await sql`
214-
select
215-
*
216-
from users
217-
where age in (${ [68, 75, 23] })
218-
`
219-
220-
```
221-
222222
## Stream ```sql` `.stream(fn) -> Promise```
223223

224224
If you want to handle rows returned by a query one by one, you can use `.stream` which returns a promise that resolves once there are no more rows.
@@ -544,26 +544,22 @@ Adding Query helpers is the recommended approach which can be done like this:
544544
const sql = postgres({
545545
types: {
546546
rect: {
547-
/**
548-
* The pg_types oid to pass to the db along with the serialized value.
549-
*/
547+
// The pg_types oid to pass to the db along with the serialized value.
550548
to : 1337,
551-
/**
552-
* An array of pg_types oids to handle when parsing values coming from the db.
553-
*/
549+
550+
// An array of pg_types oids to handle when parsing values coming from the db.
554551
from : [1337],
555-
/**
556-
* Function that transform values before sending them to the db.
557-
*/
552+
553+
//Function that transform values before sending them to the db.
558554
serialize : ({ x, y, width, height }) => [x, y, width, height],
559-
/**
560-
* Function that transforms values coming from the db.
561-
*/
555+
556+
// Function that transforms values coming from the db.
562557
parse : ([x, y, width, height]) => { x, y, width, height }
563558
}
564559
}
565560
})
566561

562+
// Now you can use sql.types.rect() as specified above
567563
const [custom] = sql`
568564
insert into rectangles (
569565
name,

0 commit comments

Comments
 (0)