TypeORM Reader
Reads TypeScript files containing TypeORM entity definitions and extracts database schema information.
Overview
The TypeORM Reader parses TypeScript source files that define TypeORM entities (classes with TypeORM decorators) and converts them into RelSpec's internal database model representation.
Features
- Parses TypeORM decorators and entity definitions
- Extracts table, column, and relationship information
- Supports various TypeORM column types and options
- Handles constraints, indexes, and relationships
Usage
Basic Example
package main
import (
"fmt"
"git.warky.dev/wdevs/relspecgo/pkg/readers"
"git.warky.dev/wdevs/relspecgo/pkg/readers/typeorm"
)
func main() {
options := &readers.ReaderOptions{
FilePath: "/path/to/entities/",
}
reader := typeorm.NewReader(options)
db, err := reader.ReadDatabase()
if err != nil {
panic(err)
}
fmt.Printf("Found %d schemas\n", len(db.Schemas))
}
CLI Example
# Read TypeORM entities and convert to JSON
relspec --input typeorm --in-file entities/ --output json --out-file schema.json
# Convert TypeORM to GORM models
relspec --input typeorm --in-file User.ts --output gorm --out-file models.go
Example TypeORM Entity
import {
Entity,
PrimaryGeneratedColumn,
Column,
CreateDateColumn,
OneToMany,
} from 'typeorm';
import { Post } from './Post';
@Entity('users')
export class User {
@PrimaryGeneratedColumn('increment')
id: number;
@Column({ type: 'varchar', length: 50, unique: true })
username: string;
@Column({ type: 'varchar', length: 100 })
email: string;
@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' })
content: string;
@ManyToOne(() => User, (user) => user.posts, { onDelete: 'CASCADE' })
@JoinColumn({ name: 'user_id' })
user: User;
}
Supported TypeORM Decorators
@Entity() - Entity/table definition
@PrimaryGeneratedColumn() - Auto-increment primary key
@PrimaryColumn() - Primary key
@Column() - Column definition
@CreateDateColumn() - Auto-set creation timestamp
@UpdateDateColumn() - Auto-update timestamp
@OneToMany() - One-to-many relationship
@ManyToOne() - Many-to-one relationship
@JoinColumn() - Foreign key column
@Index() - Index definition
@Unique() - Unique constraint
Notes
- Schema name can be specified in
@Entity() decorator
- Supports both JavaScript and TypeScript entity files
- Relationship metadata is extracted from decorators