What is pg-copy-streams?
The pg-copy-streams npm package provides a way to perform high-performance data import and export operations with PostgreSQL using the COPY command. It allows you to stream data to and from PostgreSQL databases efficiently.
What are pg-copy-streams's main functionalities?
COPY FROM
This feature allows you to import data into a PostgreSQL table from a file using the COPY FROM command. The code sample demonstrates how to stream data from a CSV file into a PostgreSQL table.
const { Client } = require('pg');
const copyFrom = require('pg-copy-streams').from;
const fs = require('fs');
const client = new Client();
client.connect();
const stream = client.query(copyFrom('COPY my_table FROM STDIN'));
const fileStream = fs.createReadStream('data.csv');
fileStream.pipe(stream).on('finish', () => {
console.log('Data imported successfully');
client.end();
});
COPY TO
This feature allows you to export data from a PostgreSQL table to a file using the COPY TO command. The code sample demonstrates how to stream data from a PostgreSQL table into a CSV file.
const { Client } = require('pg');
const copyTo = require('pg-copy-streams').to;
const fs = require('fs');
const client = new Client();
client.connect();
const stream = client.query(copyTo('COPY my_table TO STDOUT'));
const fileStream = fs.createWriteStream('data.csv');
stream.pipe(fileStream).on('finish', () => {
console.log('Data exported successfully');
client.end();
});
Other packages similar to pg-copy-streams
pg
The 'pg' package is the official PostgreSQL client for Node.js. While it does not provide streaming capabilities for the COPY command out of the box, it can be used in conjunction with other libraries to achieve similar functionality. It is a more general-purpose library for interacting with PostgreSQL databases.
pg-promise
The 'pg-promise' package is a PostgreSQL interface for Node.js that provides a more powerful and flexible API compared to the 'pg' package. It supports a wide range of features, including transactions and connection pooling, but does not natively support streaming for the COPY command. However, it can be extended to work with streaming libraries.
pg-boss
The 'pg-boss' package is a job queue built on top of PostgreSQL. While it is not directly related to streaming data with the COPY command, it provides a way to manage background jobs and tasks using PostgreSQL as the backend. It is useful for scenarios where you need to process large amounts of data asynchronously.