Visual Elements

System is looking for solutions in Knowledgebase, Blogs, AI, and other places

Saving Files Individually in the Database Using API Workflow

6. Saving Files Individually in the Database Using API Workflow

When users select multiple files, you may want to save each file separately in the Bubble database. This is achieved using an API Workflow executed "on a list," allowing each item to be processed individually.

6.1 What is an API Workflow in Bubble?

  • Definition :
    An API Workflow (or Backend Workflow) is a server-side workflow in Bubble. It can be called from the client side (e.g., using "Schedule API Workflow on a List") to perform operations like creating or modifying data in the database without blocking the user interface.

6.2 Creating the API Workflow

  1. Access Backend Workflows :
    Open your application in the Bubble editor and go to the "Backend Workflows" tab (or "API Workflows").

  2. Create a New Endpoint :
    Click "New API Workflow" and name it, e.g., saveFile.
    Enable the option "Expose as a public API endpoint" so it can be called from other workflows.

  3. Define Parameters :
    Add the following parameters (configuring the correct type for each):

    • file_url (text)
    • file_id (text)
    • file_size (number)
    • file_name (text)
    • file_extension (text)
    • mime_type (text)
    • thumbnail (text)

    Each parameter will represent a field of the file obtained from the picker.

  4. Configure the Creation Action :
    Inside the API Workflow, add a "Create a new thing" action.
    Select the data type (e.g., "File," "Document," or the name of the table in the database).
    Map each parameter to the corresponding fields of the record being created.


6.3 Configuring "Schedule API Workflow on a List"

To execute the API Workflow on each item in a list:

  1. In the Client-Side Workflow :
    When the file_selected event is triggered, add a "Schedule API Workflow on a List" action.

  2. Select the List :
    Specify the list of files to process. This list may come from an exposed state (e.g., selected_files) containing objects with file fields.

  3. Map Parameters :
    For each parameter in the API Workflow, assign the corresponding value from the Current item's list. For example:

    • file_url: Current item's file_url
    • file_id: Current item's file_id
    • file_size: Current item's file_size
    • file_name: Current item's file_name
    • file_extension: Current item's file_extension
    • mime_type: Current item's mime_type
    • thumbnail: Current item's thumbnail

6.4 Practical Example

Suppose:

  • The plugin exposes a state called selected_files, which is a list of objects. Each object includes: file_url, file_id, file_size, file_name, file_extension, mime_type, and thumbnail.

Steps:

  1. Configure the API Workflow :

    • Endpoint : saveFile
    • Parameters : file_url, file_id, file_size, file_name, file_extension, mime_type, thumbnail.
    • Action : Create a new record in the database by mapping:
      • URL = file_url
      • ID = file_id
      • Size = file_size
      • Name = file_name
      • Extension = file_extension
      • MIME Type = mime_type
      • Thumbnail = thumbnail
  2. In the Client-Side Workflow :

    • When the file_selected event is triggered, add the "Schedule API Workflow on a List" action.
    • Configure the action:
      • Endpoint to execute: saveFile
      • List to execute on: selected_files
      • Parameter Mapping: Assign each parameter (file_url, file_id, etc.) to the corresponding value of Current item's.

6.5 Notes and Best Practices

  • Data Validation :
    Ensure that the data obtained from the picker is valid. Although the plugin includes validations, the API Workflow can include additional checks.

  • Error Handling :
    Configure actions in the API Workflow to handle errors, such as notifying the administrator or retrying the operation in case of failure.

  • Scalability :
    Using "Schedule API Workflow on a List" allows processing a large number of files independently on the server, avoiding client-side blocking.

  • Auditability :
    Consider extending the API Workflow to log additional information about each operation, facilitating auditing and troubleshooting.

  • Testing :
    Perform thorough testing to ensure each file is saved correctly in the database.


Was this page helpful?

Thank you for your feedback!

Comments (00)

Leave a comment

Sign in to post your comment or Sign Up if you don't have any account.

Ai Replying

We use cookies to enhance your experience on our website. By continuing to browse, you agree to our use of cookies. Learn more in our Privacy Policy

Accept All Reject All

Support, Knowledgebase, Live Chat, and CRM.

Get In Touch

Need More Help