{"id":23346800,"url":"https://github.com/fredericoo/drizzle-toolbelt","last_synced_at":"2025-04-10T15:26:34.589Z","repository":{"id":236501392,"uuid":"792733548","full_name":"fredericoo/drizzle-toolbelt","owner":"fredericoo","description":null,"archived":false,"fork":false,"pushed_at":"2024-07-12T10:14:27.000Z","size":92,"stargazers_count":39,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-04-04T12:58:37.673Z","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":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fredericoo.png","metadata":{"files":{"readme":"readme.md","changelog":"CHANGELOG.md","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-04-27T12:24:55.000Z","updated_at":"2025-02-27T08:17:56.000Z","dependencies_parsed_at":"2024-07-12T11:52:35.509Z","dependency_job_id":null,"html_url":"https://github.com/fredericoo/drizzle-toolbelt","commit_stats":null,"previous_names":["fredericoo/drizzle-toolbelt"],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fredericoo%2Fdrizzle-toolbelt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fredericoo%2Fdrizzle-toolbelt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fredericoo%2Fdrizzle-toolbelt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fredericoo%2Fdrizzle-toolbelt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fredericoo","download_url":"https://codeload.github.com/fredericoo/drizzle-toolbelt/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248243445,"owners_count":21071054,"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":"2024-12-21T07:15:30.457Z","updated_at":"2025-04-10T15:26:34.563Z","avatar_url":"https://github.com/fredericoo.png","language":"TypeScript","funding_links":[],"categories":["Libraries"],"sub_categories":[],"readme":"# Drizzle Toolbelt\n\nSet of tools for [drizzle-orm](https://github.com/drizzle-team/drizzle-orm).\n\n## Installation\n\n```bash\nnpm i drizzle-toolbelt\n```\n\n## Table of Contents\n\n- [Drizzle Toolbelt](#drizzle-toolbelt)\n  - [Installation](#installation)\n  - [Table of Contents](#table-of-contents)\n  - [Usage](#usage)\n    - [`takeFirst`](#takefirst)\n    - [`takeFirstOrThrow`](#takefirstorthrow)\n    - [`aggregate`](#aggregate)\n      - [Data-last](#data-last)\n      - [Data-first](#data-first)\n    - [`transform`](#transform)\n      - [Data-last](#data-last-1)\n      - [Data-first](#data-first-1)\n  - [Contributing](#contributing)\n  - [License](#license)\n\n## Usage\n\n### `takeFirst`\n\nTakes first element from array if it exists.\n\n```ts\nimport { db } from \"./db\";\nimport { users } from \"./schema\";\nimport { takeFirst } from \"drizzle-toolbelt\";\n\n\nconst firstUserOrUndefined = await db.select().from(users)\n .then(takeFirst);\n ```\n\n---\n\n### `takeFirstOrThrow`\n\nTakes first element from array or throws if length is zero.\n\n**With default error**\n\n```ts\nimport { db } from \"./db\";\nimport { users } from \"./schema\";\nimport { takeFirstOrThrow } from \"drizzle-toolbelt\";\n\nconst firstUserOrThrowDefault = await db\n  .select()\n  .from(users)\n  // throw inline\n  .then(takeFirstOrThrow());\n\n// or\n\n// prepare errors\nconst takeFirstOrError = takeFirstOrThrow();\nconst takeFirstOrUnauthorized = takeFirstOrThrow(new UnauthorizedError(\"You cannot view this page.\"));\n\n// and pass it\nconst firstUserOrThrow = await db\n  .select()\n  .from(users)\n  .then(takeFirstOrError);\n\nconst firstUserOrUnauthorized = await db\n  .select()\n  .from(users)\n  .then(takeFirstOrUnauthorized);\n```\n\n---\n\n### `aggregate`\n\nAggregates rows from a query.\n\n#### Data-last\n\nExample:\n```ts\nimport { db } from \"./db\";\nimport { users } from \"./schema\";\nimport { aggregate } from \"drizzle-toolbelt\";\n\nconst usersWithPosts = await db.select({\n  id: users.id,\n  post: posts\n}).from(users)\n.leftJoin(posts.id, eq(posts.author_id, users.id))\n.then(aggregate({ pkey: 'id', fields: { posts: 'post.id' } }));\n ```\n\nThis will:\n- group all users by their id (`pkey`)\n- remove the `post` property from the rows\n- aggregate all posts in the property `posts` (set in `fields`) by their id (`posts.id`)\n\nThe final type will be:\n\n```ts\ntype Aggregated = {\n  id: number;\n  // `post` was removed\n  // `posts` was added as an array of `post`\n  posts: { id: number, …otherPostProperties }[]; \n}\n```\n\n#### Data-first\n\nYou may choose to pass rows in the arguments to call `aggregate` in a stand-alone way.\n\n```ts\nimport { db } from \"./db\";\nimport { users } from \"./schema\";\nimport { aggregate } from \"drizzle-toolbelt\";\n\nconst usersRows = await db.select({\n  id: users.id,\n  post: posts\n}).from(users)\n.leftJoin(posts.id, eq(posts.author_id, users.id))\n\nconst usersWithPosts = aggregate({ rows, pkey: 'id', fields: { posts: 'post.id' }});\n ```\n\n---\n\n### `transform`\n\nTransforms rows in a slightly more performant way than spreading the properties (in-place changes). Data-first and data-last overloads are available.\n\n#### Data-last\n\nExample:\n```ts\nimport { db } from \"./db\";\nimport { users } from \"./schema\";\nimport { transform } from \"drizzle-toolbelt\";\n\nconst users = await db.select({\n  id: users.id,\n  name: users.name,\n})\n  .from(users)\n  .then(transform({\n    fields: {\n      name: (row) =\u003e row.name.toUpperCase(),\n    }\n  }))\n```\n\n\n#### Data-first\n\nYou may choose to pass rows in the arguments to call `transform` in a stand-alone way.\n\n```ts\nimport { db } from \"./db\";\nimport { users } from \"./schema\";\nimport { transform } from \"drizzle-toolbelt\";\n\nconst users = await db.select({\n  id: users.id,\n  name: users.name,\n}).from(users);\n\nconst usersWithNameUppercase = transform({\n  rows,\n  fields: {\n    name: (row) =\u003e row.name.toUpperCase(),\n  }});\n```\n\n## Contributing\n\nThis project is open to contributions. Feel free to open an issue or a pull request.\n\n## License\n\nMIT","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffredericoo%2Fdrizzle-toolbelt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffredericoo%2Fdrizzle-toolbelt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffredericoo%2Fdrizzle-toolbelt/lists"}