Skip to main content

Write contract

Include library

Include necessary libraries, inery.hpp is mandatory.


#include<inery/inery.hpp>
#include<string>

using namespace inery;
using std::string;

Define class

class represent value contract, which inherits from inery::contract class

class [[inery::contract]] inrcrud : public inery::contract {
public:
using inery::contract::contract;
}

Make custom structure

Structure represent object type that will be stored in multi index tables Define structure within a private scope of class

  private:
struct [[inery::table]] record {
uint64_t id;
name owner;
string data;
uint64_t primary_key()const { return id; }
};

Define inery table

defined custom type of multi index table which can hold up to 16 additional indexes for faster and optimised query. In example below by default we use only one index defined as id. every index must be number type uint64_t, uint128_t , double ...


typedef inery::multi_index<"records"_n, record> records;

Make functions

Functions will be responisble for manipulating data from database tables functions represent actions that is database operations

Create

        [[inery::action]] void create( uint64_t id, name user, string data ) {
records recordstable( _self, id );
auto existing = recordstable.find( id );
check( existing == recordstable.end(), "record with that ID already exists" );
check( data.size() <= 256, "data has more than 256 bytes" );

recordstable.emplace( _self, [&]( auto& s ) {
s.id = id;
s.owner = user;
s.data = data;
});

print( "Hello, ", name{user} );
print( "Created with data: ", data );
}

Read

         [[inery::action]] void read( uint64_t id ) {
records recordstable( _self, id );
auto existing = recordstable.find( id );
check( existing != recordstable.end(), "record with that ID does not exist" );
const auto& st = *existing;
print("Data: ", st.data);
}

Update

        [[inery::action]] void update( uint64_t id, string data ) {
records recordstable( _self, id );
auto st = recordstable.find( id );
check( st != recordstable.end(), "record with that ID does not exist" );


recordstable.modify( st, get_self(), [&]( auto& s ) {
s.data = data;
});

print("Data: ", data);
}

Delete

            [[inery::action]] void destroy( uint64_t id ) {
records recordstable( _self, id );
auto existing = recordstable.find( id );
check( existing != recordstable.end(), "record with that ID does not exist" );
const auto& st = *existing;

recordstable.erase( st );

print("Record Destroyed: ", id);

Final Contract

#include <inery/inery.hpp>
#include <inery/print.hpp>
#include <string>

using namespace inery;

using std::string;

class [[inery::contract]] inrcrud : public inery::contract {
public:
using inery::contract::contract;


[[inery::action]] void create( uint64_t id, name user, string data ) {
records recordstable( _self, id );
auto existing = recordstable.find( id );
check( existing == recordstable.end(), "record with that ID already exists" );
check( data.size() <= 256, "data has more than 256 bytes" );

recordstable.emplace( _self, [&]( auto& s ) {
s.id = id;
s.owner = user;
s.data = data;
});

print( "Hello, ", name{user} );
print( "Created with data: ", data );
}

[[inery::action]] void read( uint64_t id ) {
records recordstable( _self, id );
auto existing = recordstable.find( id );
check( existing != recordstable.end(), "record with that ID does not exist" );
const auto& st = *existing;
print("Data: ", st.data);
}

[[inery::action]] void update( uint64_t id, string data ) {
records recordstable( _self, id );
auto st = recordstable.find( id );
check( st != recordstable.end(), "record with that ID does not exist" );


recordstable.modify( st, get_self(), [&]( auto& s ) {
s.data = data;
});

print("Data: ", data);
}

[[inery::action]] void destroy( uint64_t id ) {
records recordstable( _self, id );
auto existing = recordstable.find( id );
check( existing != recordstable.end(), "record with that ID does not exist" );
const auto& st = *existing;

recordstable.erase( st );

print("Record Destroyed: ", id);

}

private:
struct [[inery::table]] record {
uint64_t id;
name owner;
string data;
uint64_t primary_key()const { return id; }
};

typedef inery::multi_index<"records"_n, record> records;
};