Skip to content

Commit 78a14a1

Browse files
authored
feat(): pg-query-stream typescript (#2376)
* feat(): start converting pg-query stream * feat(): solution project, initial version of typescript-pg-query stream * chore(): mocha with typescript * fix(): eslint ignore query stream dist * refactor(pg-query-stream): convert test to ts * chore(): fixed type errors * chore(): fix helper usage * chore(): use ts-node compatibile with node v8 * fix(): addd es extension * chore(): remove emitClose and added compilation for async iterators * chore(): condition for asyc iteration test * chore(): rename class to match ts-defs * chore(): tests to import from src instead of dist * chore(): remove prettier from peer deps: * chore(): update lock file
1 parent 52dfca4 commit 78a14a1

32 files changed

+424
-320
lines changed

.eslintrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"plugins": ["prettier"],
33
"parser": "@typescript-eslint/parser",
44
"extends": ["plugin:prettier/recommended", "prettier/@typescript-eslint"],
5-
"ignorePatterns": ["node_modules", "coverage", "packages/pg-protocol/dist/**/*"],
5+
"ignorePatterns": ["node_modules", "coverage", "packages/pg-protocol/dist/**/*", "packages/pg-query-stream/dist/**/*"],
66
"parserOptions": {
77
"ecmaVersion": 2017,
88
"sourceType": "module"

package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
],
1212
"scripts": {
1313
"test": "yarn lerna exec yarn test",
14-
"build": "yarn lerna exec --scope pg-protocol yarn build",
14+
"build": "tsc --build",
15+
"build:watch": "tsc --build --watch",
1516
"pretest": "yarn build",
1617
"lint": "eslint '*/**/*.{js,ts,tsx}'"
1718
},
@@ -23,7 +24,8 @@
2324
"eslint-plugin-node": "^11.1.0",
2425
"eslint-plugin-prettier": "^3.1.4",
2526
"lerna": "^3.19.0",
26-
"prettier": "2.1.2"
27+
"prettier": "2.1.2",
28+
"typescript": "^4.0.3"
2729
},
2830
"prettier": {
2931
"semi": false,

packages/pg-protocol/package.json

+6-2
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,17 @@
1313
"chunky": "^0.0.0",
1414
"mocha": "^7.1.2",
1515
"ts-node": "^8.5.4",
16-
"typescript": "^3.7.3"
16+
"typescript": "^4.0.3"
1717
},
1818
"scripts": {
1919
"test": "mocha dist/**/*.test.js",
2020
"build": "tsc",
2121
"build:watch": "tsc --watch",
2222
"prepublish": "yarn build",
2323
"pretest": "yarn build"
24-
}
24+
},
25+
"files": [
26+
"/dist/*{js,ts,map}",
27+
"/src"
28+
]
2529
}

packages/pg-protocol/tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"moduleResolution": "node",
1010
"sourceMap": true,
1111
"outDir": "dist",
12+
"incremental": true,
1213
"baseUrl": ".",
1314
"declaration": true,
1415
"paths": {

packages/pg-query-stream/package.json

+14-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
"name": "pg-query-stream",
33
"version": "3.3.2",
44
"description": "Postgres query result returned as readable stream",
5-
"main": "index.js",
5+
"main": "./dist/index.js",
6+
"types": "./dist/index.d.ts",
67
"scripts": {
7-
"test": "mocha"
8+
"test": "mocha -r ts-node/register test/**/*.ts"
89
},
910
"repository": {
1011
"type": "git",
@@ -16,20 +17,30 @@
1617
"query",
1718
"stream"
1819
],
20+
"files": [
21+
"/dist/*{js,ts,map}",
22+
"/src"
23+
],
1924
"author": "Brian M. Carlson",
2025
"license": "MIT",
2126
"bugs": {
2227
"url": "https://github.com/brianc/node-postgres/issues"
2328
},
2429
"devDependencies": {
30+
"@types/node": "^14.0.0",
31+
"@types/pg": "^7.14.5",
32+
"@types/chai": "^4.2.13",
33+
"@types/mocha": "^8.0.3",
2534
"JSONStream": "~0.7.1",
2635
"concat-stream": "~1.0.1",
2736
"eslint-plugin-promise": "^3.5.0",
2837
"mocha": "^7.1.2",
2938
"pg": "^8.4.2",
3039
"stream-spec": "~0.3.5",
3140
"stream-tester": "0.0.5",
32-
"through": "~2.3.4"
41+
"through": "~2.3.4",
42+
"ts-node": "^8.5.4",
43+
"typescript": "^4.0.3"
3344
},
3445
"dependencies": {
3546
"pg-cursor": "^2.4.2"
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,30 @@
1-
const { Readable } = require('stream')
2-
const Cursor = require('pg-cursor')
1+
import { Readable } from 'stream'
2+
import { Submittable, Connection } from 'pg'
3+
import Cursor from 'pg-cursor'
34

4-
class PgQueryStream extends Readable {
5-
constructor(text, values, config = {}) {
5+
interface QueryStreamConfig {
6+
batchSize?: number
7+
highWaterMark?: number
8+
rowMode?: 'array'
9+
types?: any
10+
}
11+
12+
class QueryStream extends Readable implements Submittable {
13+
cursor: any
14+
_result: any
15+
16+
handleRowDescription: Function
17+
handleDataRow: Function
18+
handlePortalSuspended: Function
19+
handleCommandComplete: Function
20+
handleReadyForQuery: Function
21+
handleError: Function
22+
handleEmptyQuery: Function
23+
24+
public constructor(text: string, values?: any[], config: QueryStreamConfig = {}) {
625
const { batchSize, highWaterMark = 100 } = config
7-
// https://nodejs.org/api/stream.html#stream_new_stream_readable_options
8-
super({ objectMode: true, emitClose: true, autoDestroy: true, highWaterMark: batchSize || highWaterMark })
26+
27+
super({ objectMode: true, autoDestroy: true, highWaterMark: batchSize || highWaterMark })
928
this.cursor = new Cursor(text, values, config)
1029

1130
// delegate Submittable callbacks to cursor
@@ -21,19 +40,19 @@ class PgQueryStream extends Readable {
2140
this._result = this.cursor._result
2241
}
2342

24-
submit(connection) {
43+
public submit(connection: Connection): void {
2544
this.cursor.submit(connection)
2645
}
2746

28-
_destroy(_err, cb) {
29-
this.cursor.close((err) => {
47+
public _destroy(_err: Error, cb: Function) {
48+
this.cursor.close((err?: Error) => {
3049
cb(err || _err)
3150
})
3251
}
3352

3453
// https://nodejs.org/api/stream.html#stream_readable_read_size_1
35-
_read(size) {
36-
this.cursor.read(size, (err, rows, result) => {
54+
public _read(size: number) {
55+
this.cursor.read(size, (err: Error, rows: any[]) => {
3756
if (err) {
3857
// https://nodejs.org/api/stream.html#stream_errors_while_reading
3958
this.destroy(err)
@@ -45,4 +64,4 @@ class PgQueryStream extends Readable {
4564
}
4665
}
4766

48-
module.exports = PgQueryStream
67+
export = QueryStream

packages/pg-query-stream/test/async-iterator.es6

-112
This file was deleted.

packages/pg-query-stream/test/async-iterator.js

-4
This file was deleted.

0 commit comments

Comments
 (0)