# Records a lifecycle hides

A query leaves out the records its entity's
[lifecycle](/docs/lifecycle.md) hides, such as enquiries in the bin.
`include_hidden: true` says it reads them too, beside the visible ones.
A bin view is that with a condition on the field the lifecycle sets, and
a guard that must count binned records says it inside `exists(...)`:

```jadpo
use desk/entities { Customer, Deskhand }

type EnquiryTitle = Text {
    min_length: 1
    max_length: 200
}

/// Something a customer asked for, kept in the bin for 30 days.
entity Enquiry {
    id: Uuid
    title: EnquiryTitle
    customer_id: Customer.id {
        on_delete: OnDelete.restricted
    }
    deskhand_id: Deskhand.id {
        on_delete: OnDelete.restricted
    }
    binned_at: Instant?

    lifecycle: {
        visible: binned_at == none
        transitions: {
            delete: {
                when: binned_at == none
                set: {
                    binned_at: clock.now
                }
            }
            restore: {
                reaches: hidden
                set: {
                    binned_at: none
                }
            }
        }
        purge: {
            after: 30d
            since: binned_at
        }
    }

    policy: {
        create: [Access.authenticated]
        read: [deskhand_id]
        update: [deskhand_id]
        delete: [deskhand_id]
    }
}
```

```jadpo
use desk/entities { Enquiry }
use desk/types { EnquiryRow }

/// The caller's enquiries in the bin, which `Enquiry.restore` brings back.
query binned_enquiries(caller: Principal.user) -> List<EnquiryRow> {
    from: Enquiry
    where: Enquiry.binned_at != none
    order: [Enquiry.title asc]
    limit: 50
    include_hidden: true
}
```

```jadpo
use desk/entities { Customer, Enquiry }
use desk/types { CustomerRow }

/// The customer, while they have no enquiries at all, binned ones included.
query without_enquiries(customer_id: Customer.id) -> CustomerRow? {
    from: Customer
    where: Customer.id == customer_id
    where: not exists(Enquiry.customer_id, include_hidden: true)
}
```

- **Only what the lifecycle hides.** `include_hidden: true` leaves out
  the entity's `visible:` and nothing else. Its read policy still holds
  in full, so above, a deskhand's bin holds only their own enquiries, and a
  call with no caller runs without policy, as every read does
  ([Calls with no caller](/docs/acting-as-a-user.md#calls-with-no-caller)). A
  person's record that erasure kept stays out: only `include_erased:
  true` reaches that ([The person's own record](/docs/erasure.md#the-persons-own-record)),
  and a query may say both.
- **Where it is said.** On a query, for the records `from:` reads, a
  list's, a page's, one record's or an aggregate's; and last inside
  `exists(...)`, for the related records:
  `exists(Enquiry.customer_id, include_hidden: true)`. An include's
  children still leave hidden records out, and a parent is read whether
  its lifecycle hides it or not
  ([Children and parents](/docs/children-and-parents.md)). It takes only `true`
  (`type.setting_value`), and only for an entity whose lifecycle has a
  `visible:` ([`query.include_hidden_unused`](/docs/errors/query.include_hidden_unused.md)).
- **Bringing one back.** A query reads hidden records; only a transition
  marked `reaches: hidden` changes one
  ([Restoring a hidden record](/docs/lifecycle.md#restoring-a-hidden-record)), so
  a bin view lists what `Enquiry.restore` can bring back.
- **A delete a binned record blocks.** A `restricted` reference holds
  whether the record holding it is hidden or not, so above, deleting a
  customer with only binned enquiries is refused with the conflict its
  `conflict:` maps, until purge removes them
  ([Conflicts](/docs/reads-and-writes.md)). The guard query says the same thing first,
  so an action can answer before it tries.
- **Shown where it is read.** `jadpo explain` lists, beside the entity's
  lifecycle, each query that reads its hidden records, and marks each
  route that runs one.
- **Its index.** The index a query saying `include_hidden: true` needs
  holds every record, not only those `visible:` lets through
  ([How the derived indexes are chosen](/docs/indexes.md#how-the-derived-indexes-are-chosen)).

A guess at another spelling, `include_deleted:` or `include_binned:`, is
`query.unknown_section`, whose `next:` names `include_hidden:`.
