Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/soft-starfishes-stare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@electric-sql/pglite': patch
---

Add affectedRows for COPY command
1 change: 1 addition & 0 deletions packages/pglite/src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ function retrieveRowCount(msg: CommandCompleteMessage): number {
return parseInt(parts[2], 10)
case 'UPDATE':
case 'DELETE':
case 'COPY':
return parseInt(parts[1], 10)
default:
return 0
Expand Down
60 changes: 37 additions & 23 deletions packages/pglite/tests/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -468,32 +468,46 @@ await testEsmAndCjs(async (importType) => {
it('copy to/from blob', async () => {
const db = new PGlite()
await db.exec(`
CREATE TABLE IF NOT EXISTS test (
id SERIAL PRIMARY KEY,
test TEXT
);
INSERT INTO test (test) VALUES ('test'), ('test2');
`)
CREATE TABLE IF NOT EXISTS test (
id SERIAL PRIMARY KEY,
test TEXT
);
INSERT INTO test (test) VALUES ('test'), ('test2');
`)

// copy to
const ret = await db.query("COPY test TO '/dev/blob' WITH (FORMAT csv);")
const csv = await ret.blob.text()
const copyToRet = await db.query(
"COPY test TO '/dev/blob' WITH (FORMAT csv);",
)

// Check that the copy command returns the number of rows affected
expect(copyToRet.affectedRows).toBe(2)

const csv = await copyToRet.blob.text()
expect(csv).toBe('1,test\n2,test2\n')

// copy from
const blob2 = new Blob([csv])
await db.exec(`
CREATE TABLE IF NOT EXISTS test2 (
id SERIAL PRIMARY KEY,
test TEXT
);
`)
await db.query("COPY test2 FROM '/dev/blob' WITH (FORMAT csv);", [], {
blob: blob2,
})
CREATE TABLE IF NOT EXISTS test2 (
id SERIAL PRIMARY KEY,
test TEXT
);
`)
const copyFromRet = await db.query(
"COPY test2 FROM '/dev/blob' WITH (FORMAT csv);",
[],
{
blob: blob2,
},
)

// Check that the copy command returns the number of rows affected
expect(copyFromRet.affectedRows).toBe(2)

const res = await db.query(`
SELECT * FROM test2;
`)
SELECT * FROM test2;
`)
expect(res).toEqual({
rows: [
{
Expand Down Expand Up @@ -522,11 +536,11 @@ await testEsmAndCjs(async (importType) => {
it('close', async () => {
const db = new PGlite()
await db.query(`
CREATE TABLE IF NOT EXISTS test (
id SERIAL PRIMARY KEY,
name TEXT
);
`)
CREATE TABLE IF NOT EXISTS test (
id SERIAL PRIMARY KEY,
name TEXT
);
`)
await db.query("INSERT INTO test (name) VALUES ('test');")
await db.close()
await expectToThrowAsync(async () => {
Expand Down