TypeORM Entity and Column Creation Guide
TypeORM Entity and Column Creation Guide
What are TypeORM Entity and Column?
TypeORM is a powerful ORM (Object Relational Mapping) library that works with Node.js. It greatly simplifies database management in modern TypeScript/JavaScript projects. The Entity concept represents a table in the database, while a Column represents a field (column) within that table. Creating entities and columns with TypeORM makes your data models more readable and manageable in your code.
How to Create Entities and Columns with TypeORM?
In TypeORM, the @Entity() decorator is used to create entities and @Column() decorators are used for column definitions. Below you can see how to define a "User" entity and how to add columns of different types:
import { Entity, PrimaryGeneratedColumn, Column } from "typeorm";
@Entity()
export class Kullanici {
@PrimaryGeneratedColumn()
id: number;
@Column({ length: 100 })
ad: string;
@Column({ unique: true })
email: string;
@Column({ default: true })
aktif: boolean;
}
In the code above, we defined an auto-increment id field with @PrimaryGeneratedColumn() and columns with different properties using @Column(). We set a default value for the "aktif" column, and made the "email" column unique. Creating entities and columns with TypeORM is this practical.
Special Column Types
In some cases, you may need columns with specific types like date, number, or JSON. Different types are used during TypeORM entity and column creation as follows:
@Column({ type: "date" })
dogumTarihi: string;
@Column({ type: "float" })
yillikGelir: number;
@Column({ type: "json", nullable: true })
ekBilgiler: any;
Here, we have defined columns with date, float, and json types. A nullable column can be left optional. All these features make it easy to control database tables with TypeORM.
Conclusion: Using TypeORM Entity and Column
TypeORM entity and column creation operations make your data model flexible and powerful, and also ensure synchronization between your code and your database. In this guide, we covered not only the basic concepts of entity and column, but also the ways to define fields of different types. By using these structures correctly in your TypeORM projects, you can develop sustainable and secure applications.

Yorum Gönder