Skip to content

Commit 296a440

Browse files
committed
first fixes
1 parent 28ac2a1 commit 296a440

File tree

9 files changed

+1358
-849
lines changed

9 files changed

+1358
-849
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "node postgres monorepo",
44
"main": "index.js",
55
"private": true,
6-
"repository": "[email protected]:brianc/node-postgres.git",
6+
"repository": "[email protected]:MedFlyt/node-postgres.git",
77
"author": "Brian M. Carlson <[email protected]>",
88
"license": "MIT",
99
"workspaces": [
@@ -18,6 +18,7 @@
1818
"lint": "eslint '*/**/*.{js,ts,tsx}'"
1919
},
2020
"devDependencies": {
21+
"@types/node": "^17.0.36",
2122
"@typescript-eslint/eslint-plugin": "^4.4.0",
2223
"@typescript-eslint/parser": "^4.4.0",
2324
"eslint": "^7.11.0",

packages/pg-pool/test/idle-timeout-exit.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ if (module === require.main) {
33
const allowExitOnIdle = process.env.ALLOW_EXIT_ON_IDLE === '1'
44
const Pool = require('../index')
55

6-
const pool = new Pool({ maxLifetimeSeconds: 2, idleTimeoutMillis: 200, ...(allowExitOnIdle ? { allowExitOnIdle: true } : {}) })
6+
const pool = new Pool({
7+
maxLifetimeSeconds: 2,
8+
idleTimeoutMillis: 200,
9+
...(allowExitOnIdle ? { allowExitOnIdle: true } : {}),
10+
})
711
pool.query('SELECT NOW()', (err, res) => console.log('completed first'))
812
pool.on('remove', () => {
913
console.log('removed')

packages/pg/lib/client.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ class Client extends EventEmitter {
190190
con.on('rowDescription', this._handleRowDescription.bind(this))
191191
con.on('dataRow', this._handleDataRow.bind(this))
192192
con.on('portalSuspended', this._handlePortalSuspended.bind(this))
193+
// delegate paramDescription to active query
194+
con.on('paramDescription', this.activeQuery.handleParamDescription(this, con))
193195
con.on('emptyQuery', this._handleEmptyQuery.bind(this))
194196
con.on('commandComplete', this._handleCommandComplete.bind(this))
195197
con.on('parseComplete', this._handleParseComplete.bind(this))

packages/pg/lib/query.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class Query extends EventEmitter {
1717
this.types = config.types
1818
this.name = config.name
1919
this.binary = config.binary
20+
this.describe = config.describe
2021
// use unique portal name each time
2122
this.portal = config.portal || ''
2223
this.callback = config.callback
@@ -38,6 +39,10 @@ class Query extends EventEmitter {
3839
if (this.name) {
3940
return true
4041
}
42+
// always prepare if describing a query
43+
if (this.describe) {
44+
return true
45+
}
4146
// always prepare if there are max number of rows expected per
4247
// portal execution
4348
if (this.rows) {
@@ -159,6 +164,10 @@ class Query extends EventEmitter {
159164
return null
160165
}
161166

167+
handleParamDescription(msg, con) {
168+
this._result.addParams(msg.params)
169+
con.sync()
170+
}
162171
hasBeenParsed(connection) {
163172
return this.name && connection.parsedStatements[this.name]
164173
}
@@ -197,6 +206,15 @@ class Query extends EventEmitter {
197206
})
198207
}
199208

209+
if (this.describe) {
210+
// if describe is set, the query is not executed
211+
connection.describe({
212+
type: 'S',
213+
name: this.name,
214+
})
215+
connection.flush()
216+
return
217+
}
200218
// because we're mapping user supplied values to
201219
// postgres wire protocol compatible values it could
202220
// throw an exception, so try/catch this section

packages/pg/lib/result.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class Result {
1414
this.oid = null
1515
this.rows = []
1616
this.fields = []
17+
this.params = []
1718
this._parsers = undefined
1819
this._types = types
1920
this.RowCtor = null
@@ -95,6 +96,15 @@ class Result {
9596
}
9697
}
9798
}
98-
}
9999

100+
addParams(paramDescription) {
101+
if (this.params.length) {
102+
this.params = []
103+
}
104+
for (var i = 0; i < paramDescriptions.length; i++) {
105+
var desc = paramDescriptions[i]
106+
this.params.push(desc)
107+
}
108+
}
109+
}
100110
module.exports = Result

packages/pg/test/integration/client/prepared-statement-tests.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,32 @@ var suite = new helper.Suite()
186186

187187
suite.test('cleanup', () => client.end())
188188
})()
189+
;(function () {
190+
var client = helper.client()
191+
client.on('drain', client.end.bind(client))
192+
193+
suite.test('describe', function (done) {
194+
client.query(
195+
new Query(
196+
{
197+
text: 'SELECT id, name, age FROM person WHERE age > $1',
198+
describe: true,
199+
},
200+
(res) => {
201+
assert.deepEqual(res.params, [23])
202+
assert.deepEqual(
203+
res.fields.map((field) => ({ name: field.name, type: field.dataTypeID })),
204+
[
205+
{ name: 'id', type: 23 },
206+
{ name: 'name', type: 1043 },
207+
{ name: 'age', type: 23 },
208+
]
209+
)
210+
done()
211+
}
212+
)
213+
)
214+
})
215+
216+
suite.test('cleanup', () => client.end())
217+
})()

packages/pg/test/unit/client/prepared-statement-tests.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,55 @@ test('prepared statement with explicit portal', function () {
155155
})
156156
})
157157
})
158+
var describeClient = helper.client()
159+
var describeCon = describeClient.connection
160+
describeCon.parse = function (arg) {
161+
process.nextTick(function () {
162+
describeCon.emit('parseComplete')
163+
})
164+
}
165+
var describeDescribeArg = null
166+
describeCon.describe = function (arg) {
167+
describeDescribeArg = arg
168+
}
169+
170+
var describeFlushCalled = false
171+
describeCon.flush = function () {
172+
describeFlushCalled = true
173+
process.nextTick(function () {
174+
describeCon.emit('paramDescription', { params: [] })
175+
describeCon.emit('rowDescription', { fields: [] })
176+
})
177+
}
178+
179+
var describeSyncCalled = false
180+
describeCon.sync = function () {
181+
process.nextTick(function () {
182+
describeSyncCalled = true
183+
describeCon.emit('readyForQuery')
184+
})
185+
}
186+
187+
test('describe prepared statement', function () {
188+
assert.ok(describeClient.connection.emit('readyForQuery'))
189+
190+
var query = describeClient.query(
191+
new Query({
192+
text: 'select * from X where name = $1',
193+
describe: true,
194+
})
195+
)
196+
197+
assert.emits(query, 'end', function () {
198+
test('describe argument', function () {
199+
assert.equal(describeDescribeArg.type, 'S')
200+
assert.equal(describeDescribeArg.name, undefined)
201+
})
202+
test('flush called', function () {
203+
assert.ok(describeFlushCalled)
204+
})
205+
test('sync called', function () {
206+
assert.ok(describeSyncCalled)
207+
})
208+
})
209+
})

