@@ -3,110 +3,114 @@ import pg from 'pg'
3
3
import assert from 'assert'
4
4
5
5
const queryText = 'SELECT * FROM generate_series(0, 200) num'
6
- describe ( 'Async iterator' , ( ) => {
7
- it ( 'works' , async ( ) => {
8
- const stream = new QueryStream ( queryText , [ ] )
9
- const client = new pg . Client ( )
10
- await client . connect ( )
11
- const query = client . query ( stream )
12
- const rows = [ ]
13
- for await ( const row of query ) {
14
- rows . push ( row )
15
- }
16
- assert . equal ( rows . length , 201 )
17
- await client . end ( )
18
- } )
19
-
20
- it ( 'can async iterate and then do a query afterwards' , async ( ) => {
21
- const stream = new QueryStream ( queryText , [ ] )
22
- const client = new pg . Client ( )
23
- await client . connect ( )
24
- const query = client . query ( stream )
25
- const iteratorRows = [ ]
26
- for await ( const row of query ) {
27
- iteratorRows . push ( row )
28
- }
29
- assert . equal ( iteratorRows . length , 201 )
30
- const { rows } = await client . query ( 'SELECT NOW()' )
31
- assert . equal ( rows . length , 1 )
32
- await client . end ( )
33
- } )
34
6
35
- it ( 'can async iterate multiple times with a pool' , async ( ) => {
36
- const pool = new pg . Pool ( { max : 1 } )
7
+ // node v8 do not support async iteration
8
+ if ( ! process . version . startsWith ( 'v8' ) ) {
9
+ describe ( 'Async iterator' , ( ) => {
10
+ it ( 'works' , async ( ) => {
11
+ const stream = new QueryStream ( queryText , [ ] )
12
+ const client = new pg . Client ( )
13
+ await client . connect ( )
14
+ const query = client . query ( stream )
15
+ const rows = [ ]
16
+ for await ( const row of query ) {
17
+ rows . push ( row )
18
+ }
19
+ assert . equal ( rows . length , 201 )
20
+ await client . end ( )
21
+ } )
37
22
38
- const allRows = [ ]
39
- const run = async ( ) => {
40
- // get the client
41
- const client = await pool . connect ( )
42
- // stream some rows
23
+ it ( 'can async iterate and then do a query afterwards' , async ( ) => {
43
24
const stream = new QueryStream ( queryText , [ ] )
25
+ const client = new pg . Client ( )
26
+ await client . connect ( )
27
+ const query = client . query ( stream )
44
28
const iteratorRows = [ ]
45
- client . query ( stream )
46
- for await ( const row of stream ) {
29
+ for await ( const row of query ) {
47
30
iteratorRows . push ( row )
48
- allRows . push ( row )
49
31
}
50
32
assert . equal ( iteratorRows . length , 201 )
51
- client . release ( )
52
- }
53
- await Promise . all ( [ run ( ) , run ( ) , run ( ) ] )
54
- assert . equal ( allRows . length , 603 )
55
- await pool . end ( )
56
- } )
33
+ const { rows } = await client . query ( 'SELECT NOW()' )
34
+ assert . equal ( rows . length , 1 )
35
+ await client . end ( )
36
+ } )
57
37
58
- it ( 'can break out of iteration early' , async ( ) => {
59
- const pool = new pg . Pool ( { max : 1 } )
60
- const client = await pool . connect ( )
61
- const rows = [ ]
62
- for await ( const row of client . query ( new QueryStream ( queryText , [ ] , { batchSize : 1 } ) ) ) {
63
- rows . push ( row )
64
- break
65
- }
66
- for await ( const row of client . query ( new QueryStream ( queryText , [ ] ) ) ) {
67
- rows . push ( row )
68
- break
69
- }
70
- for await ( const row of client . query ( new QueryStream ( queryText , [ ] ) ) ) {
71
- rows . push ( row )
72
- break
73
- }
74
- assert . strictEqual ( rows . length , 3 )
75
- client . release ( )
76
- await pool . end ( )
77
- } )
38
+ it ( 'can async iterate multiple times with a pool' , async ( ) => {
39
+ const pool = new pg . Pool ( { max : 1 } )
78
40
79
- it ( 'only returns rows on first iteration' , async ( ) => {
80
- const pool = new pg . Pool ( { max : 1 } )
81
- const client = await pool . connect ( )
82
- const rows = [ ]
83
- const stream = client . query ( new QueryStream ( queryText , [ ] ) )
84
- for await ( const row of stream ) {
85
- rows . push ( row )
86
- break
87
- }
88
- for await ( const row of stream ) {
89
- rows . push ( row )
90
- }
91
- for await ( const row of stream ) {
92
- rows . push ( row )
93
- }
94
- assert . strictEqual ( rows . length , 1 )
95
- client . release ( )
96
- await pool . end ( )
97
- } )
41
+ const allRows = [ ]
42
+ const run = async ( ) => {
43
+ // get the client
44
+ const client = await pool . connect ( )
45
+ // stream some rows
46
+ const stream = new QueryStream ( queryText , [ ] )
47
+ const iteratorRows = [ ]
48
+ client . query ( stream )
49
+ for await ( const row of stream ) {
50
+ iteratorRows . push ( row )
51
+ allRows . push ( row )
52
+ }
53
+ assert . equal ( iteratorRows . length , 201 )
54
+ client . release ( )
55
+ }
56
+ await Promise . all ( [ run ( ) , run ( ) , run ( ) ] )
57
+ assert . equal ( allRows . length , 603 )
58
+ await pool . end ( )
59
+ } )
98
60
99
- it ( 'can read with delays' , async ( ) => {
100
- const pool = new pg . Pool ( { max : 1 } )
101
- const client = await pool . connect ( )
102
- const rows = [ ]
103
- const stream = client . query ( new QueryStream ( queryText , [ ] , { batchSize : 1 } ) )
104
- for await ( const row of stream ) {
105
- rows . push ( row )
106
- await new Promise ( ( resolve ) => setTimeout ( resolve , 1 ) )
107
- }
108
- assert . strictEqual ( rows . length , 201 )
109
- client . release ( )
110
- await pool . end ( )
61
+ it ( 'can break out of iteration early' , async ( ) => {
62
+ const pool = new pg . Pool ( { max : 1 } )
63
+ const client = await pool . connect ( )
64
+ const rows = [ ]
65
+ for await ( const row of client . query ( new QueryStream ( queryText , [ ] , { batchSize : 1 } ) ) ) {
66
+ rows . push ( row )
67
+ break
68
+ }
69
+ for await ( const row of client . query ( new QueryStream ( queryText , [ ] ) ) ) {
70
+ rows . push ( row )
71
+ break
72
+ }
73
+ for await ( const row of client . query ( new QueryStream ( queryText , [ ] ) ) ) {
74
+ rows . push ( row )
75
+ break
76
+ }
77
+ assert . strictEqual ( rows . length , 3 )
78
+ client . release ( )
79
+ await pool . end ( )
80
+ } )
81
+
82
+ it ( 'only returns rows on first iteration' , async ( ) => {
83
+ const pool = new pg . Pool ( { max : 1 } )
84
+ const client = await pool . connect ( )
85
+ const rows = [ ]
86
+ const stream = client . query ( new QueryStream ( queryText , [ ] ) )
87
+ for await ( const row of stream ) {
88
+ rows . push ( row )
89
+ break
90
+ }
91
+ for await ( const row of stream ) {
92
+ rows . push ( row )
93
+ }
94
+ for await ( const row of stream ) {
95
+ rows . push ( row )
96
+ }
97
+ assert . strictEqual ( rows . length , 1 )
98
+ client . release ( )
99
+ await pool . end ( )
100
+ } )
101
+
102
+ it ( 'can read with delays' , async ( ) => {
103
+ const pool = new pg . Pool ( { max : 1 } )
104
+ const client = await pool . connect ( )
105
+ const rows = [ ]
106
+ const stream = client . query ( new QueryStream ( queryText , [ ] , { batchSize : 1 } ) )
107
+ for await ( const row of stream ) {
108
+ rows . push ( row )
109
+ await new Promise ( ( resolve ) => setTimeout ( resolve , 1 ) )
110
+ }
111
+ assert . strictEqual ( rows . length , 201 )
112
+ client . release ( )
113
+ await pool . end ( )
114
+ } )
111
115
} )
112
- } )
116
+ }
0 commit comments