{"id":26921175,"url":"https://github.com/olyop/pg-helpers","last_synced_at":"2025-08-22T20:09:26.590Z","repository":{"id":42227430,"uuid":"332577150","full_name":"olyop/pg-helpers","owner":"olyop","description":"pg-helpers","archived":false,"fork":false,"pushed_at":"2023-02-05T03:26:33.000Z","size":134,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-29T04:41:31.198Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","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/olyop.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":"2021-01-24T23:11:54.000Z","updated_at":"2022-12-14T23:33:32.000Z","dependencies_parsed_at":"2023-02-18T21:31:20.388Z","dependency_job_id":null,"html_url":"https://github.com/olyop/pg-helpers","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/olyop%2Fpg-helpers","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/olyop%2Fpg-helpers/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/olyop%2Fpg-helpers/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/olyop%2Fpg-helpers/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/olyop","download_url":"https://codeload.github.com/olyop/pg-helpers/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246724793,"owners_count":20823544,"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":"2025-04-01T22:53:45.434Z","updated_at":"2025-04-01T22:53:45.909Z","avatar_url":"https://github.com/olyop.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# pg-helpers\n\n[![NPM Version][npm-image]][npm-url]\n[![NPM Downloads][downloads-image]][downloads-url]\n\npg-helpers is a wrapper library for the popular [node-postgres](https://node-postgres.com/) library (`pg` npm package). It adds many utilities including a `query` function that makes inserting variables and parameters a lot easier in my opinion. It does this by using a simple template mechanism in your SQL code that allows you to insert a variable or a parameter.\n\n## Installation\n\n```sh\n$ npm install @oly_op/pg-helpers\n```\n\n## Usage\n\n`@oly_op/pg-helpers` re-exports everything from `pg`.\n\n### types\n\n```typescript\nimport type { Pool, PoolClient, QueryResult as ResultBase } from \"pg\";\n\nexport type PoolOrClient = Pool | PoolClient;\nexport type Row = Record\u003cstring, unknown\u003e;\nexport type Result\u003cT = Row\u003e = ResultBase\u003cT\u003e;\n```\n\n### query helpers\n\n#### `query`\n\n```typescript\n// definition\nquery(client: PoolOrClient) =\u003e\n  (sql: string) =\u003e\n    \u003cT\u003e(input?: QueryOptions\u003cT\u003e | undefined) =\u003e\n      Promise\u003cT\u003e;\n\n// types\nexport type VariableType = string | number | boolean | null\n\nexport interface Variable {\n  key: string,\n  value: VariableType,\n  parameterized?: boolean,\n}\n\nexport type VariableInput = Variable[] | Record\u003cstring, VariableType\u003e\n\nexport type Parse\u003cT\u003e = (result: Result) =\u003e T\n\nexport interface QueryOptionsLog {\n  sql?: boolean,\n  result?: boolean,\n  variables?: boolean,\n}\n\nexport interface QueryOptions\u003cT\u003e {\n  parse?: Parse\u003cT\u003e,\n  log?: QueryOptionsLog,\n  variables?: VariableInput,\n}\n\n// example #1\ninterface User {\n  name: string,\n  userID: string,\n}\n\nconst getUser =\n  async (userID: string) =\u003e\n    query(context.pg)(`\n      SELECT\n        {{ columnNames }}\n      FROM\n        users\n      WHERE\n        user_id = '{{ userID }}'\n    `)({\n      parse: convertFirstRowToCamelCase\u003cUser\u003e(),\n      variables: {\n        userID,\n        columnNames: join([\"user_id\", \"name\"]),\n      },\n    })\n\nconsole.log((await getUser(\"1234\")).name)\n```\n\n[downloads-image]: https://img.shields.io/npm/dm/@oly_op/pg-helpers.svg\n[downloads-url]: https://npmjs.org/package/@oly_op/pg-helpers\n[npm-image]: https://img.shields.io/npm/v/@oly_op/pg-helpers.svg\n[npm-url]: https://npmjs.org/package/@oly_op/pg-helpers\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Folyop%2Fpg-helpers","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Folyop%2Fpg-helpers","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Folyop%2Fpg-helpers/lists"}