Skip to content

Commit 68e063e

Browse files
committedDec 27, 2019
Merge origin/master
2 parents 766e48f + 47af4e8 commit 68e063e

31 files changed

+2440
-17
lines changed
 

Diff for: ‎.devcontainer/Dockerfile

+12
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ RUN apt-get update \
3232
&& curl -sS https://dl.yarnpkg.com/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/pubkey.gpg | apt-key add - 2>/dev/null \
3333
&& echo "deb https://dl.yarnpkg.com/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/ stable main" | tee /etc/apt/sources.list.d/yarn.list \
3434
&& apt-get update \
35+
<<<<<<< HEAD
3536
&& apt-get -y install --no-install-recommends yarn tmux locales postgresql \
37+
=======
38+
&& apt-get -y install --no-install-recommends yarn tmux locales \
39+
>>>>>>> origin/master
3640
#
3741
# Install eslint globally
3842
&& npm install -g eslint \
@@ -58,6 +62,14 @@ RUN curl https://raw.githubusercontent.com/brianc/dotfiles/master/.tmux.conf > ~
5862
# install nvm
5963
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.2/install.sh | bash
6064

65+
<<<<<<< HEAD
66+
=======
67+
# set up a nicer prompt
68+
RUN git clone https://github.com/magicmonty/bash-git-prompt.git ~/.bash-git-prompt --depth=1
69+
70+
RUN echo "source $HOME/.bash-git-prompt/gitprompt.sh" >> ~/.bashrc
71+
72+
>>>>>>> origin/master
6173
# Set the locale
6274
RUN sed -i -e 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen && locale-gen
6375
ENV LANG en_US.UTF-8

Diff for: ‎.github/FUNDING.yml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# These are supported funding model platforms
2+
3+
github: [brianc]

Diff for: ‎README.md

+18-11
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,22 @@
77

88
Non-blocking PostgreSQL client for Node.js. Pure JavaScript and optional native libpq bindings.
99

10-
## Install
10+
## Monorepo
1111

