Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem creating multiple Comments on one Post #142

Open
AaronNGray opened this issue Jul 29, 2021 · 0 comments
Open

Problem creating multiple Comments on one Post #142

AaronNGray opened this issue Jul 29, 2021 · 0 comments

Comments

@AaronNGray
Copy link

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)
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant