Example: E-Commerce Catalog
A complete catalog on web pages: category pages, product pages with variants, a search box, stock that cannot be double-sold, and an admin section.
The product document carries its variants
{
"_meta": {
"cms": 1780531200118,
"created": 1780531200,
"entity": 1,
"expiry": -1,
"guid": "c45797061f2c15a75361042244385e96419d230ab804d7fa08cd52981fd78192",
"removed": 0,
"ums": 1782166695204,
"update_hash": "f1a9c2d47b6035e8419dc0a7532be96481cd30fa",
"updated": 1782166695
},
"category": "apparel",
"image": "/static/products/tshirt-black.webp",
"name": "essential cotton t-shirt",
"price": 2490,
"sku": "TSHIRT-BLK-M",
"status": "active",
"variants": [
{
"size": "m",
"sku": "TSHIRT-BLK-M",
"stock": 14
},
{
"size": "l",
"sku": "TSHIRT-BLK-L",
"stock": 3
}
]
}- Variants live inside the product. A product page reads one document and never joins (see catalog model).
- Price in cents, so there are no float rounding bugs. A template writes it with
{{price | divide 100 | money "EUR"}}. Set thedefault_divide_bysystem setting to100so pricing inputs are stored in cents too. - Everything stored lowercase, so case-sensitive search just works. Templates use
capitalizeandupperfor display.
The storefront is two pages and a search box
The category page
Add the page
web_page(new web page) witheventshop/:categoryand scope*public. Readcategoryfrom itsparamspin andoffsetfrom itsquerypin withobject_field_getter_multiple(get fields), on a number pin.Query the products
function_search(search) with a term filter oncategoryandstatus=active, sorted by name withquery_sort_field(sorting),limit24 and thatoffset.Render
web_foreach(web for each) with the row template below into a custom pinproductsonweb_render(web render), plus a custom pinoffset.
<li>
<a href="/products/{{sku}}">
<img src="{{image}}" alt="{{name}}" loading="lazy">
<strong>{{name | capitalize}}</strong>
<span>{{price | divide 100 | money "EUR"}}</span>
</a>
</li><main class="shop">
<h1>{{title}}</h1>
<input name="q" type="search" placeholder="Search products"
data-action="search" data-trigger="input" data-debounce="300">
<ul id="products" class="grid">{{{products}}}</ul>
<nav>
{{if offset > 0}}
<a href="?offset={{offset | minus 24}}">Previous</a>
{{/if}}
<a href="?offset={{offset | default 0 | plus 24}}">Next</a>
</nav>
</main>The search box
web_action (on web action) search reads q from its form pin. query_bool_simple_query_string_field (simple query string) on name goes in query_bool_must (must), with the active filter in query_bool_filter (filter), merged by query_and (multiple). Answer with web_foreach and web_update (web update) on #products. For prefix matching while typing, use query_bool_prefix_string_field (value) instead; see search optimization.
The product page
web_page with event products/:sku. Fetch with function_search_single_result (search single) on sku, and branch (branch) on has_hits: web_render the product, or the same block with status 404. Variants render from the embedded array, with out-of-stock sizes disabled. Open and close the tag inside each branch of an {{if}}, or the template does not compile.
<main class="product">
<img src="{{product.image}}" alt="{{product.name}}">
<h1>{{product.name | capitalize}}</h1>
<p>{{product.price | divide 100 | money "EUR"}}</p>
<form data-action="buy">
<input type="hidden" name="product" value="{{product._meta.guid}}">
<select name="sku" required>
{{for v in product.variants}}
{{if v.stock > 0}}
<option value="{{v.sku}}">{{v.size | upper}}</option>
{{else}}
<option value="{{v.sku}}" disabled>{{v.size | upper}} (sold out)</option>
{{/if}}
{{/for}}
</select>
<input name="quantity" type="number" min="1" value="1" required>
<div data-error-for="_form"></div>
<button type="submit" data-loading-text="Ordering">Buy</button>
</form>
</main>A lock makes double-selling impossible
An update block locks the document for the write, not for your decision. Reading stock, deciding and writing back is a sequence another checkout can slip into, so the whole sequence goes under a lock. See Locking and Concurrency.
Claim the product
web_actionbuy, thenfunction_claim_lock_wait(lock or wait) withauto_clearon and a key ofstock-plus the product guid. Two checkouts of different products never wait on each other.Read fresh
function_get_document(get document) by guid. Not a search: a search can lag behind a write, a read by guid cannot.Check the variant
array_get_objects_by_field_value(find by key - value) finds the variant byskuinvariants;condition_gte(>=) compares itsstockwith the quantity, into abranch.Decrement and record
On true,
mutations_array_upsert(upsert item to array) with match keyskuand the lowered stock, throughfunction_update_document_mutations(update document). Create the order with the product embedded as a snapshot, thenweb_redirect(web redirect) to the order page.Hold on failure
On false, hold the order as
backorderinstead of rejecting the customer, and say so withweb_update(web update).
Restock takes the same locked path with a higher number. One flow, both directions.
The admin side is a separate blueprint
- Product management pages get a
catalog_managercustom scope. See User Authentication. - Price and name changes go through
function_update_document_mutationsfrom aweb_action, answered byweb_replace(web replace) on the row. Images go through the pipeline in File Upload & Processing. - Discontinuing is a status change, not a delete: old orders keep their embedded snapshots intact.
Next steps
Tutorial: A Small Webshop Build a shop with a cart step by step. Storage Examples The catalog and order models in full. Inventory Tracker Stock-focused variant of this example. Creating a REST API Expose the catalog to other systems.Frequently asked
How do I model product variants in RUAL storage?
Keep variants inside the product document as an embedded array, so a product page reads one document and never joins. Render the variants from that array and disable out-of-stock sizes in the UI.
How do I prevent double-selling stock in RUAL?
Claim function_claim_lock_wait on the product, read it again, check stock >= quantity, and only then write. A check on what the page showed earlier proves nothing, and on the failure path you hold the order as backorder instead of rejecting the customer.
Why store prices in cents in a RUAL catalog?
Storing the price in cents with default_divide_by = 100 avoids float rounding bugs, and the display divides it back. The same model stores names in lowercase so case-sensitive search just works.