{"id":15024900,"url":"https://github.com/mscdex/pg2","last_synced_at":"2025-04-12T12:53:15.024Z","repository":{"id":57322668,"uuid":"65649036","full_name":"mscdex/pg2","owner":"mscdex","description":"A PostgreSQL driver for node.js that focuses on performance","archived":false,"fork":false,"pushed_at":"2016-08-15T02:06:30.000Z","size":20,"stargazers_count":20,"open_issues_count":0,"forks_count":0,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-26T07:36:38.548Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mscdex.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-08-14T03:59:14.000Z","updated_at":"2024-11-18T17:45:12.000Z","dependencies_parsed_at":"2022-08-26T01:11:22.394Z","dependency_job_id":null,"html_url":"https://github.com/mscdex/pg2","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mscdex%2Fpg2","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mscdex%2Fpg2/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mscdex%2Fpg2/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mscdex%2Fpg2/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mscdex","download_url":"https://codeload.github.com/mscdex/pg2/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248571640,"owners_count":21126520,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-09-24T20:01:08.752Z","updated_at":"2025-04-12T12:53:15.004Z","avatar_url":"https://github.com/mscdex.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"\nDescription\n===========\n\nA PostgreSQL driver for [node.js](http://nodejs.org/) that focuses on performance.\n\nTested with PostgreSQL 9.5.\n\n\nRequirements\n============\n\n* [node.js](http://nodejs.org/) -- v6.3.0 or newer\n\n\nInstall\n=======\n\n    npm install pg2\n\n\nExamples\n========\n\n* Buffered SELECT query:\n\n```js\nvar Client = require('pg2');\n\nvar c = new Client({\n  host: '127.0.0.1',\n  user: 'foo',\n  password: 'bar',\n  db: 'test'\n});\n\nc.query('SELECT i FROM generate_series(1, 10) AS i', (err, rows) =\u003e {\n  if (err)\n    throw err;\n  console.dir(rows);\n});\n\nc.end();\n```\n\n* Streamed SELECT query:\n\n```js\nvar Client = require('pg2');\n\nvar c = new Client({\n  host: '127.0.0.1',\n  user: 'foo',\n  password: 'bar',\n  db: 'test'\n});\n\nc.query('SELECT i FROM generate_series(1, 10) AS i');\n .on('data', (result) =\u003e {\n  result.on('data', (row) =\u003e {\n    console.dir(row);\n  });\n});\n\nc.end();\n```\n\n* Using arrays (faster) instead of objects for rows:\n\n```js\nvar Client = require('pg2');\n\nvar c = new Client({\n  host: '127.0.0.1',\n  user: 'foo',\n  password: 'bar',\n  db: 'test'\n});\n\nc.query('SELECT i FROM generate_series(1, 10) AS i', { arrays: true }, (err, rows) =\u003e {\n  if (err)\n    throw err;\n  console.dir(rows);\n});\n\nc.end();\n```\n\n* Using positional parameters in a query:\n\n```js\nvar Client = require('pg2');\n\nvar c = new Client({\n  host: '127.0.0.1',\n  user: 'foo',\n  password: 'bar',\n  db: 'test'\n});\n\nc.query('SELECT i FROM generate_series($1::numeric, $2::numeric) AS i',\n        [1, 10],\n        (err, rows) =\u003e {\n  if (err)\n    throw err;\n  console.dir(rows);\n});\n\nc.end();\n```\n\n\nAPI\n===\n\n`require('pg2')` returns a **_Client_** object\n\n\nClient properties\n-----------------\n\n* **connected** - _boolean_ - `true` if the Client instance is currently connected to the server.\n\n* **backendParams** - _object_ - Once authenticated, this value will contain any backend status values. If no such values have been received, the value of this property will be `null`.\n\n* **key** - _object_ - Once authenticated, this object will contain two properties (`pid` and `key`) which is used to uniquely identify this connection on the server.\n\n* **status** - _integer_ - Indicates the backend transaction status. Valid values:\n\n    * `73` - Not in a transactional block\n    * `84` - In a transactional block\n    * `69` - In a *failed* transactional block (queries will be rejected until block is ended)\n\n\nClient events\n-------------\n\n* **ready**() - A connection to the server has been established and authentication was successful.\n\n* **error**(\u003c _Error_ \u003eerr) - An error occurred at the connection level.\n\n* **close**() - The connection has been closed.\n\n\nClient methods\n--------------\n\n* **(constructor)**([\u003c _object_ \u003econfig]) - Creates and returns a new Client instance. Valid `config` options include:\n\n    * **user** - _string_ - Username for authentication. **Default:** (OS username of current logged in user)\n\n    * **password** - _string_ - Password for authentication. **Default:** `''`\n\n    * **host** - _string_ - Hostname or IP address of the server. **Default:** `'localhost'`\n\n    * **port** - _integer_ - Port number of the server. **Default:** `5432`\n\n    * **db** - _string_ - A database to automatically select after authentication. **Default:** `''`\n    \n    * **keepalive** - _mixed_ - If `true`, this enables TCP keepalive probes using the OS default initial delay value. If a number, this both enables TCP keepalive probes and sets the initial delay value to the given value. **Default:** `true`\n\n    * **streamType** - _string_ - Set to `'normal'` to use node's full-featured streams instead of simpler streams. The main difference is that the simpler streams do not implement `.read()` support. **Default:** `'simple'`\n\n* **query**(\u003c _string_ \u003equery[, \u003c _array_ \u003evalues][, \u003c _object_ \u003eoptions][, \u003c _function_ \u003ecallback]) - _mixed_ - Enqueues the given `query`. If `callback` is not supplied, a `ResultStream` instance is returned. `values` can be an array of values that correspond to positional placeholders inside `query`. Valid `options` are:\n\n    * **arrays** - _boolean_ - When `true`, arrays are used to store row values instead of an object keyed on column names. (Note: using arrays performs better) **Default:** `false`\n\n    * **hwm** - _integer_ - This is the `highWaterMark` to use for `RowStream` instances emitted by a `ResultStream` instance. This only applies when streaming rows. **Default:** (node's default) `16`\n\n* **connect**([\u003c _function_ \u003ecallback]) - _(void)_ - Explicitly attempts to connect to the server. Note that calling `query()` will implicitly attempt a connection if one is not already made. If not connected, `callback` is added as a one-time `'ready'` event listener.\n\n* **abort**([\u003c _function_ \u003ecallback]) - _(void)_ - Aborts the currently running query. `callback` is called after the cancel request has been sent.\n\n* **end**() - _(void)_ - Closes the connection once all queries in the queue have been executed.\n\n* **destroy**() - _(void)_ - Closes the connection immediately, even if there are other queries still in the queue. Any/all queries still in the queue are properly notified.\n\n\n`ResultStream` is an object stream that emits `RowStream` instances. In the case of multiple statements/queries passed to `query()` (separated by `;`), there will be one `RowStream` instance for each statement/query.\n\n`RowStream` is an object stream that emits rows.\n\n\nTODO (in no particular order)\n====\n\n* Allow passing back of data type OIDs for columns sent by the server\n\n* COPY data support (both in and out)\n\n* SSL/TLS connection support\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmscdex%2Fpg2","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmscdex%2Fpg2","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmscdex%2Fpg2/lists"}