{"id":13799921,"url":"https://github.com/chilts/koa-pg","last_synced_at":"2025-10-02T01:31:36.370Z","repository":{"id":12872677,"uuid":"15549062","full_name":"chilts/koa-pg","owner":"chilts","description":"Koa middleware to get you a Postgres client.","archived":true,"fork":false,"pushed_at":"2016-09-01T11:35:38.000Z","size":17,"stargazers_count":29,"open_issues_count":2,"forks_count":9,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-01-21T00:53:18.484Z","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":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/chilts.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":"2013-12-31T11:48:33.000Z","updated_at":"2023-08-25T17:44:47.000Z","dependencies_parsed_at":"2022-08-26T06:41:12.092Z","dependency_job_id":null,"html_url":"https://github.com/chilts/koa-pg","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chilts%2Fkoa-pg","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chilts%2Fkoa-pg/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chilts%2Fkoa-pg/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chilts%2Fkoa-pg/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chilts","download_url":"https://codeload.github.com/chilts/koa-pg/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":234922825,"owners_count":18907828,"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-08-04T00:01:07.198Z","updated_at":"2025-10-02T01:31:31.081Z","avatar_url":"https://github.com/chilts.png","language":"JavaScript","readme":"Note: This project is in need of an owner or maintainer since I don't use it. I fear my pulling of PRs isn't making it better since it requires more of an overview by someone. Please get in touch. :) Thanks.\n\n# koa-pg #\n\nKoa middleware to get you a Postgres client on the way down the middlewares, and release it on the way back up the\nmiddlewares.\n\nThis extends Koa by adding a `this.pg` object to it. This contains the `this.pg.client` and `this.pg.done`. You should\nuse `this.pg.db.client` to yield to the `.query_()` method (from [co-pg](https://npmjs.org/package/co-pg)) but you should\nnot call `this.pg.db.done()` since that is used internally by `koa-pg` when going back up the middlewares.\n\n## Synopsis ##\n\n```js\nvar koa = require('koa')\nvar koaPg = require('koa-pg')\n\nvar app = koa()\n\napp.use(koaPg('postgres://user:password@localhost:5432/database'))\n\napp.use(function *(next) {\n    // Here we have access to this.pg.db.client which is client returned from pg.connect().\n    var result = yield this.pg.db.client.query_('SELECT now()')\n    console.log('result:', result)\n\n    this.body = result.rows[0].now.toISOString()\n})\n\napp.listen(3000)\n```\n\n## Options ##\n\n* 'name' : default -\u003e 'db'\n\nThis is the name you want to use for this database. The default is `db` which means the client is\nat `this.pg.db.client`. If you only have one database connection you may want to just leave this as\nthe default. See below if you require two or more database connections (such as `master` or `slave`).\n\n```\napp.use(koaPg({\n    name   : 'master',\n    pg : 'postgres://user:password@localhost:5432/database'\n}))\n\napp.use(function *(next) {\n    var result = yield this.pg.master.client.query_('SELECT now()')\n    this.body = result.rows[0].now.toISOString()\n})\n```\n\n* 'pg'\n\nThis is the parameter that is passed via `co-pg` to `node-postgres`. It can be\neither a string or an object and conforms to the API as provided by the aforementioned modules.\n\n```js\nvar config = {\n    name: 'master_db',\n    pg: {\n        user: 'postgres',\n        database: 'db',\n        password: 'pass',\n        port: 5432,\n        max: 10,\n        idleTimeoutMillis: 60\n    }\n}\n\napp.use(koaPg(config))\n```\n\n## Multiple Database Connections ##\n\nWhen using only one database connection you can use the default `db` name, but if you need more than one\nconnection you must name at least one or other (or both) to something different. Let's say you have a master\nand a slave and you require a client at all times (in reality you'd probably just connect to one or the other\ndepending on what operations you are doing).\n\n```\napp.use(koaPg({\n    name   : 'master',\n    pg : 'postgres://user:password@masterhost:5432/database'\n}))\n\napp.use(koaPg({\n    name   : 'slave',\n    pg : 'postgres://user:password@slavehost:5432/database'\n}))\n\n// a write query\napp.use(function *(next) {\n    var result = yield this.pg.master.client.query_('SELECT now()')\n    // ...\n})\n\n// a read query\napp.use(function *(next) {\n    var result = yield this.pg.slave.client.query_('SELECT now()')\n    // ...\n})\n```\n\n## Author ##\n\nWritten by [Andrew Chilton](http://chilts.org/) - [Blog](http://chilts.org/blog/) -\n[Twitter](https://twitter.com/andychilton).\n\n(Ends)\n","funding_links":[],"categories":["仓库"],"sub_categories":["中间件"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchilts%2Fkoa-pg","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchilts%2Fkoa-pg","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchilts%2Fkoa-pg/lists"}