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
Use cursors if you need to throttle the amount of rows being returned from a query. New results won't be requested until the promise / async callack function has resolved.
127
+
128
+
```js
129
+
130
+
awaitsql.cursor`
131
+
select * from generate_series(1,4) as x
132
+
`.cursor(row=> {
133
+
// row = { x: 1 }
134
+
http.request('https://example.com/wat', { row })
135
+
})
136
+
137
+
// No more rows
138
+
139
+
```
140
+
141
+
A single row will be returned by default, but you can also request batches by setting the number of rows desired in each batch as the first argument. That is usefull if you can do work with the rows in parallel like in this example:
142
+
143
+
```js
144
+
145
+
awaitsql.cursor`
146
+
select * from generate_series(1,1000) as x
147
+
`.cursor(10, rows=> {
148
+
// rows = [{ x: 1 }, { x: 2 }, ... ]
149
+
awaitPromise.all(rows.map(row=>
150
+
http.request('https://example.com/wat', { row })
151
+
))
152
+
})
153
+
154
+
```
155
+
156
+
If an error is thrown inside the callback function no more rows will be requested and the promise will reject with the thrown error.
157
+
158
+
You can also stop receiving any more rows early by returning an end token `sql.END` from the callback function.
159
+
160
+
```js
161
+
162
+
awaitsql.cursor`
163
+
select * from generate_series(1,1000) as x
164
+
`.cursor(row=> {
165
+
returnMath.random() >0.9&&sql.END
166
+
})
167
+
168
+
```
169
+
124
170
## Listen and notify
125
171
126
172
When you call listen, a dedicated connection will automatically be made to ensure that you receive notifications in real time. This connection will be used for any further calls to listen. Listen returns a promise which resolves once the `LISTEN` query to Postgres completes, or if there is already a listener active.
0 commit comments