Constructors

Methods

  • Generates data based on the specified type, amount, and schema. This method generates a specified amount of data items based on the provided type and schema. If the type is 'custom', it uses the provided schema to generate data. Otherwise, it uses a predefined schema retrieved by setSchema. It can also add a unique ID (key) and an index to each item if createId and createIndex are true, respectively.

    Parameters

    • type: string

      The type of data to generate. Can be 'custom' or any other string representing a predefined schema type.

    • amount: number

      The number of data items to generate.

    • createId: boolean

      Whether to add a unique ID (key) to each item.

    • createIndex: boolean

      Whether to add an index to each item.

    • Optionalschema: any

      An optional custom schema object to use when type is 'custom'.

    Returns any

    An array of generated data items, or a single data item if amount is 1.

    const dataGenerator = new AioDataGenerator();
    // Generate 5 user data items with predefined 'user' schema
    const users = dataGenerator.generateData('user', 5, true, true);
    // Generate 3 custom data items with a custom schema
    const customSchema = { name: 'string', age: 'number' };
    const customData = dataGenerator.generateData('custom', 3, true, true, customSchema);
    // Generate a single summary data item
    const summary = dataGenerator.generateData('summary', 1, false, false, 'summary');
  • Retrieves a predefined schema object based on the provided type. This method retrieves a schema object from the predefined schemas using the provided type. If the type is found, it calls the function within the schema object and returns the result. Otherwise, it logs an error message and returns an empty object.

    Parameters

    • type: string

      The type of schema to retrieve.

    Returns any

    The schema object retrieved from the predefined schemas or an empty object if the type is not found.

    
    
  • Sets the schema to be used for data generation. This method determines which schema to use based on the provided type. If the type is 'custom', it uses the provided schema or an empty object if no schema is provided. Otherwise, it retrieves a predefined schema using the getSchema method.

    Parameters

    • type: string

      The type of schema to use. Can be 'custom' or any other string representing a predefined schema type.

    • Optionalschema: any

      An optional custom schema object to use when type is 'custom'.

    Returns any

    The schema object that will be used for data generation.

    const dataGenerator = new AioDataGenerator();
    const userSchema = dataGenerator.setSchema('user'); // Assuming 'user' is a predefined schema type
    const customSchema = { name: 'string', age: 'number' };
    const myCustomSchema = dataGenerator.setSchema('custom', customSchema);