Seed TypeORM entities decoratively
A decorator based seeding tool for TypeORM. Decorate your entities like you normally would, and populate your database automatically. Handy for prototypes and test mocking. Relations, inheritance and embedded entities are supported.
The tool is in early stages and in active development, breaking changes may occur between minor versions.
npm install @airhead/typeorm-seeder typeorm
Enable decorators in tsconfig.json
{
"compilerOptions": {
"emitDecoratorMetadata": true,
"experimentalDecorators": true
}
}
Coming soon! In the meantime, submit an issue.
import { Entity, PrimaryGeneratedColumn } from 'typeorm';
import { Seed } from '@airhead/typeorm-seeder';
@Entity()
export class User {
@PrimaryGeneratedColumn()
public id!: number;
@Column()
@Seed(() => getRandomName())
public name!: string;
}
import { DataSource } from 'typeorm';
import { forEntity } from '@airhead/typeorm-seeder';
const dataSource = await new DataSource().initialize();
const user = await forEntity(User, dataSource).persist();
// User {
// id: 1,
// name: 'Jack'
// }
import { Seeder, BaseSeeder } from '@airhead/typeorm-seeder';
@Seeder({ runsBefore: [SomeOtherSeeder] })
export class UserSeeder implements BaseSeeder {
public seed(): Promise<User[]> {
return forEntity(User, dataSource).persistMany(5);
}
}
import { DataSource } from 'typeorm';
import { forSeeders } from '@airhead/typeorm-seeder';
await forSeeders([UserSeeder, SomeOtherSeeder]).run();