Skip to content

Commit cb928de

Browse files
committed
Prettier pg-query-stream
1 parent 3002d5c commit cb928de

17 files changed

+343
-323
lines changed

.eslintrc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
"node_modules",
1111
"packages/pg",
1212
"packages/pg-protocol",
13-
"packages/pg-pool",
14-
"packages/pg-query-stream"
13+
"packages/pg-pool"
1514
],
1615
"parserOptions": {
1716
"ecmaVersion": 2017,

packages/pg-query-stream/index.js

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,45 @@
1-
const { Readable } = require('stream')
2-
const Cursor = require('pg-cursor')
1+
const { Readable } = require('stream');
2+
const Cursor = require('pg-cursor');
33

44
class PgQueryStream extends Readable {
55
constructor(text, values, config = {}) {
66
const { batchSize, highWaterMark = 100 } = config;
77
// https://nodejs.org/api/stream.html#stream_new_stream_readable_options
8-
super({ objectMode: true, emitClose: true, autoDestroy: true, highWaterMark: batchSize || highWaterMark })
9-
this.cursor = new Cursor(text, values, config)
8+
super({ objectMode: true, emitClose: true, autoDestroy: true, highWaterMark: batchSize || highWaterMark });
9+
this.cursor = new Cursor(text, values, config);
1010

1111
// delegate Submittable callbacks to cursor
12-
this.handleRowDescription = this.cursor.handleRowDescription.bind(this.cursor)
13-
this.handleDataRow = this.cursor.handleDataRow.bind(this.cursor)
14-
this.handlePortalSuspended = this.cursor.handlePortalSuspended.bind(this.cursor)
15-
this.handleCommandComplete = this.cursor.handleCommandComplete.bind(this.cursor)
16-
this.handleReadyForQuery = this.cursor.handleReadyForQuery.bind(this.cursor)
17-
this.handleError = this.cursor.handleError.bind(this.cursor)
18-
this.handleEmptyQuery = this.cursor.handleEmptyQuery.bind(this.cursor)
12+
this.handleRowDescription = this.cursor.handleRowDescription.bind(this.cursor);
13+
this.handleDataRow = this.cursor.handleDataRow.bind(this.cursor);
14+
this.handlePortalSuspended = this.cursor.handlePortalSuspended.bind(this.cursor);
15+
this.handleCommandComplete = this.cursor.handleCommandComplete.bind(this.cursor);
16+
this.handleReadyForQuery = this.cursor.handleReadyForQuery.bind(this.cursor);
17+
this.handleError = this.cursor.handleError.bind(this.cursor);
18+
this.handleEmptyQuery = this.cursor.handleEmptyQuery.bind(this.cursor);
1919
}
2020

2121
submit(connection) {
22-
this.cursor.submit(connection)
22+
this.cursor.submit(connection);
2323
}
2424

2525
_destroy(_err, cb) {
2626
this.cursor.close((err) => {
27-
cb(err || _err)
28-
})
27+
cb(err || _err);
28+
});
2929
}
3030

3131
// https://nodejs.org/api/stream.html#stream_readable_read_size_1
3232
_read(size) {
3333
this.cursor.read(size, (err, rows, result) => {
3434
if (err) {
3535
// https://nodejs.org/api/stream.html#stream_errors_while_reading
36-
this.destroy(err)
36+
this.destroy(err);
3737
} else {
38-
for (const row of rows) this.push(row)
39-
if (rows.length < size) this.push(null)
38+
for (const row of rows) this.push(row);
39+
if (rows.length < size) this.push(null);
4040
}
41-
})
41+
});
4242
}
4343
}
4444

45-
module.exports = PgQueryStream
45+
module.exports = PgQueryStream;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
// only newer versions of node support async iterator
22
if (!process.version.startsWith('v8')) {
3-
require('./async-iterator.es6')
3+
require('./async-iterator.es6');
44
}
Lines changed: 82 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,91 @@
1-
var assert = require('assert')
2-
var concat = require('concat-stream')
1+
var assert = require('assert');
2+
var concat = require('concat-stream');
33

4-
var QueryStream = require('../')
5-
var helper = require('./helper')
4+
var QueryStream = require('../');
5+
var helper = require('./helper');
66

