Skip to content

Database Table

When managing forms in the FormCreate environment, a reasonable database design is crucial for storing form design rules and user-submitted data. The following content details several core database table designs to help you organize and manage data more effectively.

1. Forms Table (forms)

This table stores overall information for each form, including name, description, and design rules:

Field NameData TypeDescription
idINTPrimary key ID, auto-increment
titleVARCHARForm name, used to identify the unique identifier of the form
descriptionTEXTForm description information
rulesJSONForm's rules and overall configuration data of fields, usually contains configuration of multiple fields
optionsJSONForm configuration data (e.g., layout, size, global data, etc.)
created_atDATETIMEForm creation time
updated_atDATETIMEForm update time
sql
CREATE TABLE forms (
    id INT AUTO_INCREMENT PRIMARY KEY COMMENT 'Primary key ID, auto-increment',
    title VARCHAR(64) NOT NULL UNIQUE COMMENT 'Form name, used to identify the unique identifier of the form',
    description TEXT COMMENT 'Form description information',
    rules JSON COMMENT 'Form's rules and overall configuration data of fields, usually contains configuration of multiple fields',
    options JSON COMMENT 'Form configuration data (e.g., layout, size, global data, etc.)',
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT 'Form creation time',
    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'Form update time'
) COMMENT='Form information table';

Example Code:

Insert data object for form component, containing key information such as title, description, form configuration, etc., used to serialize and store form design data to the backend database.

ts
//rules is the component design rules of the form
//options is the form configuration
const insert = {
    title: options.formName || '',
    description: options.description || '',
    rules: JSON.stringify(rules),
    options: JSON.stringify(options),
}

2. Field Data Table (form_data)

This table stores specific values for each field submitted by users. Each form submission generates a new record for tracking form history.

Field NameData TypeDescription
idINTPrimary key ID, auto-increment
form_idINTForm ID, foreign key associated with forms table
field_idINTField ID, points to fields table
field_nameVARCHARField name, points to field in fields table
field_valueJSONField value submitted by user
created_atDATETIMEData creation time
updated_atDATETIMEForm update time
sql
CREATE TABLE form_data (
    id INT AUTO_INCREMENT PRIMARY KEY COMMENT 'Primary key ID, auto-increment',
    form_id INT NOT NULL COMMENT 'Form ID',
    field_id INT NOT NULL COMMENT 'Field ID, points to fields table',
    field VARCHAR(32) NOT NULL COMMENT 'Field name, points to field in fields table',
    value JSON COMMENT 'Field value submitted by user',
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT 'Data creation time',
    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'Form update time'
) COMMENT='User submitted form field data table';

Example Code:

Convert form submission data to a structure that can be inserted into the database. By iterating through each field of the form data, generate an array of objects containing field ID, field name, and serialized values, facilitating batch storage of form submission records.

ts
//formData is the form submission data
const inserts = Object.keys(formData).map(field => {
    return {
        field_id: getFieldId(field), //Query corresponding field_id through field
        field: field,
        value: JSON.stringify(formData[field]),
    };
})

Through the above database table structure examples, you can build a good data storage foundation for FormCreate-based applications, facilitating storage and management of form designs and user data.

FormCreate is an open-source project released under the MIT License. Free for personal and commercial use.