Core Concepts
Create Builder

createBuilder

The createBuilder function is a factory function that facilitates the creation of a builder object. The createBuilder function is a named export from the @ibnlanre/builder package. This guide explains how to create a builder object using the createBuilder function.

The following code snippet demonstrates how to import the createBuilder function:

import { createBuilder } from '@ibnlanre/builder';

Creating a builder

The createBuilder function takes a register object and an optional array of prefixes as arguments. Invoking it, returns a builder object that can be used to access the keys and values in the register object. The builder object adds a layer of abstraction to the register object, allowing you to dynamically generate keys.

The following code snippet demonstrates how to invoke the createBuilder function:

const noPrefixBuilder = createBuilder(register);

Builder with prefixes

If the builder object is created with prefixes, the generated keys array would be prefixed with the prefixes, followed by the keys accessed in the register. The prefixes serve as an identifier for each builder object created. Although there is no limit to the number of prefixes that can be used, it is advised to use a maximum of three prefixes.

The following code snippet demonstrates how to call the createBuilder function with prefixes:

const prefixedBuilder = createBuilder(register, { prefix: ["user", "record"] });