-
Notifications
You must be signed in to change notification settings - Fork 226
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for adding or receiving a blob for use with COPY FROM/TO (#106)
* Support for adding or receving a blob for a COPY * Add tests * Docs * Fix submodule
- Loading branch information
Showing
9 changed files
with
241 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<script type="module"> | ||
import { PGlite } from "../dist/index.js"; | ||
|
||
const pg = new PGlite(); | ||
|
||
await pg.exec(` | ||
CREATE TABLE IF NOT EXISTS test ( | ||
id SERIAL PRIMARY KEY, | ||
name TEXT | ||
); | ||
`); | ||
|
||
// add 1000 rows: | ||
await pg.exec("INSERT INTO test (name) SELECT 'name' || i FROM generate_series(1, 1000) i;"); | ||
|
||
// Copy the date to a file: | ||
console.log('Copying data to file...') // 'test.csv | ||
const ret = await pg.query("COPY test TO '/dev/blob' WITH (FORMAT binary);"); | ||
|
||
console.log('Data copied to blob:') | ||
const blob = ret.blob; | ||
console.log(blob); | ||
|
||
// Download the file: | ||
const a = document.createElement('a'); | ||
a.href = URL.createObjectURL(new File([blob], 'test.csv', { type: 'text/csv' })); | ||
a.download = 'test.csv'; | ||
a.click(); | ||
|
||
// Other table | ||
await pg.exec(` | ||
CREATE TABLE IF NOT EXISTS test2 ( | ||
id SERIAL PRIMARY KEY, | ||
name TEXT | ||
); | ||
`); | ||
|
||
// import the data from the file: | ||
console.log('Importing data from file...') | ||
const ret2 = await pg.query("COPY test2 FROM '/dev/blob' WITH (FORMAT binary);", [], { | ||
blob: blob | ||
}); | ||
|
||
console.log('Data imported from file:') | ||
const ret3 = await pg.query("SELECT * FROM test2;", []); | ||
console.log(ret3.rows); | ||
|
||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.