12-
```sh
13-
$ npm install pg
14-
```
12+
This repo is a monorepo which contains the core [pg](https://github.com/brianc/node-postgres/tree/master/packages/pg) module as well as a handful of related modules.
1513

16-
---
17-
## :star: [Documentation](https://node-postgres.com) :star:
14+
- [pg](https://github.com/brianc/node-postgres/tree/master/packages/pg)
15+
- [pg-cursor](https://github.com/brianc/node-postgres/tree/master/packages/pg-cursor)
16+
- [pg-query-stream](https://github.com/brianc/node-postgres/tree/master/packages/pg-query-stream)
1817

18+
_(more to come, I'm in the process of migrating repos over here)_
19+
20+
21+
## Documenation
22+
23+
Each package in this repo should have it's own readme more focused on how to develop/contribute. For overall documentation on the project and the related modules managed by this repo please see:
24+
25+
### :star: [Documentation](https://node-postgres.com) :star:
1926

2027
### Features
2128

@@ -44,13 +51,11 @@ When you open an issue please provide:
4451

4552
You can also follow me [@briancarlson](https://twitter.com/briancarlson) if that's your thing. I try to always announce noteworthy changes & developments with node-postgres on Twitter.
4653

47-
### Professional Support
48-
49-
I offer professional support for node-postgres. I provide implementation, training, and many years of expertise on how to build applications with Node, Express, PostgreSQL, and React/Redux. Please contact me at [brian.m.carlson@gmail.com](mailto:brian.m.carlson@gmail.com) to discuss how I can help your company be more successful!
50-
5154
### Sponsorship :star:
5255

53-
If you are benefiting from node-postgres and would like to help keep the project financially sustainable please visit Brian Carlson's [Patreon page](https://www.patreon.com/node_postgres).
56+
[If you or your company are benefiting from node-postgres and would like to help keep the project financially sustainable please consider supporting](https://github.com/sponsors/brianc) its development.
57+
58+
Also, you can view a historical list of all [previous and existing sponsors](https://github.com/brianc/node-postgres/blob/master/SPONSORS.md).
5459

5560
## Contributing
5661

@@ -61,6 +66,8 @@ I will __happily__ accept your pull request if it:
6166
- looks reasonable
6267
- does not break backwards compatibility
6368

69+
If your change involves breaking backwards compatibility please please point that out in the pull request & we can discuss & plan when and how to release it and what type of documentation or communicate it will require.
70+
6471
## Troubleshooting and FAQ
6572

6673
The causes and solutions to common errors can be found among the [Frequently Asked Questions (FAQ)](https://github.com/brianc/node-postgres/wiki/FAQ)

Diff for: ‎lerna.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@
44
],
55
"npmClient": "yarn",
66
"useWorkspaces": true,
7-
"version": "independent"
7+
"version": "independent",
8+
"ignoreChanges": [
9+
"**/*.md"
10+
]
811
}

Diff for: ‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"packages/*"
1111
],
1212
"scripts": {
13-
"test": "yarn lerna exec --parallel yarn test",
13+
"test": "yarn lerna exec yarn test",
1414
"lint": "yarn lerna exec --parallel yarn lint"
1515
},
1616
"devDependencies": {

Diff for: ‎packages/pg-cursor/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "pg-cursor",
3-
"version": "2.0.2",
3+
"version": "2.0.3",
44
"description": "Query cursor extension for node-postgres",
55
"main": "index.js",
66
"directories": {
@@ -21,7 +21,7 @@
2121
"eslint-config-prettier": "^6.4.0",
2222
"eslint-plugin-prettier": "^3.1.1",
2323
"mocha": "^6.2.2",
24-
"pg": "^7.15.1",
24+
"pg": "^7.15.2",
2525
"prettier": "^1.18.2"
2626
},
2727
"prettier": {

Diff for: ‎packages/pg-query-stream/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

Diff for: ‎packages/pg-query-stream/LICENSE

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2013 Brian M. Carlson
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Diff for: ‎packages/pg-query-stream/README.md

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# pg-query-stream
2+
3+
[![Build Status](https://travis-ci.org/brianc/node-pg-query-stream.svg)](https://travis-ci.org/brianc/node-pg-query-stream)
4+
5+
Receive result rows from [pg](https://github.com/brianc/node-postgres) as a readable (object) stream.
6+
7+
8+
## installation
9+
10+
```bash
11+
$ npm install pg --save
12+
$ npm install pg-query-stream --save
13+
```
14+
15+
_requires pg>=2.8.1_
16+
17+
18+
## use
19+
20+
```js
21+
const pg = require('pg')
22+
const QueryStream = require('pg-query-stream')
23+
const JSONStream = require('JSONStream')
24+
25+
//pipe 1,000,000 rows to stdout without blowing up your memory usage
26+
pg.connect((err, client, done) => {
27+
if (err) throw err;
28+
const query = new QueryStream('SELECT * FROM generate_series(0, $1) num', [1000000])
29+
const stream = client.query(query)
30+
//release the client when the stream is finished
31+
stream.on('end', done)
32+
stream.pipe(JSONStream.stringify()).pipe(process.stdout)
33+
})
34+
```
35+
36+
The stream uses a cursor on the server so it efficiently keeps only a low number of rows in memory.
37+
38+
This is especially useful when doing [ETL](http://en.wikipedia.org/wiki/Extract,_transform,_load) on a huge table. Using manual `limit` and `offset` queries to fake out async itteration through your data is cumbersome, and _way way way_ slower than using a cursor.
39+
40+
_note: this module only works with the JavaScript client, and does not work with the native bindings. libpq doesn't expose the protocol at a level where a cursor can be manipulated directly_
41+
42+
## contribution
43+
44+
I'm very open to contribution! Open a pull request with your code or idea and we'll talk about it. If it's not way insane we'll merge it in too: isn't open source awesome?
45+
46+
## license
47+
48+
The MIT License (MIT)
49+
50+
Copyright (c) 2013 Brian M. Carlson
51+
52+
Permission is hereby granted, free of charge, to any person obtaining a copy
53+
of this software and associated documentation files (the "Software"), to deal
54+
in the Software without restriction, including without limitation the rights
55+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
56+
copies of the Software, and to permit persons to whom the Software is
57+
furnished to do so, subject to the following conditions:
58+
59+
The above copyright notice and this permission notice shall be included in
60+
all copies or substantial portions of the Software.
61+
62+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
63+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
64+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
65+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
66+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
67+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
68+
THE SOFTWARE.

Diff for: ‎packages/pg-query-stream/index.js

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
'use strict'
2+
var Cursor = require('pg-cursor')
3+
var Readable = require('stream').Readable
4+
5+
class PgQueryStream extends Readable {
6+
constructor (text, values, options) {
7+
super(Object.assign({ objectMode: true }, options))
8+
this.cursor = new Cursor(text, values, options)
9+
this._reading = false
10+
this._closed = false
11+
this.batchSize = (options || {}).batchSize || 100
12+
13+
// delegate Submittable callbacks to cursor
14+
this.handleRowDescription = this.cursor.handleRowDescription.bind(this.cursor)
15+
this.handleDataRow = this.cursor.handleDataRow.bind(this.cursor)
16+
this.handlePortalSuspended = this.cursor.handlePortalSuspended.bind(this.cursor)
17+
this.handleCommandComplete = this.cursor.handleCommandComplete.bind(this.cursor)
18+
this.handleReadyForQuery = this.cursor.handleReadyForQuery.bind(this.cursor)
19+
this.handleError = this.cursor.handleError.bind(this.cursor)
20+
}
21+
22+
submit (connection) {
23+
this.cursor.submit(connection)
24+
}
25+
26+
close (callback) {
27+
this._closed = true
28+
const cb = callback || (() => this.emit('close'))
29+
this.cursor.close(cb)
30+
}
31+
32+
_read (size) {
33+
if (this._reading || this._closed) {
34+
return false
35+
}
36+
this._reading = true
37+
const readAmount = Math.max(size, this.batchSize)
38+
this.cursor.read(readAmount, (err, rows) => {
39+
if (this._closed) {
40+
return
41+
}
42+
if (err) {
43+
return this.emit('error', err)
44+
}
45+
// if we get a 0 length array we've read to the end of the cursor
46+
if (!rows.length) {
47+
this._closed = true
48+
setImmediate(() => this.emit('close'))
49+
return this.push(null)
50+
}
51+
52+
// push each row into the stream
53+
this._reading = false
54+
for (var i = 0; i < rows.length; i++) {
55+
this.push(rows[i])
56+
}
57+
})
58+
}
59+
}
60+
61+
module.exports = PgQueryStream

Diff for: ‎packages/pg-query-stream/package.json

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "pg-query-stream",
3+
"version": "2.0.2",
4+
"description": "Postgres query result returned as readable stream",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "mocha",
8+
"lint": "eslint ."
9+
},
10+
"repository": {
11+
"type": "git",
12+
"url": "git://github.com/brianc/node-postgres.git"
13+
},
14+
"keywords": [
15+
"postgres",
16+
"pg",
17+
"query",
18+
"stream"
19+
],
20+
"author": "Brian M. Carlson",
21+
"license": "MIT",
22+
"bugs": {
23+
"url": "https://github.com/brianc/node-postgres/issues"
24+
},
25+
"devDependencies": {
26+
"JSONStream": "~0.7.1",
27+
"concat-stream": "~1.0.1",
28+
"eslint-plugin-promise": "^3.5.0",
29+
"mocha": "^6.2.2",
30+
"pg": "^7.15.2",
31+
"stream-spec": "~0.3.5",
32+
"stream-tester": "0.0.5",
33+
"through": "~2.3.4"
34+
},
35+
"dependencies": {
36+
"pg-cursor": "^2.0.3"
37+
}
38+
}

Diff for: ‎packages/pg-query-stream/test/async-iterator.es6

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
const QueryStream = require('../')
2+
const pg = require('pg')
3+
const assert = require('assert')
4+
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+
35+
it('can async iterate multiple times with a pool', async () => {
36+
const pool = new pg.Pool({ max: 1 })
37+
38+
const allRows = []
39+
const run = async () => {
40+
// get the client
41+
const client = await pool.connect()
42+
// stream some rows
43+
const stream = new QueryStream(queryText, [])
44+
const iteratorRows = []
45+
client.query(stream)
46+
for await (const row of stream) {
47+
iteratorRows.push(row)
48+
allRows.push(row)
49+
}
50+
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+
})
57+
})

Diff for: ‎packages/pg-query-stream/test/async-iterator.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// only newer versions of node support async iterator
2+
if (!process.version.startsWith('v8')) {
3+
require('./async-iterator.es6')
4+
}

0 commit comments

Comments
 (0)
Please sign in to comment.