Skip to main content

Insert data

This chapter provides a detailed explanation and step-by-step guide for inserting data into an indexed composition in Inery.

Before Proceeding

Ensure that you are familiar with the indexed composition definitions covered in the Instantiate Composition section.

Data Insertion Process

Inserting data into composition involves verifying if the target object already exists within the composition. If it does not exist, the object is added using the emplace method. Below are the key steps:

  • Verify if the Object Exists: Use an iterator to check for the presence of the object.
  • Insert the Object: Insert the object using the emplace method if it is not already in the indexed composition.

Verify Ownership

Verify that the user calling the action is authorized. Use the require_auth method to enforce this security check.

[[inery::action]] void database::set( name owner, uint64_t data ) {

require_auth(owner); // Private key authorization of database owner
}
info

owner parameter is inery::name type pointing to the database owner on the ledger.
require_auth() function check whether action signature used private key of owner database.

Check Primary Key Uniqueness

Define logic for checking if the owner already exists in container composition.

Add custom error handling and proceed with insertion only if all checks pass.

Full Action for Setting Initial Owner and Data Records
[[inery::action]] void database::set( name owner, uint64_t data ) {
// Ensure that the action is authorized
require_auth(_self); // Replace \_self with the authorized owner, if needed

auto itr = container.find(owner.value);

// Custom error handling if owner already exists
check(itr == container.end(), "Error: Owner already exists in the composition.");

// Insert the owner into the indexed composition
container.emplace(_self, [&]( auto& u ) {
u.owner = owner; // Set the owner name
u.data = data; // Initialize data
});
}
Best Practices
  • Centralized Logic: Encapsulate reusable code into private methods for better maintainability.
  • Secure Authorization: Always enforce require_auth to restrict unauthorized access.
  • Descriptive Errors: Use check to provide informative messages for better debugging.
  • Data Integrity: Ensure all object attributes are initialized during insertion.