# TypeORM Writer

Generates TypeScript files with TypeORM entity definitions from database schema information.

## Overview

The TypeORM Writer converts RelSpec's internal database model representation into TypeScript source code with TypeORM entity classes, including proper decorators, relationships, and column configurations.

## Features

- Generates TypeORM-compatible TypeScript entities
- Creates proper decorator usage (@Entity, @Column, etc.)
- Adds relationship decorators (@OneToMany, @ManyToOne, @JoinColumn)
- Handles column types and options
- Supports constraints and indexes
- Outputs formatted TypeScript code

## Usage

### Basic Example

```go
package main

import (
    "git.warky.dev/wdevs/relspecgo/pkg/models"
    "git.warky.dev/wdevs/relspecgo/pkg/writers"
    "git.warky.dev/wdevs/relspecgo/pkg/writers/typeorm"
)

func main() {
    options := &writers.WriterOptions{
        OutputPath: "entities/",
    }

    writer := typeorm.NewWriter(options)
    err := writer.WriteDatabase(db)
    if err != nil {
        panic(err)
    }
}
```

### CLI Examples

```bash
# Generate TypeORM entities from PostgreSQL database
relspec --input pgsql \
  --conn "postgres://localhost/mydb" \
  --output typeorm \
  --out-file entities/

# Convert GORM models to TypeORM
relspec --input gorm --in-file models.go --output typeorm --out-file src/entities/

# Convert JSON to TypeORM entities
relspec --input json --in-file schema.json --output typeorm --out-file entities/
```

## Generated Code Example

```typescript
import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  OneToMany,
  ManyToOne,
  JoinColumn,
  Index,
} from 'typeorm';
import { Post } from './Post';

@Entity('users')
export class User {
  @PrimaryGeneratedColumn('increment')
  id: number;

  @Column({ type: 'varchar', length: 50, unique: true })
  @Index()
  username: string;

  @Column({ type: 'varchar', length: 100 })
  email: string;

  @Column({ type: 'text', nullable: true })
  bio: string | null;

  @CreateDateColumn({ name: 'created_at' })
  createdAt: Date;

  @OneToMany(() => Post, (post) => post.user)
  posts: Post[];
}

@Entity('posts')
export class Post {
  @PrimaryGeneratedColumn('increment')
  id: number;

  @Column({ name: 'user_id' })
  userId: number;

  @Column({ type: 'varchar', length: 200 })
  title: string;

  @Column({ type: 'text', nullable: true })
  content: string | null;

  @ManyToOne(() => User, (user) => user.posts, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'user_id' })
  user: User;
}
```

## Supported TypeORM Decorators

### Entity Decorators
- `@Entity()` - Define entity/table
- `@PrimaryGeneratedColumn()` - Auto-increment primary key
- `@PrimaryColumn()` - Primary key
- `@Column()` - Column definition
- `@CreateDateColumn()` - Auto-set creation timestamp
- `@UpdateDateColumn()` - Auto-update timestamp

### Relationship Decorators
- `@OneToMany()` - One-to-many relationship
- `@ManyToOne()` - Many-to-one relationship
- `@JoinColumn()` - Foreign key column specification

### Constraint Decorators
- `@Index()` - Create index
- `@Unique()` - Unique constraint

## Column Options

```typescript
@Column({
  type: 'varchar',      // Column type
  length: 255,          // Length for varchar/char
  nullable: true,       // Allow NULL
  unique: true,         // Unique constraint
  default: 'value',     // Default value
  name: 'column_name',  // Database column name
})
```

## Type Mapping

| SQL Type | TypeScript Type | TypeORM Type |
|----------|-----------------|--------------|
| bigint | number | 'bigint' |
| integer | number | 'int' |
| varchar | string | 'varchar' |
| text | string | 'text' |
| boolean | boolean | 'boolean' |
| timestamp | Date | 'timestamp' |
| json | object | 'json' |
| uuid | string | 'uuid' |

## Notes

- Entity class names are PascalCase
- One file per entity (named after the entity)
- Relationship imports are auto-generated
- Nullable columns use TypeScript union with `null`
- Foreign key actions (CASCADE, etc.) are included
- Schema names can be specified in `@Entity()` decorator
