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

Product Document
{
  "_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 the default_divide_by system setting to 100 so pricing inputs are stored in cents too.
  • Everything stored lowercase, so case-sensitive search just works. Templates use capitalize and upper for display.

The storefront is two pages and a search box

The category page

  1. Add the page

    web_page (new web page) with event shop/:category and scope *public. Read category from its params pin and offset from its query pin with object_field_getter_multiple (get fields), on a number pin.

  2. Query the products

    function_search (search) with a term filter on category and status = active, sorted by name with query_sort_field (sorting), limit 24 and that offset.

  3. Render

    web_foreach (web for each) with the row template below into a custom pin products on web_render (web render), plus a custom pin offset.

Row template on web_foreach
<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>
Page template
<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.

Page template: the product
<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.

  1. Claim the product

    web_action buy, then function_claim_lock_wait (lock or wait) with auto_clear on and a key of stock- plus the product guid. Two checkouts of different products never wait on each other.

  2. Read fresh

    function_get_document (get document) by guid. Not a search: a search can lag behind a write, a read by guid cannot.

  3. Check the variant

    array_get_objects_by_field_value (find by key - value) finds the variant by sku in variants; condition_gte (>=) compares its stock with the quantity, into a branch.

  4. Decrement and record

    On true, mutations_array_upsert (upsert item to array) with match key sku and the lowered stock, through function_update_document_mutations (update document). Create the order with the product embedded as a snapshot, then web_redirect (web redirect) to the order page.

  5. Hold on failure

    On false, hold the order as backorder instead of rejecting the customer, and say so with web_update (web update).

Restock takes the same locked path with a higher number. One flow, both directions.

The admin side is a separate blueprint

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.