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
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
+
constusers=awaitsql`
155
+
select
156
+
name,
157
+
age
158
+
from users
159
+
where
160
+
name like${ search +'%'}
161
+
`
162
+
163
+
// users = [{ name: 'Murray', age: 68 }]
146
164
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
+
constusers=awaitsql`
175
+
select
176
+
*
177
+
from users
178
+
where age in (${ [68, 75, 23] })
179
+
`
180
+
181
+
```
182
+
183
+
### TypeScript support
148
184
149
185
`postgres` has TypeScript support. You can pass a row list type for your queries in this way:
150
186
```ts
@@ -183,42 +219,6 @@ const [first, second] = await sql<[User?]>`SELECT * FROM users WHERE id = ${id}`
183
219
184
220
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.
185
221
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
-
constusers=awaitsql`
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
-
constusers=awaitsql`
214
-
select
215
-
*
216
-
from users
217
-
where age in (${ [68, 75, 23] })
218
-
`
219
-
220
-
```
221
-
222
222
## Stream ```sql` `.stream(fn) -> Promise```
223
223
224
224
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:
544
544
constsql=postgres({
545
545
types: {
546
546
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.
550
548
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.
554
551
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.
558
554
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.
562
557
parse: ([x, y, width, height]) => { x, y, width, height }
563
558
}
564
559
}
565
560
})
566
561
562
+
// Now you can use sql.types.rect() as specified above
0 commit comments