Faker.js v7 clear store

289 Views Asked by At

I use faker.js in gulp to create a lot of unique data

faker.helpers.unique

Due to debugging, the store is often full and displays the error

Error: Exceeded maxRetries: 1000 for uniqueness check.

So I need to clear the store so that faker.js can recreate unique data.

I can't find an instruction to clear the store from the docs, how should I reset/clear the store?

2

There are 2 best solutions below

0
Zac Anger On BEST ANSWER

There is not a builtin method for resetting the store. Right now, your best option is probably to use your own store implementation. You could change maxRetries, override compare, use a store that resets on a timer, or have a catch looking for a FakerError with that message, and use that to trigger the reset.

0
Mert Ekinci On

Faker's unique is now deprecated. You can use enforce-unique to achieve same goal instead of implementing your own store.

import { faker } from '@faker-js/faker';
import { UniqueEnforcer } from 'enforce-unique';

const uniqueEnforcer = new UniqueEnforcer();

const uniqueData = uniqueEnforcer.enforce(
  () => {
    // Create your data as you like and return it
    return faker.internet.email();
  },
  {
    // Increase the limits as you need.
    maxTime: 1000 * 60,
    maxRetries: 99999,
  },
);

You can create a loop with this to generate thousands of unique data.

Check documentation, https://www.npmjs.com/package/enforce-unique