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:
Parameter | Type | Description |
---|---|---|
builder | object | The builder definition. |
options | object optional | An optional partial object with initialization options. |
The options
parameter properties:
Property | Type | Description |
---|---|---|
initialData | object optional | The optional partial initial data. |
Returns
createBuilderStore
instantiates a builder store, providing a set of methods to operate with the store.
Property | Type | Description |
---|---|---|
getData | function | Retrieves the store's data. |
getSchema | function | Retrieves the schema from the store's data. |
getEntitiesAttributesErrors | function | Retrieves the entities' attributes errors from the store's data. |
getSchemaError | function | Retrieves the schema error from the store's data. |
setData | function | Sets the store's new data. |
subscribe | function | Subscribes to the store's events, returning a function () => void for unsubscribing. |
addEntity | function | Adds a new entity, returning the newly added entity instance. |
setEntityParent | function | Sets an entity's parent. |
unsetEntityParent | function | Unsets an entity's parent and moves it to the root. |
setEntityIndex | function | Updates an entity's index, either in the parent or in the root. |
setEntityAttribute | function | Updates an entity's attribute value. |
deleteEntity | function | Deletes an entity and all its children. |
validateEntityAttribute | function | An async function that triggers the validation of a specific attribute of a single entity. |
validateEntityAttributes | function | An async function that triggers the validation of all attributes of a single entity. |
validateEntitiesAttributes | function | An async function that triggers the validation of the attributes of all entities. |
resetEntityAttributeError | function | Removes the error of a specific attribute of a single entity. |
setEntityAttributeError | function | Sets the error of a specific attribute of a single entity. |
resetEntityAttributesErrors | function | Removes all errors of all attributes of a single entity. |
setEntityAttributesErrors | function | Sets all errors of all attributes of a single entity. |
resetEntitiesAttributesErrors | function | Removes all errors of all attributes of all entities. |
setEntitiesAttributesErrors | function | Sets all errors of all attributes of all entities. |
validateSchema | function | An async function that triggers the validation of the schema, returning the successfully validated schema or the reason for failure. |
setSchemaError | function | Sets the schema's error. |
resetSchemaError | function | Removes the schema's error. |
cloneEntity | function | Clones an entity along with all its children. |
getEntity | function | Retrieves a specific entity. |
builder | object | The builder definition used to instantiate the store. |
Data
The data of the builder store is an object containing the following properties:
Property | Type | Description |
---|---|---|
schema | object | Represents the schema, which contains the collection of all entities instances and their order. |
entitiesAttributesErrors | object | Represents the validation errors of various entity attributes. |
schemaError | unknown | Represents 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:
Event | Description |
---|---|
EntityAdded | An entity was added. |
EntityUpdated | An entity was updated. |
EntityAttributeUpdated | An entity's attribute was updated. |
EntityDeleted | An entity was deleted. |
EntityCloned | An entity was cloned. |
RootUpdated | The root was updated. |
EntityAttributeErrorUpdated | An entity's attribute error was updated. |
SchemaErrorUpdated | The schema's error was updated. |
SchemaUpdated | The schema was updated. |
DataSet | The data was manually set. |