{"id":16410628,"url":"https://github.com/acro5piano/knex-graphql-utils","last_synced_at":"2026-02-17T04:31:14.269Z","repository":{"id":48452934,"uuid":"389036885","full_name":"acro5piano/knex-graphql-utils","owner":"acro5piano","description":"Set of useful functions for Knex + GraphQL - Batch Loading, Column reducer","archived":false,"fork":false,"pushed_at":"2022-01-20T13:28:50.000Z","size":853,"stargazers_count":18,"open_issues_count":2,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-10-03T14:48:30.188Z","etag":null,"topics":["graphql","knex","postgresql","typescript"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/knex-graphql-utils","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/acro5piano.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-07-24T07:56:41.000Z","updated_at":"2023-10-05T15:56:23.000Z","dependencies_parsed_at":"2022-08-28T03:43:38.725Z","dependency_job_id":null,"html_url":"https://github.com/acro5piano/knex-graphql-utils","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/acro5piano/knex-graphql-utils","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/acro5piano%2Fknex-graphql-utils","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/acro5piano%2Fknex-graphql-utils/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/acro5piano%2Fknex-graphql-utils/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/acro5piano%2Fknex-graphql-utils/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/acro5piano","download_url":"https://codeload.github.com/acro5piano/knex-graphql-utils/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/acro5piano%2Fknex-graphql-utils/sbom","scorecard":{"id":163363,"data":{"date":"2025-08-11","repo":{"name":"github.com/acro5piano/knex-graphql-utils","commit":"a918acba64e5b2ffe6ae29d4b232dbf2e864086d"},"scorecard":{"version":"v5.2.1-40-gf6ed084d","commit":"f6ed084d17c9236477efd66e5b258b9d4cc7b389"},"score":2.5,"checks":[{"name":"Packaging","score":-1,"reason":"packaging workflow not detected","details":["Warn: no GitHub/GitLab publishing workflow detected."],"documentation":{"short":"Determines if the project is published as a package that others can easily download, install, easily update, and uninstall.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#packaging"}},{"name":"Code-Review","score":0,"reason":"Found 0/22 approved changesets -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project requires human code review before pull requests (aka merge requests) are merged.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#code-review"}},{"name":"Maintained","score":0,"reason":"0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project is \"actively maintained\".","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#maintained"}},{"name":"Binary-Artifacts","score":10,"reason":"no binaries found in the repo","details":null,"documentation":{"short":"Determines if the project has generated executable (binary) artifacts in the source repository.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#binary-artifacts"}},{"name":"Dangerous-Workflow","score":10,"reason":"no dangerous workflow patterns detected","details":null,"documentation":{"short":"Determines if the project's GitHub Action workflows avoid dangerous patterns.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#dangerous-workflow"}},{"name":"Token-Permissions","score":0,"reason":"detected GitHub workflow tokens with excessive permissions","details":["Warn: no topLevel permission defined: .github/workflows/release.yml:1","Warn: no topLevel permission defined: .github/workflows/test.yml:1","Info: no jobLevel write permissions found"],"documentation":{"short":"Determines if the project's workflows follow the principle of least privilege.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#token-permissions"}},{"name":"Pinned-Dependencies","score":0,"reason":"dependency not pinned by hash detected -- score normalized to 0","details":["Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/release.yml:11: update your workflow using https://app.stepsecurity.io/secureworkflow/acro5piano/knex-graphql-utils/release.yml/master?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/release.yml:12: update your workflow using https://app.stepsecurity.io/secureworkflow/acro5piano/knex-graphql-utils/release.yml/master?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/release.yml:15: update your workflow using https://app.stepsecurity.io/secureworkflow/acro5piano/knex-graphql-utils/release.yml/master?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/test.yml:10: update your workflow using https://app.stepsecurity.io/secureworkflow/acro5piano/knex-graphql-utils/test.yml/master?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/test.yml:11: update your workflow using https://app.stepsecurity.io/secureworkflow/acro5piano/knex-graphql-utils/test.yml/master?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/test.yml:14: update your workflow using https://app.stepsecurity.io/secureworkflow/acro5piano/knex-graphql-utils/test.yml/master?enable=pin","Info:   0 out of   6 GitHub-owned GitHubAction dependencies pinned"],"documentation":{"short":"Determines if the project has declared and pinned the dependencies of its build process.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#pinned-dependencies"}},{"name":"CII-Best-Practices","score":0,"reason":"no effort to earn an OpenSSF best practices badge detected","details":null,"documentation":{"short":"Determines if the project has an OpenSSF (formerly CII) Best Practices Badge.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#cii-best-practices"}},{"name":"Security-Policy","score":0,"reason":"security policy file not detected","details":["Warn: no security policy file detected","Warn: no security file to analyze","Warn: no security file to analyze","Warn: no security file to analyze"],"documentation":{"short":"Determines if the project has published a security policy.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#security-policy"}},{"name":"Fuzzing","score":0,"reason":"project is not fuzzed","details":["Warn: no fuzzer integrations found"],"documentation":{"short":"Determines if the project uses fuzzing.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#fuzzing"}},{"name":"License","score":10,"reason":"license file detected","details":["Info: project has a license file: LICENSE:0","Info: FSF or OSI recognized license: MIT License: LICENSE:0"],"documentation":{"short":"Determines if the project has defined a license.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#license"}},{"name":"Signed-Releases","score":-1,"reason":"no releases found","details":null,"documentation":{"short":"Determines if the project cryptographically signs release artifacts.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#signed-releases"}},{"name":"Branch-Protection","score":0,"reason":"branch protection not enabled on development/release branches","details":["Warn: branch protection not enabled for branch 'master'"],"documentation":{"short":"Determines if the default and release branches are protected with GitHub's branch protection settings.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#branch-protection"}},{"name":"SAST","score":0,"reason":"SAST tool is not run on all commits -- score normalized to 0","details":["Warn: 0 commits out of 10 are checked with a SAST tool"],"documentation":{"short":"Determines if the project uses static code analysis.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#sast"}},{"name":"Vulnerabilities","score":0,"reason":"45 existing vulnerabilities detected","details":["Warn: Project is vulnerable to: GHSA-v6h2-p8h4-qcjw","Warn: Project is vulnerable to: GHSA-pxg6-pf52-xh8x","Warn: Project is vulnerable to: GHSA-67mh-4wv8-2f99","Warn: Project is vulnerable to: GHSA-3fjj-p79j-c9hh","Warn: Project is vulnerable to: GHSA-p6vg-p826-qp3v","Warn: Project is vulnerable to: GHSA-4pcg-wr6c-h9cq","Warn: Project is vulnerable to: GHSA-fjxv-7rqg-78g4","Warn: Project is vulnerable to: GHSA-896r-f27r-55mw","Warn: Project is vulnerable to: GHSA-4jv9-3563-23j3","Warn: Project is vulnerable to: GHSA-cm8h-q92v-xcfc","Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3","Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw","Warn: Project is vulnerable to: GHSA-m6fv-jmcg-4jfg","Warn: Project is vulnerable to: GHSA-3cvr-822r-rqcc","Warn: Project is vulnerable to: GHSA-q768-x9m6-m9qp","Warn: Project is vulnerable to: GHSA-8qr4-xgw6-wmr3","Warn: Project is vulnerable to: GHSA-f772-66g8-q5h3","Warn: Project is vulnerable to: GHSA-5r9g-qh6m-jxff","Warn: Project is vulnerable to: GHSA-r6ch-mqf9-qc9w","Warn: Project is vulnerable to: GHSA-wqq4-5wpv-mx2g","Warn: Project is vulnerable to: GHSA-3787-6prv-h9w3","Warn: Project is vulnerable to: GHSA-9qxr-qj54-h672","Warn: Project is vulnerable to: GHSA-m4v8-wqvr-p9f7","Warn: Project is vulnerable to: GHSA-cxrh-j4jr-qwg3","Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q","Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw","Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg","Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275","Warn: Project is vulnerable to: GHSA-pfrx-2q88-qq97","Warn: Project is vulnerable to: GHSA-rc47-6667-2j5j","Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv","Warn: Project is vulnerable to: GHSA-xvch-5gv4-984h","Warn: Project is vulnerable to: GHSA-hrpp-h998-j3pp","Warn: Project is vulnerable to: GHSA-p8p7-x288-28g6","Warn: Project is vulnerable to: GHSA-g4rg-993r-mgx7","Warn: Project is vulnerable to: GHSA-9qrh-qjmc-5w2p","Warn: Project is vulnerable to: GHSA-jqv5-7xpx-qj74","Warn: Project is vulnerable to: GHSA-3jfq-g458-7qm9","Warn: Project is vulnerable to: GHSA-5955-9wpr-37jh","Warn: Project is vulnerable to: GHSA-f5x3-32g6-xq36","Warn: Project is vulnerable to: GHSA-r628-mhmh-qjhw","Warn: Project is vulnerable to: GHSA-9r2w-394v-53qc","Warn: Project is vulnerable to: GHSA-qq89-hq3f-393p","Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3","Warn: Project is vulnerable to: GHSA-38fc-wpqx-33j7"],"documentation":{"short":"Determines if the project has open, known unfixed vulnerabilities.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#vulnerabilities"}}]},"last_synced_at":"2025-08-16T14:12:08.658Z","repository_id":48452934,"created_at":"2025-08-16T14:12:08.658Z","updated_at":"2025-08-16T14:12:08.658Z"},"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29533702,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-17T03:01:11.216Z","status":"ssl_error","status_checked_at":"2026-02-17T03:00:31.803Z","response_time":100,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["graphql","knex","postgresql","typescript"],"created_at":"2024-10-11T06:43:27.321Z","updated_at":"2026-02-17T04:31:14.251Z","avatar_url":"https://github.com/acro5piano.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# knex-graphql-utils\n\n[![npm version](https://badge.fury.io/js/knex-graphql-utils.svg)](https://badge.fury.io/js/knex-graphql-utils)\n[![test](https://github.com/acro5piano/knex-graphql-utils/actions/workflows/test.yml/badge.svg)](https://github.com/acro5piano/knex-graphql-utils/actions/workflows/test.yml)\n[![release](https://github.com/acro5piano/knex-graphql-utils/actions/workflows/release.yml/badge.svg)](https://github.com/acro5piano/knex-graphql-utils/actions/workflows/release.yml)\n\nSet of useful functions for Knex + GraphQL.\n\n- **BatchLoader** Loads and paginates relationship without N+1 problem.\n- **SelectionFilter** Filters selected columns based on GraphQL field selection set.\n\n**Note: Only PostgreSQL is supported for now**\n\n# tl;dr\n\n### Before\n\n![image](https://user-images.githubusercontent.com/10719495/126906832-f991dd7d-b563-4fa2-9313-738f476d46c8.png)\n\n### After\n\n![image](https://user-images.githubusercontent.com/10719495/126906862-71b456bd-82da-46d1-b6d3-acd1c3233885.png)\n\n# Motivation\n\nCreating a GraphQL service with a Relational Database is a hard thing. We should take care of:\n\n- Performance for querying relations. N+1 problem will happen if you don't use Dataloader.\n- `select *` make your server slow, but hard to filter columns based on requests.\n- Pagination. [Dataloader pattern is hard to implement pagination](https://github.com/graphql/dataloader/issues/231) without a hacky `union` or window functions. `knex-graphql-utils` uses `row_number()` window function to do it.\n\nWith `knex-graphql-utils`, You can build performant GraphQL servers without hassle.\n\n# Install\n\n```\nnpm install --save knex-graphql-utils\n```\n\nOr if you use Yarn:\n\n```\nyarn add knex-graphql-utils\n```\n\n# Getting Started\n\nIn this example, I use `mercurius` but this can be applied to any GraphQL frameworks.\n\n```typescript\n// app.ts\n\nimport Fastify from 'fastify'\nimport mercurius from 'mercurius'\nimport { BatchLoader, SelectionFilter } from 'knex-graphql-utils'\n\nimport { knex } from './knex' // Your knex instance\n\nconst app = Fastify()\n\nconst schema = `\n  type Query {\n    user: User\n  }\n  type User {\n    id: ID!\n    name: String!\n    posts(page: Int!): [Post!]!\n  }\n  type Post {\n    id: ID!\n    title: String!\n    user: User!\n  }\n`\n\nconst selectionFilter = new SelectionFilter(knex)\n\nconst resolvers = {\n  Query: {\n    user: (user, args, ctx, info) =\u003e\n      knex('users')\n        .select(selectionFilter.reduce({ info, table: 'users' }))\n        .first(),\n  },\n  User: {\n    posts: (user, args, ctx) =\u003e\n      ctx.batchLoader\n        .getLoader({\n          type: 'hasMany',\n          foreignKey: 'userId',\n          targetTable: 'posts',\n          page: {\n            offset: ((args.page || 1) - 1) * 10,\n            limit: 10,\n          },\n          orderBy: ['createdAt', 'asc'],\n          queryModifier: (query) =\u003e {\n            query.select(selectionFilter.reduce({ info, table: 'posts' }))\n          },\n        })\n        .load(user.id),\n  },\n  Post: {\n    user: (post, _args, ctx, info) =\u003e\n      ctx.batchLoader\n        .getLoader({\n          type: 'belongsTo',\n          foreignKey: 'userId',\n          targetTable: 'users',\n          queryModifier: (query) =\u003e {\n            query.select(selectionFilter.reduce({ info, table: 'users' }))\n          },\n        })\n        .load(post.userId),\n  },\n}\n\napp.register(mercurius, {\n  schema,\n  resolvers,\n  context: () =\u003e ({\n    batchLoader: new BatchLoader(knex),\n  }),\n})\n\napp.listen(8877).then(async () =\u003e {\n  await selectionFilter.prepare(['users', 'posts'], /(_id)|(Id)$/)\n})\n```\n\n# Integrate `BatchLoader` and `SelectionFilter`\n\nYou can tell `BatchLoader` to use `SelectionFilter`, and the loader automatically reduces the selection based on `info`.\n\n```typescript\nimport { BatchLoader, SelectionFilter } from 'knex-graphql-utils'\n\nimport { knex } from './knex' // Your knex instance\n\nconst selectionFilter = new SelectionFilter(knex)\nconst context = {\n  batchLoader: new BatchLoader(knex).useSelectionFilter(selectionFilter), // Attach SelectionFilter into batch loader\n}\n\nconst resolver = {\n  User: {\n    posts: (user, args, ctx, info) =\u003e\n      ctx.batchLoader\n        .getLoader({\n          type: 'hasMany',\n          foreignKey: 'userId',\n          targetTable: 'posts',\n          info, // By passing `info`, loader automatically reduces the selection\n        })\n        .load(user.id),\n  },\n}\n\nawait selectionFilter.prepare(['users'], /_id/)\n```\n\n# Limitation \u0026 Todo\n\n- Only PostgreSQL is supported for now.\n- Primary key is always assumed to be `id`.\n- Add `where` clause with paginating relationship.\n\n# Further reading\n\nFor more details, please visit [demo](https://github.com/acro5piano/knex-graphql-utils/blob/master/demo/index.ts) and [unit tests](https://github.com/acro5piano/knex-graphql-utils/blob/master/tests/BatchLoader.test.ts).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Facro5piano%2Fknex-graphql-utils","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Facro5piano%2Fknex-graphql-utils","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Facro5piano%2Fknex-graphql-utils/lists"}