packages/pg/test/unit/client/result-metadata-tests.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,14 @@ var check = function (oid, rowCount, command) {
3232
assert.equal(result.command, command)
3333
}
3434
}
35-
36-
testForTag('INSERT 0 3', check(0, 3, 'INSERT'))
37-
testForTag('INSERT 841 1', check(841, 1, 'INSERT'))
38-
testForTag('DELETE 10', check(null, 10, 'DELETE'))
39-
testForTag('UPDATE 11', check(null, 11, 'UPDATE'))
40-
testForTag('SELECT 20', check(null, 20, 'SELECT'))
41-
testForTag('COPY', check(null, null, 'COPY'))
42-
testForTag('COPY 12345', check(null, 12345, 'COPY'))
35+
try {
36+
testForTag('INSERT 0 3', check(0, 3, 'INSERT'))
37+
testForTag('INSERT 841 1', check(841, 1, 'INSERT'))
38+
testForTag('DELETE 10', check(null, 10, 'DELETE'))
39+
testForTag('UPDATE 11', check(null, 11, 'UPDATE'))
40+
testForTag('SELECT 20', check(null, 20, 'SELECT'))
41+
testForTag('COPY', check(null, null, 'COPY'))
42+
testForTag('COPY 12345', check(null, 12345, 'COPY'))
43+
} catch (error) {
44+
console.log(error)
45+
}

0 commit comments

Comments
 (0)