Socket
Socket
Sign inDemoInstall

pg-query-stream

Package Overview
Dependencies
Maintainers
1
Versions
58
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pg-query-stream

Postgres query result returned as readable stream


Version published
Weekly downloads
167K
decreased by-41.56%
Maintainers
1
Weekly downloads
 
Created

What is pg-query-stream?

The pg-query-stream npm package allows you to stream PostgreSQL query results using Node.js. This is particularly useful for handling large datasets that you don't want to load entirely into memory. It leverages the PostgreSQL client for Node.js (pg) to provide a readable stream interface for query results.

What are pg-query-stream's main functionalities?

Streaming Query Results

This feature allows you to stream the results of a PostgreSQL query. The code sample demonstrates how to set up a client, create a query stream, and pipe the results to the standard output in JSON format.

const { Client } = require('pg');
const QueryStream = require('pg-query-stream');
const JSONStream = require('JSONStream');

async function streamQuery() {
  const client = new Client({
    user: 'your_user',
    host: 'your_host',
    database: 'your_database',
    password: 'your_password',
    port: 5432,
  });

  await client.connect();

  const query = new QueryStream('SELECT * FROM your_table');
  const stream = client.query(query);

  stream.pipe(JSONStream.stringify()).pipe(process.stdout);

  stream.on('end', () => {
    client.end();
  });
}

streamQuery();

Handling Large Datasets

This feature is useful for handling large datasets by streaming query results directly to a file. The code sample shows how to stream the results of a large table query to an output file.

const { Client } = require('pg');
const QueryStream = require('pg-query-stream');
const fs = require('fs');

async function streamToFile() {
  const client = new Client({
    user: 'your_user',
    host: 'your_host',
    database: 'your_database',
    password: 'your_password',
    port: 5432,
  });

  await client.connect();

  const query = new QueryStream('SELECT * FROM large_table');
  const stream = client.query(query);
  const fileStream = fs.createWriteStream('output.json');

  stream.pipe(fileStream);

  stream.on('end', () => {
    client.end();
  });
}

streamToFile();

Other packages similar to pg-query-stream

Keywords

FAQs

Package last updated on 06 Mar 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc