05 / 06Intermediate
Blockchain components
Navigate to the blockchain folder
cd rell/srcThe key components of the blockchain dapp
The pagination utility comes from the FT4 library, so you need to import it first.
libs: ft4: registry: https://gitlab.com/chromaway/ft4-lib.git path: rell/src/lib/ft4 tagOrBranch: v1.2.0r rid: x"E96915FD7C9A5DD4E183D86F4A5B4BB5083A7CC1EDEB1465EDFFC926E67C93F6" insecure: falseYou store the data to be analyzed in an entity called product. Its structure works much like a table in a relational database.
entity product { name: text; description: text; quantity: integer; price: integer;}To populate the data, you use an operation called seed_data, which builds a list of products. It creates all the entities in a single batch with the statement create product (batch);.
operation seed_data() { // 100 bytes val name = "UltraPure Hydro Flask ProMax: Advanced Insulated Stainless Steel Bottle for Active and Everyday Use."; // 500 bytes val description = "Introducing the EcoSmart Water Bottle – your ultimate hydration companion! Made from premium, BPA-free materials, this bottle is designed for both functionality and style. With double-wall insulation, it keeps your beverages icy cold for up to 24 hours or piping hot for up to 12 hours. Its sleek, ergonomic design fits perfectly in your hand or cup holder. Featuring a leak-proof lid and a wide mouth for easy cleaning or adding ice cubes, it’s perfect for gym sessions, hikes, or daily commutes. Stay refreshed and eco-friendly with EcoSmart!"; // in total a size of 1 object is going to be 616 bytes val batch_amount = 10; // amount of batches created val batch_size = 10000; // the batch will have this number of entities to be created as a batch // total data size can be calculated // total_size = batch_amount * batch_size * object_size // the total size can be tuned with changing any parameter listed above. var batch = list<struct<product>>(); for (x in range(batch_amount)) { val shift_index = x * batch_size; for (y in range(batch_size)) { batch.add( struct<product>( name = name + shift_index + y, description = description, quantity = shift_index + y, price = shift_index + y ) ); } create product ( batch ); batch.clear(); print("saved", x); }}To read the products back from the node one page at a time, you call the query get_products_paginated. It relies on the cursor-based pagination provided by FT4.
query get_products_paginated(page_size: integer?, page_cursor: text?): paginator.paged_result { val before_rowid = paginator.before_rowid(page_cursor); val paginated_result = product @* { .rowid > (before_rowid ?: rowid(0)) } ( paginator.pagination_result( data = map_product($).to_gtv_pretty(), rowid = .rowid ) ) limit paginator.fetch_data_size(page_size); return paginator.make_page(paginated_result, page_size);}Data model
product { name: text; description: text; quantity: integer; price: integer;}Written for this site from Chromia’s public course. Code samples are the course’s own.