@airhead/typeorm-seeder
v1.3.0
Published
Decorative seeder for TypeORM
Downloads
10
Readme
Introduction
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.
Installation
npm install @airhead/typeorm-seeder typeorm
Prerequisites
Enable decorators in tsconfig.json
{
"compilerOptions": {
"emitDecoratorMetadata": true,
"experimentalDecorators": true
}
}
Documentation
Coming soon! In the meantime, submit an issue.
Quick start
Entities
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'
// }
Seeders
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();
Grow icons created by Freepik - Flaticon