Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Model Typescript Transfomer Laravel Package

nolanos/laravel-model-typescript-transfomer

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Pros:

    • Seamless Laravel Integration: Leverages spatie/laravel-typescript-transformer, a battle-tested foundation, ensuring compatibility with Laravel’s Eloquent ORM and existing TypeScript workflows.
    • Model-Centric: Directly targets Eloquent models, aligning with Laravel’s core abstraction layer. Ideal for projects with a rich domain model (e.g., SaaS platforms, CRUD-heavy applications).
    • Type Safety: Generates runtime-agnostic TypeScript definitions, reducing frontend-backend type mismatches (e.g., API responses, form inputs).
    • Customizable: Supports $hidden/$visible filters, enabling granular control over exposed properties (critical for APIs or sensitive data).
  • Cons:

    • Limited to Eloquent: Does not support non-Eloquent models (e.g., custom query builders, raw SQL models). May require workarounds for hybrid architectures.
    • Static Analysis: Relies on database schema and model metadata, not runtime validation (e.g., polymorphic relationships or dynamic attributes may need manual overrides).
    • No Nested Relationships: By default, focuses on flat model properties; nested hasOne/belongsTo types require additional configuration (see spatie’s docs).

Integration Feasibility

  • Low Effort for Standard Use Cases:
    • Prerequisites: Requires spatie/laravel-typescript-transformer (v2+) and a Laravel project with Eloquent models.
    • Configuration: Minimal—append two classes to the transformer config (collectors/transformers).
    • Generation: Single CLI command (typescript:generate) with optional --watch for dev workflows.
  • Potential Friction Points:
    • Database Schema Changes: TypeScript types are schema-dependent; migrations or manual ALTER TABLE operations may require regenerating types.
    • Complex Relationships: Deeply nested or polymorphic relationships may need custom transformers (e.g., overriding ModelTransformer).
    • Monorepo/Shared Types: If TypeScript types are shared across frontend/backend (e.g., via shared-types package), synchronization must be manual or automated via CI.

Technical Risk

Risk Area Severity Mitigation Strategy
Schema Drift High Implement CI checks (e.g., diff generated types post-migration) or use php artisan migrate:fresh in tests.
Type Inaccuracy Medium Override ModelTransformer for edge cases (e.g., custom casts, computed attributes).
Build Pipeline Impact Low Cache generated types (e.g., .gitignore or node_modules) to avoid regenerating on every npm install.
Frontend Sync Medium Use a shared types package (e.g., @myorg/shared-types) or a post-generation script to copy types to frontend.
Performance Low Generation is O(n) per model; negligible for typical Laravel apps (<100 models).

Key Questions for TPM

  1. Use Case Alignment:
    • Are we generating types for API responses, form inputs, or both? (Affects $visible/$hidden configuration.)
    • Do we need runtime validation (e.g., Zod schemas) alongside TypeScript types?
  2. Architecture Constraints:
    • Are models monolithic (single table) or composite (e.g., User + UserProfile)? Nested types may require customization.
    • Do we use polymorphic relationships (e.g., morphTo) or accessors/mutators? These may not auto-generate correctly.
  3. Workflow Integration:
    • Should types be regenerated on every deploy or cached (with manual triggers for schema changes)?
    • How will frontend teams consume these types? (Direct import, shared package, or API-first approach?)
  4. Maintenance:
    • Who owns type accuracy (backend devs, QA, or a shared "types team")?
    • How will we handle breaking changes in the database schema?

Integration Approach

Stack Fit

  • Best For:
    • Laravel + TypeScript Monoliths: Full-stack apps where backend models directly map to frontend types (e.g., Next.js, Vue, React).
    • API-First Projects: When backend models define the contract for frontend consumers (e.g., GraphQL, REST APIs).
    • Dev Workflows: Teams using Laravel Mix/Vite or Inertia.js where type safety between layers is critical.
  • Less Ideal For:
    • Headless Backends: If the backend is decoupled from frontend (e.g., separate GraphQL schema), this adds unnecessary coupling.
    • Legacy Systems: Projects with raw SQL, non-Eloquent models, or dynamic schemas (e.g., NoSQL hybrids).
    • Microservices: Cross-service type sharing would require additional tooling (e.g., OpenAPI + TypeScript codegen).

Migration Path

  1. Assessment Phase:
    • Audit existing Eloquent models for:
      • Complex relationships (polymorphic, many-to-many).
      • Custom casts/accessors that may break auto-generation.
    • Define type consumption strategy (direct import, shared package, etc.).
  2. Pilot Integration:
    • Install spatie/laravel-typescript-transformer and nolanos/laravel-model-typescript-transfomer.
    • Configure for 1–2 critical models (e.g., User, Order).
    • Test generation and frontend consumption.
  3. Gradual Rollout:
    • Phase 1: Generate types for core models used in APIs/forms.
    • Phase 2: Extend to nested relationships (custom transformers if needed).
    • Phase 3: Automate in CI/CD (e.g., regenerate on main branch pushes).
  4. Optimization:
    • Cache generated types in node_modules or a .types directory.
    • Add pre-commit hooks to validate type generation.

Compatibility

Component Compatibility Notes
Laravel Tested with Laravel 9+. Ensure spatie/laravel-typescript-transformer is v2+.
Eloquent Models Works with standard models; custom casts may need manual type overrides.
Database Relies on current schema. Migrations require type regeneration.
TypeScript Outputs plain TypeScript interfaces. No framework-specific types (e.g., React hooks).
Frontend Frameworks Agnostic; works with React, Vue, Svelte, etc.
Monorepos May need path aliases or post-generation scripts to sync types.

Sequencing

  1. Prerequisites:
    • Laravel project with Eloquent models.
    • spatie/laravel-typescript-transformer installed (v2+).
    • TypeScript setup in frontend (or shared types directory).
  2. Core Setup:
    • Install nolanos/laravel-model-typescript-transfomer.
    • Update config/typescript-transformer.php.
    • Test generation with php artisan typescript:generate.
  3. Validation:
    • Verify types in frontend (e.g., API responses, form inputs).
    • Check for false positives/negatives (e.g., hidden fields leaking).
  4. Automation:
    • Add to CI/CD (e.g., regenerate on main or schema changes).
    • Cache types in node_modules or .gitignore them.
  5. Scaling:
    • Extend to relationships (custom transformers if needed).
    • Document type maintenance processes (e.g., who regenerates on schema changes).

Operational Impact

Maintenance

  • Proactive Tasks:
    • Schema Changes: Regenerate types after migrations (php artisan typescript:generate).
    • Model Updates: Add/remove $hidden/$visible properties and regenerate.
    • Custom Transformers: Maintain overrides for edge cases (e.g., polymorphic types).
  • Reactive Tasks:
    • Type Mismatches: Debug when frontend types don’t match backend (e.g., due to $appends or accessors).
    • CI Failures: Monitor for broken builds due to missing or incorrect types.
  • Ownership:
    • Backend Team: Owns model schema and type generation.
    • Frontend Team: Owns type consumption and integration.
    • Shared: Define SLA for type accuracy (e.g., "Types must match API within 24 hours of schema changes").

Support

  • Common Issues:

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours