5 Data Persistence
This section provides instructions for creating and setting up a table for user data, creating an instance of the table, adding new rows as well as instructions for adjusting and deleting them. In addition, there are instructions for setting up an ABI file and testing the installed contract.
To store user data, a contract can be created to be used as an address book. Despite the fact that this use of data is not very practical this operation illustrates a method of storing data in CyberWay without any distortion of business logic.
5.1 Create a new directory for the address book contract
5.2 Create file
Fill the file with the following content:
5.3 Create address book data structure
Before a table can be configured and created, it is necessary to create an address book data structure. Since this is an address book, the table must contain information about people, and the contract itself must store some important data for each record or person. A possible person
structure is the following:
The structure of the table cannot be changed as long as it is filled out with data. If necessary, change data structure of the table. For that you must first delete all its rows.
5.4 Configure table indexes
The table has to be configured after determining its data structure. The first step is to define eosio::multi_index
constructor to use the table data structure. Add the following line to the created class:
This definition of multi_index
creates a table called people
, which allows the following:
to use the
_n
operator to determine the type ofeosio::name
and also to use this name for the table. This table will contain several descriptions of individual «person»s, so you can assign the name «people» to the table;to transfer the structure for an individual defined in the previous step;
to declare the type of this particular table. This type will be used to create instances of this table;
to configure and accommodate the table indexes, which are to be mentioned later.
At this stage the file should have the following form:
5.5 Create class constructor
When working with C++ classes, the first open method to be created is the constructor. The constructor determines the initial setting of the contract. The contracts allow you to expand the class. To do this, you need to initialize the parent class of the contract with the code name of the contract and the recipient.
Параметр:
name code
— a contract account name.
5.6 Create table entries
5.6.1 The following conditions must be met:
Any changes are added to the address book by the user himself only.
For the ease of use, the contract should provide an ability to create or modify a row of table in one action.
5.6.2 Define the user action to add a record or modify a table. This action should take any values necessary to create or edit an entry in the table.
Format the method. For further convenience and simplicity of the interface, the same method will perform both the creation and the modification of lines. It is given the name «upsert» (as a combination of «update» and «insert»).
Only the user has the right to control his own entry in the table. To perform this, use the require_auth
method provided by eosio.cdt
. This method takes one argument — the name of the account performing the transaction. The upsert
method is:
5.6.3 Create a table. The multi_index
table is configured and declared as address_index
. Creating an instance of a table requires two arguments:
contract account code;
scope of the contract.
You also need to set an iterator for a frequent use.
The table will be the following:
5.6.4 Write the logic for creating or modifying a table, the logic for determining the existence of a user. To do this, you can use the table's method called find
, passing the user parameter. The find
method returns a table iterator.
5.6.5 Create an entry in the table using the emplace
method. This method takes two arguments: the «payer» of this record, which pays for the use of storage and the callback function.
The callback function for the emplace
method should use lambda to create a link. Inside a body, assign values to the rows for the upsert
.
5.6.6 To modify an entry in a table, you can use the modify
method with the following arguments:
iterator — an iterator (previously defined);
user — a user who is the payer;
a function to handle the table modification.
Upon completion of the instructions in this clause, the address book contract will have a functional action that will allow the user to create or modify a row in the table.
5.7 Deleting records from the table
Create a public method in your address book and make sure that it includes ABI-ads. Also create require_auth
to check the user's right to perform the action. Only the owner of the record can change his account. In the address book, each account has only one entry.
To delete a record, you need to set an iterator using find
and call the erase
method
Upon completion of the instructions in this clause, the address book contract will have a functional action that will allow the user to create, modify or delete any entry in the table. However, the contract is not yet ready for compilation.
5.8 Edit the ABI file
Before you compile the contract you need to edit the addressbook.cpp
file.
5.8.1 At the end of the file, place the macro EOSIO_DISPATCH, specifying the name of the contract, as well as the «upsert» and «erase» actions.
This macro sends calls to specific methods in this contract. The macro ensures the compatibility of the cpp-file with the wasm EOSIO interpreter. Failure to include this declaration may lead to an error in the deployment of the contract.
5.8.2 Declare actions in the ABI file.
Although eosio.cdt includes an ABI generator, you also need to manually modify the generated ABI file. At the top of the description of the functions upsert
and erase
add a declaration in the following form:
This declaration allows you to extract action arguments and to create the necessary ABI structure descriptions in the generated ABI file.
5.8.3 Declare tables in an ABI file. Matching the previous paragraph instructions change the declaration for the table:
The final state of the contract with the address book before compilation is the following:
5.9 Compile a contract
5.10 Describe the index of the primary key of the table in the ABI file
The generator creates a table without indices during the compilation of the ABI file:
You must edit the table in the ABI file, keeping only the name
and type
unchanged and replacing the remaining fields with the following description of the primary key index:
5.11 Install a contract
5.11.1 Create an account for the contract by executing:
5.11.2 Set an addressbook contract by executing:
5.12 Test an established contract
5.12.1 Check an addition of a row to a table for a user named alice
.
As an output the following information should appear:
5.12.2 Verify that the alice
user cannot create an entry for another user by running:
The require_auth
method in a contract should not allow another user to create (or modify) strings. As a result, you should receive an error message:
5.12.3 Get a user record with the alice
name.
As an output the following information should appear:
5.12.4 Verify that the user with the name alice
can delete the record from the table by executing::
As an output the following information should appear:
Check for no record by running:
The following Information should appear:
Last updated