Methods for Creating Seed Data with TypeORM
Methods for Creating Seed Data with TypeORM
What is TypeORM and Why Use Seed Data?
TypeORM is a popular and powerful ORM (Object-Relational Mapping) library used in Node.js-based projects. Especially thanks to its full compatibility with TypeScript, it is highly preferred in the modern JavaScript world. In the software development process, "seed data," i.e. predefined initial data, automatically creates the basic information that we may need during testing or demos in the database. Creating seed data with TypeORM ensures that the application starts with a certain set of initial data and offers great convenience in the development environment.
How to Create Seed Data with TypeORM?
There are different methods to add seed data with TypeORM. The most common approach is to write a special seed file or script to add your data to Entities. Below you can find the steps to create simple seed data with TypeORM.
Step 1: Creating the Seed Script
import { AppDataSource } from "./data-source";
import { User } from "./entity/User";
AppDataSource.initialize()
.then(async () => {
const user = new User();
user.firstName = "Ali";
user.lastName = "Veli";
user.age = 28;
await AppDataSource.manager.save(user);
console.log("Seed data added successfully!");
process.exit(0);
})
.catch((error) => {
console.error(error);
process.exit(1);
});
Step 2: Running the Seed Script
You can run the seed file you created with the following command:
node seed.ts
With these steps, you can quickly and practically fill your database with test data using TypeORM.
Conclusion: Advantages of Seed Data with TypeORM
The process of creating seed data with TypeORM provides great convenience both in development and testing environments. Especially with ever-changing database schemas, it offers automation instead of manually populating the database each time. Adding seed data with TypeORM is indispensable for debugging, demo presentations, and end-to-end application testing. By using seed data in your own projects, you can significantly increase your efficiency.

Yorum Gönder