{"id":15573609,"url":"https://github.com/jedireza/hapi-node-postgres","last_synced_at":"2025-04-24T02:09:21.242Z","repository":{"id":23317236,"uuid":"26677107","full_name":"jedireza/hapi-node-postgres","owner":"jedireza","description":":package: Wrap hapi requests with a pg connection","archived":false,"fork":false,"pushed_at":"2017-12-07T13:51:28.000Z","size":28,"stargazers_count":31,"open_issues_count":2,"forks_count":11,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-24T02:09:15.159Z","etag":null,"topics":["hapi","javascript","nodejs","postgres"],"latest_commit_sha":null,"homepage":"","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/jedireza.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":"2014-11-15T10:54:48.000Z","updated_at":"2023-07-25T13:53:53.000Z","dependencies_parsed_at":"2022-08-31T12:22:51.600Z","dependency_job_id":null,"html_url":"https://github.com/jedireza/hapi-node-postgres","commit_stats":null,"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jedireza%2Fhapi-node-postgres","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jedireza%2Fhapi-node-postgres/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jedireza%2Fhapi-node-postgres/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jedireza%2Fhapi-node-postgres/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jedireza","download_url":"https://codeload.github.com/jedireza/hapi-node-postgres/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250546082,"owners_count":21448260,"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":["hapi","javascript","nodejs","postgres"],"created_at":"2024-10-02T18:13:43.309Z","updated_at":"2025-04-24T02:09:21.222Z","avatar_url":"https://github.com/jedireza.png","language":"JavaScript","readme":"# hapi-node-postgres\n\nWrap requests with a Postgres connection.\n\n[![Build Status](https://travis-ci.org/jedireza/hapi-node-postgres.svg?branch=master)](https://travis-ci.org/jedireza/hapi-node-postgres)\n[![Dependency Status](https://david-dm.org/jedireza/hapi-node-postgres/status.svg)](https://david-dm.org/jedireza/hapi-node-postgres)\n[![devDependency Status](https://david-dm.org/jedireza/hapi-node-postgres/dev-status.svg)](https://david-dm.org/jedireza/hapi-node-postgres?type=dev)\n[![peerDependency Status](https://david-dm.org/jedireza/hapi-node-postgres/peer-status.svg)](https://david-dm.org/jedireza/hapi-node-postgres?type=peer)\n\nWe use the [`pg`](https://github.com/brianc/node-postgres) (`node-postgres`)\nmodule and take advantage of its connection pooling feature.\n\nNote: Your project should have its own `pg` dependency installed. We depend on\n`pg` via `peerDependencies`. If you elect to use native bindings you'll also\nneed to install the `pg-native` package.\n\n\n## Install\n\n```bash\n$ npm install hapi-node-postgres\n```\n\n\n## Usage\n\n### In request handlers\n\nIn your request handlers you'll have access to `request.pg.client` which you\ncan use to make DB requests. We even clean up the connection for you after the\nrequest is complete.\n\nDuring your request handler you can set `request.pg.kill` to `true`, and we'll\nremove the connection from the pool instead of simply returning it to be\nreused. This is usually done when an error is detected during a query.\n\n```js\nserver.route({\n    method: 'GET',\n    path: '/',\n    handler: function (req, reply) {\n\n        const cmd = 'SELECT * FROM stats;';\n\n        req.pg.client.query(cmd, (err, result) =\u003e {\n\n            if (err) {\n                request.pg.kill = true;\n\n                return reply(err);\n            }\n\n            reply('Number of rows: ', result.rows.length);\n        });\n    }\n});\n```\n\n### Outside of request handlers\n\nOutside of request handlers you can get access to the Postgres `connect`\nmethod. We expose it from the plugin already bound to the connection string.\n\nAfter plugin registration is complete you'll have access through the\n[`server.plugins` api](https://hapijs.com/api#serverplugins).\n\n```js\nconst hpg = server.plugins['hapi-node-postgres'];\n\nhpg.connect((err, client, done) =\u003e {\n\n      if (err) {\n          // handle error case\n      }\n\n      // make queries with client\n\n      // call done to return client to the connection pool\n  });\n```\n\n\n## Plugin registration\n\n#### Register the plugin manually.\n\n```js\nconst plugin = {\n    register: require('hapi-node-postgres'),\n    options: {\n        connectionString: 'postgres://username:password@localhost/database',\n        native: true\n    }\n};\n\nserver.register(plugin, (err) =\u003e {\n\n    if (err) {\n        console.error('Failed loading \"hapi-node-postgres\" plugin');\n    }\n });\n```\n\n#### Or include it in your composer manifest.\n\n```json\n\"plugins\": {\n    \"hapi-node-postgres\": {\n        \"connectionString\": \"postgres://username:password@localhost/database\",\n        \"native\": true\n    }\n}\n```\n\nThe options passed to the plugin is an object where:\n\n - `connectionString` - a string representing the connection details for\n    Postgres.\n - `attach` - a string representing the extension point event where the\n    request is decorated with the `pg` attribute. Defaults to `onPreHandler`.\n - `detach` - a string representing the server event where the connection is\n    cleaned up. Defaults to `tail`.\n - `native` - a boolean indicating if the native bindings should be used.\n    Defaults to `false`. [Native bindings offer 20-30% increase in parsing\n    speed.](https://github.com/brianc/node-postgres#native-bindings)\n\n[See the hapijs docs to learn more about request lifecycle\nevents.](http://hapijs.com/api/#request-lifecycle)\n\n\n## License\n\nMIT\n\n\n## Don't forget\n\nWhat you create with `hapi-node-postgres` is more important than `hapi-node-postgres`.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjedireza%2Fhapi-node-postgres","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjedireza%2Fhapi-node-postgres","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjedireza%2Fhapi-node-postgres/lists"}