---
tags: help, example
order: 130
description: an init page that uses them
---
# Help: an init page, by example

A small, complete init page for a portfolio-tracking project, to copy from. A project's own init page is `Settings:Init` (open it from **Admin → Init**); this example lives in d2's help, so it isn't compiled or run here.

It does three things: records when the project last started, keeps a running count of holdings as they're added and removed, and notes each release.

    ---
    tags: spec
    ---
    # Init

    What this project keeps for itself.

    $class Stats (@key id: String, started: String?, holdings: Int?, lastRelease: String?)

    When the project starts (and whenever this page is saved):

    $when diesel.project.on.init {
      s = dom.find(cls = "Stats", key = "main") ?? {id: "main", holdings: 0}
      dom.upsert(cls = "Stats", entity = s + {started: isoDate(now())})
    }

    A holding added or removed: keep the count.

    $when diesel.entity.on.created (category, key) if (category is "Holding") {
      s = dom.find(cls = "Stats", key = "main") ?? {id: "main", holdings: 0}
      dom.upsert(cls = "Stats", entity = s + {holdings: (s.holdings ?? 0) + 1})
    }

    $when diesel.entity.on.deleted (category, key) if (category is "Holding") {
      s = dom.find(cls = "Stats", key = "main") ?? {id: "main", holdings: 0}
      dom.upsert(cls = "Stats", entity = s + {holdings: (s.holdings ?? 1) - 1})
    }

    After a release (Admin → Project):

    $when diesel.project.on.released (version) {
      s = dom.find(cls = "Stats", key = "main") ?? {id: "main", holdings: 0}
      dom.upsert(cls = "Stats", entity = s + {lastRelease: version})
    }

## How it works

- **The `spec` tag** makes the page compiled, like a Spec topic; the lines starting with `$` are the rules, the rest is notes for people.
- **`diesel.project.on.init`** runs when the project loads and every time you save the page: the place to declare what the project needs, since it runs again whenever you change it.
- **`diesel.entity.on.created`, `updated`, `deleted`** get the object's `category` (its class) and `key`; `if (category is "Holding")` picks the ones this rule is for. A rule's own writes don't set off more of these, so it can't loop.
- **`diesel.project.on.released`** gets the new `version`.
- Each run is a flow you can open under **Flows**; a mistake shows there and in the audit log, and never stops the project.

More: [[Help:LifecycleMessages]] for every lifecycle message and what it carries, and [[Help:Expressions]] for the expressions.
