w3tecch / typeorm-seeding

🌱 A delightful way to seed test data into your database.
https://www.npmjs.com/package/typeorm-seeding
MIT License
887 stars 132 forks source link

Generate the unique value #135

Closed sdr0x07b6 closed 3 years ago

sdr0x07b6 commented 3 years ago

I have installed and am using typorm-seeding on Nestjs. The factory cannot generate unique values.

Faker uses the unique() method to generate unique values. However, the Faker in typeorm-seeding's package.json is an older version and does not implement the unique() method.

I tried to install the latest Faker myself, but it is not unique because it runs on a new Faker instance each the factory is executed. I think the faker instance that is passed to define() needs to have a unique() method.

Or is there any way to make the factory generate unique values?

define(User, (faker: typeof Faker) => {
  const user = new User()

  // Error
  // unique() method is not implemented
  user.email = faker.unique(faker.internet.​email)

  return user
})
spmsupun commented 3 years ago

did you find a solution?

sdr0x07b6 commented 3 years ago

I think we have to wait until typeorm-seeding itself updates it. 🤔

spmsupun commented 3 years ago

Found a solution, install latest fake and require it seperatly:

const fk = require('faker');

define(User, (faker: typeof Faker) => {
  const user = new User()
  user.email = fk.unique(faker.internet.​email)

  return user
})
sdr0x07b6 commented 3 years ago

@spmsupun Hi, Thanks for taking the time to comment. And sorry for the late reply.

I've tried the method you suggested. I think it's safe to say that I'm getting unique generation!

I had assumed that if I require() each time in the factory instead of receiving the faker instance as an argument, it would not be unique because it determines the uniqueness in the new instance each time.

I tried seeding several times issuing 10,000 unique email addresses, and all were successful. I'm going to use the method you taught me for a while now. Thanks a lot!

sdr0x07b6 commented 3 years ago

Even fk.unique(fk.internet.email) instead of fk.unique(faker.internet.email) succeeded in generating 50,000 unique entries. After running it several times, I have the impression that there is no difference in the results between them.


When I generated 50,000 integers, fk.unique(fk.random.number), no matter how many times I tried, the uniqueness check error occurred after about 40,000 times. At that time, the range of generated values was about 10-99,999. I think the probability is high because the conflict occurs at the 40,000th time among them. In the case of internet.email, conflicts may be less likely to occur.

However, if this has nothing to do with typeorm-seeding, but is caused by Faker.unique() itself, I don't think this is the Issue to discuss here. In any case, random seeding rarely generates more than 40,000 records, so it's not a big problem.


I'd like to try faker.random.arrayElement because it's easier to check for unique generation, but I wasn't sure how. fk.unique(fk.random.arrayElement, 'abcdefghijklmnopqrstuvwxyz0123456789'.split('')). With this method, we get "a" every time, which is not unique.