77
if (process.version.startsWith('v8.')) {
8-
return console.error('warning! node versions less than 10lts no longer supported & stream closing semantics may not behave properly');
9-
}
8+
console.error('warning! node less than 10lts stream closing semantics may not behave properly');
9+
} else {
10+
helper('close', function (client) {
11+
it('emits close', function (done) {
12+
var stream = new QueryStream('SELECT * FROM generate_series(0, $1) num', [3], { batchSize: 2, highWaterMark: 2 });
13+
var query = client.query(stream);
14+
query.pipe(concat(function () {}));
15+
query.on('close', done);
16+
});
17+
});
1018

11-
helper('close', function (client) {
12-
it('emits close', function (done) {
13-
var stream = new QueryStream('SELECT * FROM generate_series(0, $1) num', [3], { batchSize: 2, highWaterMark: 2 })
14-
var query = client.query(stream)
15-
query.pipe(concat(function () { }))
16-
query.on('close', done)
17-
})
18-
})
19+
helper('early close', function (client) {
20+
it('can be closed early', function (done) {
21+
var stream = new QueryStream('SELECT * FROM generate_series(0, $1) num', [20000], {
22+
batchSize: 2,
23+
highWaterMark: 2,
24+
});
25+
var query = client.query(stream);
26+
var readCount = 0;
27+
query.on('readable', function () {
28+
readCount++;
29+
query.read();
30+
});
31+
query.once('readable', function () {
32+
query.destroy();
33+
});
34+
query.on('close', function () {
35+
assert(readCount < 10, 'should not have read more than 10 rows');
36+
done();
37+
});
38+
});
1939

20-
helper('early close', function (client) {
21-
it('can be closed early', function (done) {
22-
var stream = new QueryStream('SELECT * FROM generate_series(0, $1) num', [20000], { batchSize: 2, highWaterMark: 2 })
23-
var query = client.query(stream)
24-
var readCount = 0
25-
query.on('readable', function () {
26-
readCount++
27-
query.read()
28-
})
29-
query.once('readable', function () {
30-
query.destroy()
31-
})
32-
query.on('close', function () {
33-
assert(readCount < 10, 'should not have read more than 10 rows')
34-
done()
35-
})
36-
})
40+
it('can destroy stream while reading', function (done) {
41+
var stream = new QueryStream('SELECT * FROM generate_series(0, 100), pg_sleep(1)');
42+
client.query(stream);
43+
stream.on('data', () => done(new Error('stream should not have returned rows')));
44+
setTimeout(() => {
45+
stream.destroy();
46+
stream.on('close', done);
47+
}, 100);
48+
});
3749

38-
it('can destroy stream while reading', function (done) {
39-
var stream = new QueryStream('SELECT * FROM generate_series(0, 100), pg_sleep(1)')
40-
client.query(stream)
41-
stream.on('data', () => done(new Error('stream should not have returned rows')))
42-
setTimeout(() => {
43-
stream.destroy()
44-
stream.on('close', done)
45-
}, 100)
46-
})
50+
it('emits an error when calling destroy with an error', function (done) {
51+
var stream = new QueryStream('SELECT * FROM generate_series(0, 100), pg_sleep(1)');
52+
client.query(stream);
53+
stream.on('data', () => done(new Error('stream should not have returned rows')));
54+
setTimeout(() => {
55+
stream.destroy(new Error('intentional error'));
56+
stream.on('error', (err) => {
57+
// make sure there's an error
58+
assert(err);
59+
assert.strictEqual(err.message, 'intentional error');
60+
done();
61+
});
62+
}, 100);
63+
});
4764

48-
it('emits an error when calling destroy with an error', function (done) {
49-
var stream = new QueryStream('SELECT * FROM generate_series(0, 100), pg_sleep(1)')
50-
client.query(stream)
51-
stream.on('data', () => done(new Error('stream should not have returned rows')))
52-
setTimeout(() => {
53-
stream.destroy(new Error('intentional error'))
54-
stream.on('error', (err) => {
55-
// make sure there's an error
56-
assert(err);
57-
assert.strictEqual(err.message, 'intentional error');
58-
done();
59-
})
60-
}, 100)
61-
})
65+
it('can destroy stream while reading an error', function (done) {
66+
var stream = new QueryStream('SELECT * from pg_sleep(1), basdfasdf;');
67+
client.query(stream);
68+
stream.on('data', () => done(new Error('stream should not have returned rows')));
69+
stream.once('error', () => {
70+
stream.destroy();
71+
// wait a bit to let any other errors shake through
72+
setTimeout(done, 100);
73+
});
74+
});
6275

63-
it('can destroy stream while reading an error', function (done) {
64-
var stream = new QueryStream('SELECT * from pg_sleep(1), basdfasdf;')
65-
client.query(stream)
66-
stream.on('data', () => done(new Error('stream should not have returned rows')))
67-
stream.once('error', () => {
68-
stream.destroy()
69-
// wait a bit to let any other errors shake through
70-
setTimeout(done, 100)
71-
})
72-
})
76+
it('does not crash when destroying the stream immediately after calling read', function (done) {
77+
var stream = new QueryStream('SELECT * from generate_series(0, 100), pg_sleep(1);');
78+
client.query(stream);
79+
stream.on('data', () => done(new Error('stream should not have returned rows')));
80+
stream.destroy();
81+
stream.on('close', done);
82+
});
7383

74-
it('does not crash when destroying the stream immediately after calling read', function (done) {
75-
var stream = new QueryStream('SELECT * from generate_series(0, 100), pg_sleep(1);')
76-
client.query(stream)
77-
stream.on('data', () => done(new Error('stream should not have returned rows')))
78-
stream.destroy()
79-
stream.on('close', done)
80-
})
81-
82-
it('does not crash when destroying the stream before its submitted', function (done) {
83-
var stream = new QueryStream('SELECT * from generate_series(0, 100), pg_sleep(1);')
84-
stream.on('data', () => done(new Error('stream should not have returned rows')))
85-
stream.destroy()
86-
stream.on('close', done)
87-
})
88-
})
84+
it('does not crash when destroying the stream before its submitted', function (done) {
85+
var stream = new QueryStream('SELECT * from generate_series(0, 100), pg_sleep(1);');
86+
stream.on('data', () => done(new Error('stream should not have returned rows')));
87+
stream.destroy();
88+
stream.on('close', done);
89+
});
90+
});
91+
}
Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
1-
var assert = require('assert')
2-
var concat = require('concat-stream')
3-
var through = require('through')
4-
var helper = require('./helper')
1+
var assert = require('assert');
2+
var concat = require('concat-stream');
3+
var through = require('through');
4+
var helper = require('./helper');
55

