typeorm-seeding icon indicating copy to clipboard operation
typeorm-seeding copied to clipboard

Problem creating multiple Comments on one Post

Open AaronNGray opened this issue 4 years ago • 0 comments

I am only seeming to get one Comment per Post !

post.factory.ts

import * as Faker from 'faker';
import { define, factory } from 'typeorm-seeding-updated';
import { User } from '../../entity/User';
import { Post } from '../../entity/Post';
import { Comment } from '../../entity/Comment';

define(Post, (faker: typeof Faker) => {

  const post = new Post()

  post.text = faker.lorem.text();

  post.user = factory(User)() as any;

  post.date = faker.date.past(3);

  return post;
})

post.seed.ts

import * as faker from 'faker';
import { Seeder, Factory } from 'typeorm-seeding-updated'
import { Post } from '../../entity/Post'
import { Comment } from '../../entity/Comment'

export default class CreatePosts implements Seeder {
  public async run(factory: Factory): Promise<void> {
    const posts = await factory(Post)({ roles: [] }).createMany(100);

    for (const post of posts) {
        const { id } = post;

        if (faker.datatype.number(1))
            await factory(Comment)({ post:id }).createMany(faker.datatype.number(25));
    }
  }
}

comment.factory.ts

import * as Faker from 'faker';
import { define, factory } from 'typeorm-seeding-updated';
import { User } from '../../entity/User';
import { Post } from '../../entity/Post';
import { Comment } from '../../entity/Comment';

define(Comment, (faker: typeof Faker) => {

  const comment = new Comment()

  comment.text = faker.lorem.text();

  comment.user = factory(User)() as any;

  comment.post = factory(Post)() as any;

  comment.date = faker.date.past(3);

  return comment;
})

comment.seed.ts

import { Connection } from 'typeorm'
import { Seeder, Factory } from 'typeorm-seeding-updated'
import { Comment } from '../../entity/Comment'

export default class CreateComments implements Seeder {
  public async run(factory: Factory, connection: Connection): Promise<any> {
    await factory(Comment)().createMany(100)
  }
}

AaronNGray avatar Jul 29 '21 23:07 AaronNGray