{"id":31070160,"url":"https://github.com/aminya/mikro-typebox","last_synced_at":"2025-09-15T23:02:10.219Z","repository":{"id":314812009,"uuid":"1056837312","full_name":"aminya/mikro-typebox","owner":"aminya","description":"Generate validation schemas from Mikro-ORM entities. Supports TypeBox, Zod, Valibot, ArkType, Effect, io-ts, Yup, and more.","archived":false,"fork":false,"pushed_at":"2025-09-14T23:01:41.000Z","size":34,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-09-15T00:24:28.695Z","etag":null,"topics":["db","mikro","mikro-orm","schema","typebox","typescript","valibot","validation","yup","zod"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/mikro-typebox","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/aminya.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"License.txt","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,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null},"funding":{"github":["aminya"],"polar":"aminya","patreon":"aminya"}},"created_at":"2025-09-14T22:36:45.000Z","updated_at":"2025-09-14T23:01:44.000Z","dependencies_parsed_at":"2025-09-15T00:24:31.391Z","dependency_job_id":"a429272b-2f62-4da0-8ac6-4f5719193de9","html_url":"https://github.com/aminya/mikro-typebox","commit_stats":null,"previous_names":["aminya/mikro-typebox"],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/aminya/mikro-typebox","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aminya%2Fmikro-typebox","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aminya%2Fmikro-typebox/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aminya%2Fmikro-typebox/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aminya%2Fmikro-typebox/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/aminya","download_url":"https://codeload.github.com/aminya/mikro-typebox/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aminya%2Fmikro-typebox/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":275333311,"owners_count":25446100,"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-09-15T02:00:09.272Z","response_time":75,"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":["db","mikro","mikro-orm","schema","typebox","typescript","valibot","validation","yup","zod"],"created_at":"2025-09-15T23:00:27.478Z","updated_at":"2025-09-15T23:02:10.180Z","avatar_url":"https://github.com/aminya.png","language":"TypeScript","readme":"# mikro-typebox\n\nGenerate validation schemas from Mikro-ORM entities. Supports TypeBox, Zod, Valibot, ArkType, Effect, io-ts, Yup, and more.\n\n## Features\n\n- 🏗️ **Entity Type Generation**: Converts Mikro-ORM entity classes to TypeScript types\n- 🔄 **Multiple Validation Libraries**: Supports TypeBox, Zod, Valibot, ArkType, Effect, io-ts, Yup, and more\n- 🎯 **Smart Type Resolution**: Automatically replaces entity references with their ID types\n- 📦 **Collection Handling**: Converts `Collection\u003cT\u003e` to `Array\u003cT\u003e` with proper type mapping\n- 🧹 **Code Cleanup**: Removes imports, decorators, and method calls from generated types\n\n## Installation\n\n```shell\nnpm install --save-dev mikro-typebox\n```\n\n## CLI Usage\n\nThe package includes a command-line interface for easy usage:\n\n```shell\n# Generate typebox schema from `./src/entities` to `./src/entity-validators.ts`\nnpx mikro-typebox generate\n\n# Generate for Zod\nnpx mikro-typebox generate --target zod\n\n# Specify entities directory and output file\nnpx mikro-typebox generate --target zod --entities ./src/models --output ./src/entity-validators.ts\n```\n\n### CLI Options\n\n#### `generate` command\n\n- `-e, --entities \u003cpath\u003e`: Directory containing entity files (default: `./src/entities`)\n- `-o, --output \u003cfile\u003e`: Output file path (default: `./src/entity-validators.ts`)\n- `--no-write`: Print the code to the console instead of writing to a file (default: writes to a file)\n- `-t, --target \u003clibrary\u003e`: Target validation library (default: `typebox`)\n- `--partials`, `--no-partials`: Generate partial types instead of inline primary key references (default: `true` for `typebox`)\n\n### Supported Validation Libraries\n\n- `typebox` - TypeBox (default)\n- `zod` - Zod\n- `valibot` - Valibot\n- `arktype` - ArkType\n- `effect` - Effect\n- `io-ts` - io-ts\n- `yup` - Yup\n- `json-schema` - JSON Schema\n- `javascript` - JavaScript\n- `typescript` - TypeScript\n- `value` - Value\n\n## Programmatic Usage\n\nYou can also use the API to generate validation schemas programmatically.\n\n### `generateEntityValidator(options)`\n\nGenerates validation schemas from Mikro-ORM entities.\n\n#### Parameters\n\n- `options.entitiesDir` (optional): Directory containing the entity files (default: `\"./src/entities\"`)\n- `options.outputFile` (optional): File path to write the generated code (default: `\"./src/entity-validators.ts\"`)\n- `options.write` (optional): Whether to write the code to a file (default: `true`)\n- `options.targetValidationLibrary` (optional): Target validation library (default: `\"typebox\"`)\n- `options.partials` (optional): Whether to generate partial types instead of inline primary key references (default: `true` for `typebox`)\n\n\n### `generateEntityTypes(code, entityIdTypes)`\n\nConverts Mikro-ORM entity code to TypeScript types.\n\n#### Parameters\n\n- `code`: The entity code as a string\n- `entityIdTypes` (optional): Map of entity names to their ID types\n\n### `generateEntityFileTypes(fileContents)`\n\nProcesses multiple entity files and generates types with proper entity ID replacement.\n\n#### Parameters\n\n- `fileContents`: Array of entity file contents as strings\n\n## Usage Examples\n\n```typescript\nimport { generateEntityValidator } from 'mikro-typebox';\n\n// Generate for TypeBox\nawait generateEntityValidator({\n  entitiesDir: './src/entities',\n  outputFile: './src/validators.ts',\n  write: true,\n  partials: true\n});\n\n// Generate for Zod\nconst zodCode = await generateEntityValidator({\n  entitiesDir: './src/entities',\n  targetValidationLibrary: 'zod',\n  outputFile: './src/zod-validators.ts',\n  write: true,\n});\n\n\n// Generate for Valibot\nconst valibotCode = await generateEntityValidator({\n  entitiesDir: './src/entities',\n  targetValidationLibrary: 'valibot',\n  outputFile: './src/valibot-validators.ts',\n  write: true\n});\n```\n\n### Working with Entity Files\n\nGiven a Mikro-ORM entity file like this:\n\n```typescript\n// src/entities/User.ts\nimport { Entity, PrimaryKey, Property, Collection, OneToMany } from '@mikro-orm/core';\nimport { Book } from './Book';\n\n@Entity()\nexport class User {\n  @PrimaryKey()\n  id!: number;\n\n  @Property()\n  name!: string;\n\n  @Property()\n  email!: string;\n\n  @OneToMany(() =\u003e Book, book =\u003e book.author)\n  books = new Collection\u003cBook\u003e(this);\n\n  constructor({ name, email }: User) {\n    this.name = name;\n    this.email = email;\n  }\n}\n\n// src/entities/Book.ts\nimport { Entity, PrimaryKey, Property, ManyToOne } from '@mikro-orm/core';\nimport { User } from './User';\n\n@Entity()\nexport class Book {\n  @PrimaryKey()\n  id!: number;\n\n  @Property()\n  title!: string;\n\n  @ManyToOne(() =\u003e User)\n  author!: User;\n\n  constructor({ title, author }: Book) {\n    this.title = title;\n    this.author = author;\n  }\n}\n```\n\nAnd then generate validation schemas (e.g., with TypeBox):\n\n```typescript\nimport { Type, Static } from '@sinclair/typebox'\n\nexport namespace schema {\n    export type Book = Static\u003ctypeof Book\u003e\n    export const Book = Type.Object({\n        id: Type.Number(),\n        title: Type.String(),\n        author: schema.PartialUser\n    }, { \"$id\": \"schema.Book\" })\n\n    export type PartialBook = Static\u003ctypeof PartialBook\u003e\n    export const PartialBook = Type.Object({\n        id: Type.Number(),\n        title: Type.Optional(Type.String()),\n        author: Type.Optional(schema.PartialUser)\n    }, { \"$id\": \"schema.PartialBook\" })\n\n    export type User = Static\u003ctypeof User\u003e\n    export const User = Type.Object({\n        id: Type.Number(),\n        name: Type.String(),\n        email: Type.String(),\n        books: Type.Any()\n    }, { \"$id\": \"schema.User\" })\n\n    export type PartialUser = Static\u003ctypeof PartialUser\u003e\n    export const PartialUser = Type.Object({\n        id: Type.Number(),\n        name: Type.Optional(Type.String()),\n        email: Type.Optional(Type.String()),\n        books: Type.Optional(Type.Any())\n    }, { \"$id\": \"schema.PartialUser\" })\n\n}\n```\n\nor for Zod\n\n```typescript\nimport { z } from 'zod'\n\nexport type schema_Book = z.infer\u003ctypeof schema_Book\u003e\nexport const schema_Book = z.object({\n    id: z.number(),\n    title: z.string(),\n    author: z.object({})\n})\n\nexport type schema_User = z.infer\u003ctypeof schema_User\u003e\nexport const schema_User = z.object({\n    id: z.number(),\n    name: z.string(),\n    email: z.string(),\n    books: z.any()\n})\n```\n\n### Programmatic Usage\n\n```typescript\nimport { generateEntityTypes, generateEntityFileTypes } from 'mikro-typebox';\n\n// Process a single entity file\nconst entityCode = `\nimport { Entity, PrimaryKey, Property } from '@mikro-orm/core';\n\n@Entity()\nexport class Product {\n  @PrimaryKey()\n  id!: number;\n\n  @Property()\n  name!: string;\n}\n`;\n\nconst types = generateEntityTypes(entityCode);\nconsole.log(types);\n// Output: export type Product = { id: number; name: string; };\n\n// Process multiple entity files\nconst fileContents = [entityCode, anotherEntityCode];\nconst allTypes = generateEntityFileTypes(fileContents);\n```\n\n## How It Works\n\n1. **Entity Discovery**: Scans entity files for classes decorated with `@Entity()`\n2. **Type Extraction**: Extracts property types and relationships from entity classes\n3. **ID Type Resolution**: Replaces entity references with their primary key types\n4. **Collection Conversion**: Converts `Collection\u003cT\u003e` to `Array\u003cT\u003e` with proper type mapping\n5. **Code Cleanup**: Removes Mikro-ORM specific imports, decorators, and method calls\n6. **Schema Generation**: Converts TypeScript types to validation schemas using the target library\n\n## License\n\nApache-2.0, Copyright (c) 2025 Amin Yara\n\n","funding_links":["https://github.com/sponsors/aminya","https://polar.sh/aminya","https://patreon.com/aminya"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faminya%2Fmikro-typebox","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faminya%2Fmikro-typebox","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faminya%2Fmikro-typebox/lists"}