Skip to content

Commit

Permalink
feat(): pg-query-stream typescript (#2376)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
chyzwar committed Nov 3, 2020
1 parent 52dfca4 commit 78a14a1
Show file tree
Hide file tree
Showing 32 changed files with 424 additions and 320 deletions.
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 })

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.

0 comments on commit 78a14a1

Please sign in to comment.