Skip to content

Commit

Permalink
Message parsing optimization (#495)
Browse files Browse the repository at this point in the history
* do not run twice through the messages when parsing

* return affectedRows as 0 when the resultSets is empty

* changeset

---------

Co-authored-by: tudor <[email protected]>
  • Loading branch information
copiltembel and tudor authored Jan 14, 2025
1 parent 358b8eb commit c36fd09
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 20 deletions.
5 changes: 5 additions & 0 deletions .changeset/afraid-wolves-tease.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@electric-sql/pglite': patch
---

Improvements to parsing results received from pg
27 changes: 7 additions & 20 deletions packages/pglite/src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,7 @@ export function parseResults(
let affectedRows = 0
const parsers = { ...defaultParsers, ...options?.parsers }

const processMessageTypes = new Set([
'rowDescription',
'dataRow',
'commandComplete',
])

const filteredMessages = messages.filter((msg) =>
processMessageTypes.has(msg.name),
)

filteredMessages.forEach((message, index) => {
messages.forEach((message) => {
switch (message.name) {
case 'rowDescription': {
const msg = message as RowDescriptionMessage
Expand Down Expand Up @@ -73,15 +63,11 @@ export function parseResults(
const msg = message as CommandCompleteMessage
affectedRows += retrieveRowCount(msg)

if (index === filteredMessages.length - 1) {
resultSets.push({
...currentResultSet,
affectedRows,
...(blob ? { blob } : {}),
})
} else {
resultSets.push(currentResultSet)
}
resultSets.push({
...currentResultSet,
affectedRows,
...(blob ? { blob } : {}),
})

currentResultSet = { rows: [], fields: [] }
break
Expand All @@ -91,6 +77,7 @@ export function parseResults(

if (resultSets.length === 0) {
resultSets.push({
affectedRows: 0,
rows: [],
fields: [],
})
Expand Down
2 changes: 2 additions & 0 deletions packages/pglite/tests/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ await testEsmAndCjs(async (importType) => {

expect(multiStatementResult).toEqual([
{
affectedRows: 1,
rows: [],
fields: [],
},
{
affectedRows: 2,
rows: [],
fields: [],
},
Expand Down

0 comments on commit c36fd09

Please sign in to comment.