Core API Reference

createBuilderStore

This function instantiates a builder store, used for building and operating with schemas based on a specific builder definition.

In most cases, you won't need to use the createBuilderStore method directly. Instead, you will mostly utilize useBuilderStore from @coltorapps/builder-react, which essentially creates the builder store for you.

Reference

createBuilderStore(builder, options?)

Use the createBuilderStore function to instantiate a builder store.

import { createBuilderStore } from "@coltorapps/builder";

import { formBuilder } from "./form-builder";

export const builderStore = createBuilderStore(formBuilder);

Parameters

createBuilderStore accepts two parameters:

ParameterTypeDescription
builderobjectThe builder definition.
optionsobject optionalAn optional partial object with initialization options.

The options parameter properties:

PropertyTypeDescription
initialDataobject optionalThe optional partial initial data.

Returns

createBuilderStore instantiates a builder store, providing a set of methods to operate with the store.

PropertyTypeDescription
getDatafunctionRetrieves the store's data.
getSchemafunctionRetrieves the schema from the store's data.
getEntitiesAttributesErrorsfunctionRetrieves the entities' attributes errors from the store's data.
getSchemaErrorfunctionRetrieves the schema error from the store's data.
setDatafunctionSets the store's new data.
subscribefunctionSubscribes to the store's events, returning a function () => void for unsubscribing.
addEntityfunctionAdds a new entity, returning the newly added entity instance.
setEntityParentfunctionSets an entity's parent.
unsetEntityParentfunctionUnsets an entity's parent and moves it to the root.
setEntityIndexfunctionUpdates an entity's index, either in the parent or in the root.
setEntityAttributefunctionUpdates an entity's attribute value.
deleteEntityfunctionDeletes an entity and all its children.
validateEntityAttributefunctionAn async function that triggers the validation of a specific attribute of a single entity.
validateEntityAttributesfunctionAn async function that triggers the validation of all attributes of a single entity.
validateEntitiesAttributesfunctionAn async function that triggers the validation of the attributes of all entities.
resetEntityAttributeErrorfunctionRemoves the error of a specific attribute of a single entity.
setEntityAttributeErrorfunctionSets the error of a specific attribute of a single entity.
resetEntityAttributesErrorsfunctionRemoves all errors of all attributes of a single entity.
setEntityAttributesErrorsfunctionSets all errors of all attributes of a single entity.
resetEntitiesAttributesErrorsfunctionRemoves all errors of all attributes of all entities.
setEntitiesAttributesErrorsfunctionSets all errors of all attributes of all entities.
validateSchemafunctionAn async function that triggers the validation of the schema, returning the successfully validated schema or the reason for failure.
setSchemaErrorfunctionSets the schema's error.
resetSchemaErrorfunctionRemoves the schema's error.
cloneEntityfunctionClones an entity along with all its children.
getEntityfunctionRetrieves a specific entity.
builderobjectThe builder definition used to instantiate the store.

Data

The data of the builder store is an object containing the following properties:

PropertyTypeDescription
schemaobjectRepresents the schema, which contains the collection of all entities instances and their order.
entitiesAttributesErrorsobjectRepresents the validation errors of various entity attributes.
schemaErrorunknownRepresents the schema validation error.

Events

The builder store emits various events after mutations to subscribed listeners, with different payloads based on the event. A mutation might cause the store to emit multiple events simultaneously. These events can be emitted by the store:

EventDescription
EntityAddedAn entity was added.
EntityUpdatedAn entity was updated.
EntityAttributeUpdatedAn entity's attribute was updated.
EntityDeletedAn entity was deleted.
EntityClonedAn entity was cloned.
RootUpdatedThe root was updated.
EntityAttributeErrorUpdatedAn entity's attribute error was updated.
SchemaErrorUpdatedThe schema's error was updated.
SchemaUpdatedThe schema was updated.
DataSetThe data was manually set.
Previous
createBuilder