typeorm

package
v1.0.12 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 10, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

README

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
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
# 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

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

@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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Writer

type Writer struct {
	// contains filtered or unexported fields
}

Writer implements the writers.Writer interface for TypeORM entity format

func NewWriter

func NewWriter(options *writers.WriterOptions) *Writer

NewWriter creates a new TypeORM writer with the given options

func (*Writer) WriteDatabase

func (w *Writer) WriteDatabase(db *models.Database) error

WriteDatabase writes a Database model to TypeORM entity format

func (*Writer) WriteSchema

func (w *Writer) WriteSchema(schema *models.Schema) error

WriteSchema writes a Schema model to TypeORM entity format

func (*Writer) WriteTable

func (w *Writer) WriteTable(table *models.Table) error

WriteTable writes a Table model to TypeORM entity format

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL