Skip to content
This repository has been archived by the owner on Dec 2, 2022. It is now read-only.

Latest commit

 

History

History
111 lines (81 loc) · 2.77 KB

README.md

File metadata and controls

111 lines (81 loc) · 2.77 KB



Seed TypeORM entities decoratively



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