Batch Operations: Reference Sibling Operation Results

I can see that the Fibery API allows you to run batches of commands, which is really cool.

Is there some way to run a series of commands like this?:

(Pardon my psuedoJSON - I know this isn’t the correct syntax/schema for the Fibery API, but hopefully you understand the intent)

[
  {
    "command": "create-entity",
    "data": {...},
    "return": "SOME_KIND_OF_REFERENCE_TO_THE_FIRST_RESULTS"
  },
  {
    "command": "update-entity",
    "target": { "fibery/id": "$SOME_KIND_OF_REFERENCE_TO_THE_FIRST_RESULTS" },
    "data": {...}
  }
]

I assume that you can’t, and that you can only batch operations on the database in the state it’s in before ANY of the operations are run. But I thought I’d check, because FIbery continues to pleasantly surprise me, and you never know…

Hi there,
api/commands indeed accepts an array of commands, however there is a catch.

  • this commands will be executed sequentially
  • if one command fails it won’t stop execution
  • each command will run in a separate transaction

You may try fibery.command/batch, for example it’s used here Fibery API for creating Single Select and populate it with some select-items in one batch.

To achieve your behaviour with "SOME_KIND_OF_REFERENCE_TO_THE_FIRST_RESULTS" we usually recommend the following approach:

  • generate fibery/id upfront for each entity to be created. It should be UUID version 4 e.g. crypto.randomUUID() in nodejs.
  • then use this fibery/id in update / delete / add-collection-item commands.
// generate uuid e.g. 5b1b06f3-07ae-42a4-b2d4-ca00fb06ea79
[
  {
    "command": "fibery.command/batch",
    "args": {
      "commands": [
        {
          "command": "fibery.entity/create",
          "args": {
             "type": "CRM/Client",
             "entity": {"fibery/id": "5b1b06f3-07ae-42a4-b2d4-ca00fb06ea79"}
          },
        },
        {
          "command": "fibery.entity/update",
          "args": {
             "type": "CRM/Client",
            "entity": {
              "fibery/id": "5b1b06f3-07ae-42a4-b2d4-ca00fb06ea79",
              // ... ("field name": "field value") pairs to update
            }
          }
        }
      ]
    },
  }
]

Also please mind that you can set Reference values in Entity create command, so you don’t need to split it into several commands in that case, however it is not the case for adding Entity into some other Entity collection field.

Hope this helps.

Woah, you can generate the ID ahead of time??? That’s wonderful. Yeah, in this case, I’m trying to connect a collection reference field, but this is good to know for later.

The uuid utility function may be useful for you:
https://the.fibery.io/@public/User_Guide/Guide/Scripts-54/anchor=Utils-service-functions--7f5abb66-1887-40c2-a3ed-3ed22ac0292f

1 Like