{"id":18070141,"url":"https://github.com/polatengin/andorra","last_synced_at":"2025-04-05T16:41:34.874Z","repository":{"id":144741569,"uuid":"455671507","full_name":"polatengin/andorra","owner":"polatengin","description":"A TypeScript utility to tag and differentiate ID fields across data models, ensuring type-safe usage and preventing mismatches in large projects.","archived":false,"fork":false,"pushed_at":"2022-02-05T19:16:25.000Z","size":2,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-11T13:47:53.184Z","etag":null,"topics":["data-model","extends","interface","type","type-safety","typescript"],"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/polatengin.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":"2022-02-04T19:28:22.000Z","updated_at":"2024-12-28T00:24:38.000Z","dependencies_parsed_at":null,"dependency_job_id":"d5d7ce9d-d27d-4c1b-a08c-d7366944c4de","html_url":"https://github.com/polatengin/andorra","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/polatengin%2Fandorra","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/polatengin%2Fandorra/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/polatengin%2Fandorra/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/polatengin%2Fandorra/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/polatengin","download_url":"https://codeload.github.com/polatengin/andorra/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247369927,"owners_count":20927927,"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":["data-model","extends","interface","type","type-safety","typescript"],"created_at":"2024-10-31T08:24:13.731Z","updated_at":"2025-04-05T16:41:34.818Z","avatar_url":"https://github.com/polatengin.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Andorra\n\nIn large projects, we usually have way more than one data model.\n\nExcept very rare situations, in every data model, we have an ID field.\n\nFor example;\n\n```typescript\nexport interface User {\n  id: number;\n  name: string;\n  email: string;\n  password: string;\n}\n\nexport interface Product {\n  id: number;\n  name: string;\n  price: number;\n  description: string;\n}\n```\n\nIf a function returns a `User` or `Product`, we can use the `ID` to find the data in the database.\n\nThat means, seperate functions can be used to find a `User` or `Product` by their `ID`s.\n\nBut, what if a developer pass `ID` of a product to a function that expects an `ID` of a `User`?\n\nMore generally, how can we differentiate `ID` of a `User` from `ID` of a `Product`?\n\nIn the [index.ts](./index.ts) file, we have a type to differentiate `ID` fields of different data models.\n\n```typescript\ntype ID\u003cT, Tag extends string\u003e = T \u0026 { __id: Tag };\n```\n\nBasically, we `tag` different `ID` fields and make sure the expected `ID` field is tagged with the correct `tag`.\n\n```typescript\ntype UserId = ID\u003cnumber, 'UserId'\u003e;\n\ntype ProductId = ID\u003cnumber, 'ProductId'\u003e;\n```\n\nIn the below example, we have seperate functions that return `User` and `Product` objects, from different `ID`s.\n\n```typescript\nconst getUser = (id: UserId) =\u003e {\n  return {\n    id,\n    name: 'Engin Polat 🧑‍💻',\n  };\n};\n\nconst getProduct = (id: ProductId) =\u003e {\n  return {\n    id,\n    name: 'Surface Book 3 Pro 💻',\n  };\n};\n```\n\nWe can call these functions ONLY by passing correct `ID` to them.\n\n```typescript\ngetUser(5 as UserId);\nconst userId = 5 as UserId;\ngetUser(userId);\n```\n\nEither it should be a `UserId` or a `number` that explicitely casted to `UserId`.\n\n```typescript\ngetProduct(8 as ProductId);\nconst productId = 8 as ProductId;\ngetProduct(productId);\n```\n\nIt's the same with `ProductId` field in the above example.\n\nGood part is, it's not possible to call `getUser` with a `ProductId` or `getProduct` with a `UserId`.\n\n```typescript\n// We can't even compile the below code\ngetUser(productId);\ngetProduct(userId);\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpolatengin%2Fandorra","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpolatengin%2Fandorra","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpolatengin%2Fandorra/lists"}