{"id":19805337,"url":"https://github.com/mzalevski/postgrest-js-tools","last_synced_at":"2025-05-01T06:31:13.129Z","repository":{"id":44562095,"uuid":"432677592","full_name":"mzalevski/postgrest-js-tools","owner":"mzalevski","description":"Tiny tools library for @supabase/supabase-js","archived":false,"fork":false,"pushed_at":"2022-09-08T07:44:07.000Z","size":302,"stargazers_count":26,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-03-14T16:43:37.835Z","etag":null,"topics":["postgrest","supabase"],"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/mzalevski.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}},"created_at":"2021-11-28T10:10:26.000Z","updated_at":"2023-12-01T05:13:38.000Z","dependencies_parsed_at":"2022-09-20T22:55:53.516Z","dependency_job_id":null,"html_url":"https://github.com/mzalevski/postgrest-js-tools","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mzalevski%2Fpostgrest-js-tools","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mzalevski%2Fpostgrest-js-tools/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mzalevski%2Fpostgrest-js-tools/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mzalevski%2Fpostgrest-js-tools/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mzalevski","download_url":"https://codeload.github.com/mzalevski/postgrest-js-tools/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224062785,"owners_count":17249289,"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":["postgrest","supabase"],"created_at":"2024-11-12T09:03:27.052Z","updated_at":"2024-11-12T09:03:46.246Z","avatar_url":"https://github.com/mzalevski.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# `postgrest-js-tools`\n\n[![Package](https://img.shields.io/npm/v/postgrest-js-tools)](https://www.npmjs.com/package/postgrest-js-tools)\n[![License: ISC](https://img.shields.io/badge/License-ISC-blue.svg)](https://opensource.org/licenses/ISC)\n\nTiny tools library for [@supabase/supabase-js](https://github.com/supabase/supabase-js).\n\n### TLDR\n\n```ts\ntype User = { id: string; email: string };\nconst shape = getShape\u003cUser\u003e()({ id: true }); // with intellisense!\n\n// typeof shape =\u003e { id: string; }\n// getFields(shape) =\u003e \"id\"\n\nconst result = await supabase\n  .from\u003ctypeof shape\u003e(\"users\")\n  .select(getFields(shape));\n\n// typeof result =\u003e PostgrestResponse\u003c{ id: string; }\u003e\n```\n\n### Features:\n\n1. intellisense while selecting fields (`getShape`)\n2. auto generated select string (`getFields`)\n3. return type based on the selected fields (`typeof shape`)\n4. 1:1, 1:m \u0026 n:m relations support\n\n### Quick start\n\nInstallation\n\n```bash\nnpm i postgrest-js-tools\n```\n\n```bash\nyarn add postgrest-js-tools\n```\n\n```bash\npnpm i postgrest-js-tools\n```\n\nImports\n\nNode\n\n```ts\nimport { getShape, getFields } from \"postgrest-js-tools\";\n```\n\nDeno\n\n```ts\nimport {\n  getShape,\n  getFields,\n} from \"https://cdn.skypack.dev/postgrest-js-tools?dts\";\n```\n\nUsage\n\nClick [here](https://supabase.com/docs/reference/javascript/generating-types#generate-database-types-from-openapi-specification) to read about how to generate database types. These are the `definitions` from the example below.\n\nLet's assume we work on a following data structure:\n\n- Each **University** has many **Scholars**\n- Each **Scholar** has many **Papers**\n\n```ts\nimport { definitions } from \"path/to/types\";\n\ntype UniversityBase = definitions[\"universities\"]; // { id: string; name: string }\ntype ScholarBase = definitions[\"scholars\"]; // { id: string; email: string; university_id: string }\ntype PaperBase = definitions[\"papers\"]; // { id: string; title: string; scholar_id: string }\n\ntype University = UniversityBase \u0026 {\n  scholars: Scholar[];\n};\n\ntype Scholar = ScholarBase \u0026 {\n  university: University;\n  papers: Paper[];\n};\n\ntype Paper = PaperBase \u0026 {\n  scholar: Scholar;\n};\n\n// with intellisense!\nconst shape = getShape\u003cScholar\u003e()({\n  id: true,\n  email: false, // if value === false then we skip the key\n  university: { _: \"university_id\", name: true }, // _ is the key to join by\n  papers: [{ \"*\": true }], // * selects all the fields\n});\n\n// typeof shape =\u003e { id: string; university: { name: string }; papers: { id: string; title: string; scholar_id: string }[] }\n// getFields(shape) =\u003e \"id,university:university_id(name),papers(*)\"\n\nconst result = await supabase\n  .from\u003ctypeof shape\u003e(\"scholars\")\n  .select(getFields(shape));\n\n// typeof result =\u003e PostgrestResponse\u003c{ id: string; university: { name: string }; papers: { id: string, title: string; scholar_id: string }[] }\u003e\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmzalevski%2Fpostgrest-js-tools","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmzalevski%2Fpostgrest-js-tools","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmzalevski%2Fpostgrest-js-tools/lists"}