Batch actions with app items

When you need to process a large number of app items in a single request, you can write a script using batch methods for adding or updating app item fields:

  • Application.batch().save() — for batch adding app items;

  • Application.batch().update() — for batch updating fields of app items. Note: A maximum of 10000 items can be processed in a single request. This limitation applies to all mentioned methods.

    Batch adding of items

    Using Application.batch().save(), you can perform the following actions in a single request:

  • Save new items to the database.

  • Update the fields of existing items. In this case, specify their identifiers to locate the required items, make changes to them, and save the edited items to the database. When specifying the items to be created, the method allows you to use:

  • An array: To create a defined number of items;

    await Application.batch().save().items(orders).all(); // Here `orders` is an array of app items with modifications
    
  • A generator: To work with large volumes by dynamically creating items one at a time until the specified number is reached, then saving them to the database. This avoids overloading the worker service memory on the server..

    await Application.batch().save().items((function*(){ // Define package saving of app items and in the items method recive the items created by the generator function
      for (var i = 0; i < 500; i++) { // Perform the loop 500 times to create 500 items
          var item = Application.create(); // Each time create an item
          yield item // Return the created item from the generator
      }
    })()).all(); // Save all items
    

    To learn more about Application.batch().save() see the following articles:

  • [[Applicationbatchsaver | App item saving object]];

  • [[Applicationbatchsavebuilder |App item batch saving configuration objects]];

  • Batch actions with app items in a script .

    Batch updating of app items

    The Application.batch().update() method allows you to update multiple fields in a single request. You can:

  • Define rules for updating;

  • Specify criteria for finding the items to be updated;

  • Limit the number of items to be updated. When writing a script with Application.batch().update() вyou can define criteria for the items to update using the set() method. In this method:

1.\tSpecify the name of the field to update. Only existing app fields can be selected. 2.\tDefine what to do with this field:

  • Assign a specific value. For a Number type field you can only specify a number, and for a String field only text etc.;
    Application.batch().update().set('numberField', (f, op) => 1); // OK. The constant matches the `numberField`type – Number
    Application.batch().update().set('numberField', (f, op) => ''); // Error. A string constant is incompatible with the Number type
    
  • Assign the value of another field of the same type;
    Application.batch().update().set('numberField1', (f, op) => f.numberField2); // OK. The types of app fields `numberField1` and `numberField2` are numbers
    Application.batch().update().set('numberField1', (f, op) => f.strField); // Error. The string field`strField` cannot be matched to the number field `numberField1`
    
  • Perform an operation on the field value:
  • Increment a Number field by a specific value;
    Application.batch().update().set('numberField', (f, op) => op.inc(f.numberField, 1)); // Increment the `numberField` value by 1
    
  • Concatenate multiple String fields;
    Application.batch().update().set('strLocation', (f, op) => op.concat(f.strCountry, ',', f.strCity)); // The `strLocation` field will update as '{strCountry}, {strCity}', i.e. it will store two values separated by comma: country, city
    
  • remove an item from a Manyt field;
    Application.batch().update().set('users', (f, op) => op.remove(user)); // Remove the user
    
  • add an item to the Many fields.
    Application.batch().update().set('users', (f, op) => op.push(user, user1)); // Add two users to the array end. The type of the `user`, `user1` fields must be of the same type as the single item of the `users` field
    
    Important notes for using the Application.batch().update() method :
  • A field can only be updated once in a single request.
  • If the script does not specify the number of items to update, the default is 10 items, with a maximum limit of 10000 items.
  • Actions defined in the set() methods of a single request are independent of each other, so their order does not matter. Example:
    var rows1 = await Application.batch()
         .update()
         .set('number', _ => 100)
         .set('another_number', f => t.number)
         .all();
    var rows2 = await Application.batch()
         .update()
         .set('another_number', f => t.number)
         .set('number', _ => 100)
         .all();
    // In both cases, `number` = 100, а `another_number` = value of the `number` field
    
    For more details on using Application.batch().update() refer to:
  • [[Applicationbatchupdater | App item update object]];
  • [[Applicationbatchupdatebuilder | App item batch update configuration object»]];
  • Batch actions with app items in a script.