Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(): pg-query-stream typescript #2376

Merged
merged 15 commits into from Nov 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc
Expand Up @@ -2,7 +2,7 @@
"plugins": ["prettier"],
"parser": "@typescript-eslint/parser",
"extends": ["plugin:prettier/recommended", "prettier/@typescript-eslint"],
"ignorePatterns": ["node_modules", "coverage", "packages/pg-protocol/dist/**/*"],
"ignorePatterns": ["node_modules", "coverage", "packages/pg-protocol/dist/**/*", "packages/pg-query-stream/dist/**/*"],
"parserOptions": {
"ecmaVersion": 2017,
"sourceType": "module"
Expand Down
6 changes: 4 additions & 2 deletions package.json
Expand Up @@ -11,7 +11,8 @@
],
"scripts": {
"test": "yarn lerna exec yarn test",
"build": "yarn lerna exec --scope pg-protocol yarn build",
"build": "tsc --build",
"build:watch": "tsc --build --watch",
"pretest": "yarn build",
"lint": "eslint '*/**/*.{js,ts,tsx}'"
},
Expand All @@ -23,7 +24,8 @@
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^3.1.4",
"lerna": "^3.19.0",
"prettier": "2.1.2"
"prettier": "2.1.2",
"typescript": "^4.0.3"
},
"prettier": {
"semi": false,
Expand Down
8 changes: 6 additions & 2 deletions packages/pg-protocol/package.json
Expand Up @@ -13,13 +13,17 @@
"chunky": "^0.0.0",
"mocha": "^7.1.2",
"ts-node": "^8.5.4",
"typescript": "^3.7.3"
"typescript": "^4.0.3"
},
"scripts": {
"test": "mocha dist/**/*.test.js",
"build": "tsc",
"build:watch": "tsc --watch",
"prepublish": "yarn build",
"pretest": "yarn build"
}
},
"files": [
"/dist/*{js,ts,map}",
"/src"
]
}
1 change: 1 addition & 0 deletions packages/pg-protocol/tsconfig.json
Expand Up @@ -9,6 +9,7 @@
"moduleResolution": "node",
"sourceMap": true,
"outDir": "dist",
"incremental": true,
"baseUrl": ".",
"declaration": true,
"paths": {
Expand Down
17 changes: 14 additions & 3 deletions packages/pg-query-stream/package.json
Expand Up @@ -2,9 +2,10 @@
"name": "pg-query-stream",
"version": "3.3.2",
"description": "Postgres query result returned as readable stream",
"main": "index.js",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"scripts": {
"test": "mocha"
"test": "mocha -r ts-node/register test/**/*.ts"
},
"repository": {
"type": "git",
Expand All @@ -16,20 +17,30 @@
"query",
"stream"
],
"files": [
"/dist/*{js,ts,map}",
"/src"
],
"author": "Brian M. Carlson",
"license": "MIT",
"bugs": {
"url": "https://github.com/brianc/node-postgres/issues"
},
"devDependencies": {
"@types/node": "^14.0.0",
"@types/pg": "^7.14.5",
"@types/chai": "^4.2.13",
"@types/mocha": "^8.0.3",
"JSONStream": "~0.7.1",
"concat-stream": "~1.0.1",
"eslint-plugin-promise": "^3.5.0",
"mocha": "^7.1.2",
"pg": "^8.4.2",
"stream-spec": "~0.3.5",
"stream-tester": "0.0.5",
"through": "~2.3.4"
"through": "~2.3.4",
"ts-node": "^8.5.4",
"typescript": "^4.0.3"
},
"dependencies": {
"pg-cursor": "^2.4.2"
Expand Down
@@ -1,11 +1,30 @@
const { Readable } = require('stream')
const Cursor = require('pg-cursor')
import { Readable } from 'stream'
import { Submittable, Connection } from 'pg'
import Cursor from 'pg-cursor'

class PgQueryStream extends Readable {
constructor(text, values, config = {}) {
interface QueryStreamConfig {
batchSize?: number
highWaterMark?: number
rowMode?: 'array'
types?: any
}

class QueryStream extends Readable implements Submittable {
cursor: any
_result: any

handleRowDescription: Function
handleDataRow: Function
handlePortalSuspended: Function
handleCommandComplete: Function
handleReadyForQuery: Function
handleError: Function
handleEmptyQuery: Function

public constructor(text: string, values?: any[], config: QueryStreamConfig = {}) {
const { batchSize, highWaterMark = 100 } = config
// https://nodejs.org/api/stream.html#stream_new_stream_readable_options
super({ objectMode: true, emitClose: true, autoDestroy: true, highWaterMark: batchSize || highWaterMark })
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

curious: why remove emitClose here?


super({ objectMode: true, autoDestroy: true, highWaterMark: batchSize || highWaterMark })
this.cursor = new Cursor(text, values, config)

// delegate Submittable callbacks to cursor
Expand All @@ -21,19 +40,19 @@ class PgQueryStream extends Readable {
this._result = this.cursor._result
}

submit(connection) {
public submit(connection: Connection): void {
this.cursor.submit(connection)
}

_destroy(_err, cb) {
this.cursor.close((err) => {
public _destroy(_err: Error, cb: Function) {
this.cursor.close((err?: Error) => {
cb(err || _err)
})
}

// https://nodejs.org/api/stream.html#stream_readable_read_size_1
_read(size) {
this.cursor.read(size, (err, rows, result) => {
public _read(size: number) {
this.cursor.read(size, (err: Error, rows: any[]) => {
if (err) {
// https://nodejs.org/api/stream.html#stream_errors_while_reading
this.destroy(err)
Expand All @@ -45,4 +64,4 @@ class PgQueryStream extends Readable {
}
}

module.exports = PgQueryStream
export = QueryStream
112 changes: 0 additions & 112 deletions packages/pg-query-stream/test/async-iterator.es6

This file was deleted.

4 changes: 0 additions & 4 deletions packages/pg-query-stream/test/async-iterator.js

This file was deleted.