{"id":21884210,"url":"https://github.com/timbo-rafa/type-safe-prisma-query","last_synced_at":"2026-05-07T00:36:33.906Z","repository":{"id":263515070,"uuid":"890597065","full_name":"timbo-rafa/type-safe-prisma-query","owner":"timbo-rafa","description":null,"archived":false,"fork":false,"pushed_at":"2024-11-19T00:25:42.000Z","size":346,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-08-22T11:03:00.715Z","etag":null,"topics":["postgresql","prisma-orm","problem","typescript","workaround"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/timbo-rafa.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-11-18T21:09:23.000Z","updated_at":"2024-11-19T00:25:45.000Z","dependencies_parsed_at":"2024-11-19T01:18:45.897Z","dependency_job_id":"0c214eee-f370-4241-96c4-f7697e03c1f7","html_url":"https://github.com/timbo-rafa/type-safe-prisma-query","commit_stats":null,"previous_names":["timbo-rafa/type-safe-prisma-query"],"tags_count":0,"template":false,"template_full_name":"timbo-rafa/nestjs-template","purl":"pkg:github/timbo-rafa/type-safe-prisma-query","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timbo-rafa%2Ftype-safe-prisma-query","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timbo-rafa%2Ftype-safe-prisma-query/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timbo-rafa%2Ftype-safe-prisma-query/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timbo-rafa%2Ftype-safe-prisma-query/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/timbo-rafa","download_url":"https://codeload.github.com/timbo-rafa/type-safe-prisma-query/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timbo-rafa%2Ftype-safe-prisma-query/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":271628166,"owners_count":24792821,"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","status":"online","status_checked_at":"2025-08-22T02:00:08.480Z","response_time":65,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":["postgresql","prisma-orm","problem","typescript","workaround"],"created_at":"2024-11-28T10:12:44.440Z","updated_at":"2026-05-07T00:36:33.869Z","avatar_url":"https://github.com/timbo-rafa.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Problem Statement\n\nORMs often struggle to enforce type-safety to queries with partial data and joins.\nThis repository contains a small code sample on how to achieve that with Prisma ORM.\n\nOn our [repository example](/src/posts/broken-post.repository.ts),\nwe try to select only the email field from the user table, along only with their posts' title. Prisma currently does not allow us to do that and errors out:\n\n\u003cimg src=\"/screenshots/prisma-query-error.png\"\u003e\n\nSimilarly, if we try Prisma validators as suggested on the post [Operating against partial structures](https://www.prisma.io/docs/orm/prisma-client/type-safety/operating-against-partial-structures-of-model-types), prisma seems to join with the User table twice, one correctly hiding the \"name\" field, but the second joins the complete table incorrectly:\n\n\u003cimg src=\"/screenshots/prisma-wrong-join.png\"\u003e\n\n# Solution\n\nIn order to solve these problematic joins, we can generate one type per table and merge them in Typescript, as shown in [post.entities.ts](/src/posts/post.entities.ts).\n\nSince we will do that multiple times across different features, we can extract this type logic into a generic for reusability as is in [posts-using-generics.entities.ts](/src/posts/post-using-generics.entities.ts)\n\nIt's correctness can be seen by type errors as below:\n\n\u003cimg src=\"/screenshots/prisma-partial-types.png\"\u003e\n\nWe can then use this type on query results returned by prisma.\nHowever, since the typed query does not allow `select` and `include`, we may need to resort to [prisma.$queryRaw](https://www.prisma.io/docs/orm/prisma-client/using-raw-sql/raw-queries#queryraw),\nin which case the raw sql will **not** be type-safe. You may want to consider tools like [SafeQL](https://safeql.dev/compatibility/prisma.html),\nor migrating to Prisma 5.19.0+, so you can use Prisma's [TypedSQL](https://www.prisma.io/docs/orm/prisma-client/using-raw-sql/typedsql)\n(in preview at time of writing)\n\n# Try it yourself\n\nAfter cloning repository:\n\n```bash\nnpm install\nprisma generate\n```\n\nThen go to [posts-using-generics.entities.ts](/src/posts/post-using-generics.entities.ts) to see the type errors in your editor\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftimbo-rafa%2Ftype-safe-prisma-query","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftimbo-rafa%2Ftype-safe-prisma-query","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftimbo-rafa%2Ftype-safe-prisma-query/lists"}