Example: Inventory Tracker

A complete warehouse tracker on web pages: items, locations, an append-only movement ledger, a live stock table and low-stock alerts, where the ledger is always right.

Three storages, one of them append-only

  • items: sku, name, unit, reorder_level.
  • locations: warehouse, zone, bin.
  • movements: one append-only document per change.
Movement Document
{
  "_meta": {
    "cms": 1782166695055,
    "created": 1782166695,
    "entity": 1,
    "expiry": -1,
    "guid": "08193b4a5d6e7f8091a2b3c4d5e6f708192a3b4c5d6e7f8091a2b3c4d5e6f708",
    "removed": 0,
    "ums": 1782166695092,
    "update_hash": "5e0d38c7a1f294b6037ce8a51d9f27b046a3c81e",
    "updated": 1782166695
  },
  "actor_guid": "b9ee6a631b24e78d1aa48aef0dc067fff7bfbacd24a56ef4ed1f08e537d48b6bd02",
  "item_name": "essential cotton t-shirt",
  "location": "wh-1/zone-a/bin-04",
  "note": "po-10482",
  "quantity": 24,
  "sku": "TSHIRT-BLK-M",
  "type": "receive"
}

Movement types are receive, ship, adjust and transfer (two linked movements: out and in). Never update a movement: corrections are new adjust movements. It is the activity-log principle from Storage Examples.

Current stock can be read two ways

Ledger sum (exact)

Aggregate movements per sku and location when you need the precise figure.

Counter document (fast)

A stock storage with one document per sku and location. Every movement flow also applies mutations_increment_by_field (increment by field) through function_upsert_document_from_mutations (upsert document), with sku and location as guid_seed. Increments are applied sequentially per document (see mutations). Lists and alerts read only counters.

Audit job: a daily schedule_repeating_event (repeating event) recomputes the ledger sum against the counter and logs drift. You will catch a missed movement within a day, not at year-end.

The stock page updates one row at a time

web_page (new web page) with event stock and a custom scope warehouse. Search the stock storage with function_search (search), wire array into web_foreach (web for each) with the row template, and print it through web_render (web render). Each counter document carries what its row shows: sku, item_name, location, reorder_level and on_hand.

Stored template: stock-row
<tr class="{{if reorder_level > on_hand}}low{{/if}}">
  <td>{{sku}}</td>
  <td>{{item_name | capitalize}}</td>
  <td>{{location}}</td>
  <td>{{on_hand | number}}</td>
</tr>
Page template
<main>
  <h1>{{title}}</h1>
  <form data-action="receive">
    <label>SKU <input name="sku" required></label>
    <label>Location <input name="location" required></label>
    <label>Quantity <input name="quantity" type="number" min="1" required></label>
    <label>Purchase order <input name="source" required></label>
    <div data-error-for="_form"></div>
    <button type="submit" data-loading-text="Saving">Receive</button>
  </form>
  <table>
    <tbody id="stock-rows">{{{rows}}}</tbody>
  </table>
</main>

Each movement is one flow

Receive is idempotent

  1. Write the movement

    web_action (on web action) receive, then function_create_document_from_mutations (create document) with the source reference and sku (po-10482 plus the sku) as guid_seed.

  2. Skip a repeat

    Re-receiving the same reference makes the block answer success false with DOCUMENT_GUID_ALREADY_EXISTS. branch (branch) on it: answer with web_errors (web form errors) and leave the counter alone.

  3. Move the counter

    function_upsert_document_from_mutations with mutations_increment_by_field on on_hand.

  4. Update the page

    web_template (web template) with {{> stock-row}}, the updated object as data and its guid as key. branch on the upsert's created pin: a new row goes in with web_insert (web insert) on #stock-rows, an existing one is swapped with web_replace (web replace).

Ship is guarded by a lock

"Only if enough is left" is a read, a decision and a write, so it needs a lock around the sequence (see Locking and Concurrency).

  1. Claim

    function_claim_lock_wait (lock or wait) with auto_clear on, keyed on the sku and location.

  2. Check

    function_get_document (get document) reads the counter by guid; condition_gte (>=) compares on_hand with the quantity.

  3. Write

    Enough: create the ship movement and apply a negative increment. Not enough: hold it as a backorder movement instead of going negative.

Transfer and count check

  • Transfer: one flow writes the out-movement and in-movement as two linked documents sharing a transfer guid; the counters move in the same flow.
  • Count check: an adjust movement with the counted quantity difference. The counter follows the same path as everything else.

Low-stock alerts fire on the crossing

  1. Compare

    After every counter change, compare against the item's reorder_level.

  2. Alert once

    Crossing the threshold, and only then, creates an alert document and emails the buyer via the email pattern. Track the previous state on the counter to avoid alert spam.

  3. Close it

    An alert page lists open alerts sorted by severity. A button runs a web_action with a status mutation once the receive lands, and web_remove (web remove) takes the row away.

Next steps

E-Commerce Catalog Sell through this stock. Storage Examples The ledger and counter models in full. Repeating Events The daily drift-check scheduler. Actions Replacing, inserting and removing rows from a flow.

Frequently asked

How do I track current stock in RUAL?

Two ways: aggregate the movement documents per sku and location when you need the exact figure, or keep a counter document per sku and location that every movement flow updates with mutations_increment_by_field. Lists and alerts read only the counters, and a daily repeating event recomputes the ledger sum against the counter to catch drift.

How do I correct a wrong stock movement in RUAL?

Never update a movement: corrections are new adjust movements. The movements storage is append-only, so the ledger stays a complete, ordered history.

How do I build low-stock alerts in RUAL?

After every counter change, compare against the item's reorder_level, and only when the threshold is crossed create an alert document and email the buyer. Track the previous state on the counter so a stock level that stays low does not spam alerts.