6-
var QueryStream = require('../')
6+
var QueryStream = require('../');
77

88
helper('concat', function (client) {
99
it('concats correctly', function (done) {
10-
var stream = new QueryStream('SELECT * FROM generate_series(0, 200) num', [])
11-
var query = client.query(stream)
12-
query.pipe(through(function (row) {
13-
this.push(row.num)
14-
})).pipe(concat(function (result) {
15-
var total = result.reduce(function (prev, cur) {
16-
return prev + cur
17-
})
18-
assert.equal(total, 20100)
19-
}))
20-
stream.on('end', done)
21-
})
22-
})
10+
var stream = new QueryStream('SELECT * FROM generate_series(0, 200) num', []);
11+
var query = client.query(stream);
12+
query
13+
.pipe(
14+
through(function (row) {
15+
this.push(row.num);
16+
})
17+
)
18+
.pipe(
19+
concat(function (result) {
20+
var total = result.reduce(function (prev, cur) {
21+
return prev + cur;
22+
});
23+
assert.equal(total, 20100);
24+
})
25+
);
26+
stream.on('end', done);
27+
});
28+
});
Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
var assert = require('assert')
2-
var QueryStream = require('../')
1+
var assert = require('assert');
2+
var QueryStream = require('../');
33

44
describe('stream config options', () => {
55
// this is mostly for backwards compatability.
66
it('sets readable.highWaterMark based on batch size', () => {
77
var stream = new QueryStream('SELECT NOW()', [], {
8-
batchSize: 88
9-
})
10-
assert.equal(stream._readableState.highWaterMark, 88)
11-
})
8+
batchSize: 88,
9+
});
10+
assert.equal(stream._readableState.highWaterMark, 88);
11+
});
1212

1313
it('sets readable.highWaterMark based on highWaterMark config', () => {
1414
var stream = new QueryStream('SELECT NOW()', [], {
15-
highWaterMark: 88
16-
})
15+
highWaterMark: 88,
16+
});
1717

18-
assert.equal(stream._readableState.highWaterMark, 88)
19-
})
18+
assert.equal(stream._readableState.highWaterMark, 88);
19+
});
2020

2121
it('defaults to 100 for highWaterMark', () => {
22-
var stream = new QueryStream('SELECT NOW()', [])
22+
var stream = new QueryStream('SELECT NOW()', []);
2323

24-
assert.equal(stream._readableState.highWaterMark, 100)
25-
})
26-
})
24+
assert.equal(stream._readableState.highWaterMark, 100);
25+
});
26+
});
Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
1-
const assert = require('assert')
2-
const helper = require('./helper')
3-
const QueryStream = require('../')
1+
const assert = require('assert');
2+
const helper = require('./helper');
3+
const QueryStream = require('../');
44

55
helper('empty-query', function (client) {
6-
it('handles empty query', function(done) {
7-
const stream = new QueryStream('-- this is a comment', [])
8-
const query = client.query(stream)
9-
query.on('end', function () {
10-
// nothing should happen for empty query
11-
done();
12-
}).on('data', function () {
13-
// noop to kick off reading
14-
})
15-
})
6+
it('handles empty query', function (done) {
7+
const stream = new QueryStream('-- this is a comment', []);
8+
const query = client.query(stream);
9+
query
10+
.on('end', function () {
11+
// nothing should happen for empty query
12+
done();
13+
})
14+
.on('data', function () {
15+
// noop to kick off reading
16+
});
17+
});
1618

1719
it('continues to function after stream', function (done) {
18-
client.query('SELECT NOW()', done)
19-
})
20-
})
20+
client.query('SELECT NOW()', done);
21+
});
22+
});

0 commit comments

Comments
 (0)