{"library":"pg-cursor","title":"PostgreSQL Query Cursor for Node.js","description":"pg-cursor is a specialized extension for the `node-postgres` library, enabling Node.js applications to utilize PostgreSQL result cursors. This functionality allows developers to fetch large query results incrementally in manageable chunks, effectively preventing out-of-memory issues that can arise when loading massive datasets into memory all at once. The library is currently at version 2.19.0 and is actively maintained as part of the broader `node-postgres` ecosystem, with updates typically released to ensure compatibility, address bugs, or introduce minor enhancements. Its primary differentiator lies in providing an efficient, streaming approach to data retrieval from PostgreSQL, which is crucial for high-performance applications dealing with extensive database tables. A key technical requirement is its exclusive compatibility with the pure JavaScript `pg` client, explicitly stating it does not support native PostgreSQL bindings.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install pg-cursor"],"cli":null},"imports":["import Cursor from 'pg-cursor';","const Cursor = require('pg-cursor');"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { Client } from 'pg';\nimport Cursor from 'pg-cursor';\n\nasync function processLargeResult() {\n  const client = new Client({\n    user: process.env.PGUSER ?? 'postgres',\n    host: process.env.PGHOST ?? 'localhost',\n    database: process.env.PGDATABASE ?? 'your_database',\n    password: process.env.PGPASSWORD ?? 'password',\n    port: parseInt(process.env.PGPORT ?? '5432', 10),\n  });\n\n  await client.connect();\n  console.log('Connected to database.');\n\n  const query = 'SELECT id, data FROM large_table ORDER BY id'; // Replace large_table with your table\n  const cursor = client.query(new Cursor(query));\n\n  const BATCH_SIZE = 100;\n  let rowsProcessed = 0;\n\n  try {\n    while (true) {\n      const rows = await new Promise<any[]>((resolve, reject) => {\n        cursor.read(BATCH_SIZE, (err, batchRows) => {\n          if (err) return reject(err);\n          resolve(batchRows);\n        });\n      });\n\n      if (rows.length === 0) {\n        break; // No more rows to read\n      }\n\n      for (const row of rows) {\n        // console.log(`Processing row ID: ${row.id}`);\n        // Perform your row-specific logic here\n        rowsProcessed++;\n      }\n\n      if (rows.length < BATCH_SIZE) {\n        break; // Less than batch size means we've reached the end\n      }\n    }\n    console.log(`Successfully processed ${rowsProcessed} rows using a cursor.`);\n  } catch (error) {\n    console.error('Error during cursor processing:', error);\n  } finally {\n    try {\n      await cursor.close();\n      console.log('Cursor closed.');\n    } catch (closeError) {\n      console.warn('Error closing cursor (might be already closed or connection issue):', closeError);\n    }\n    await client.end();\n    console.log('Database connection ended.');\n  }\n}\n\nprocessLargeResult().catch(err => console.error('Unhandled error:', err));\n","lang":"typescript","description":"Demonstrates connecting to a PostgreSQL database, creating a cursor for a large query, reading rows in batches, processing them, and properly closing the cursor and client connection.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}