# Linked apps: showing records that live in another app

Two apps can be linked so one shows records that live in the other. A roster app keeps the people; a schedule app shows those same people instead of keeping a second copy. The records stay in the app they live in. The owner links the two apps on the app's page; you only make a place for the records to show.

Use it when the owner says the records already exist in another of their apps, or should be kept in one place for several apps. Do not use it to keep a copy: there is no copy.

## 1. Declare where the records show

```json
"data": {
  "scope": "app",
  "collections": {
    "people": {
      "scope": "link",
      "fields": { "name": { "type": "string" }, "phone": { "type": "string", "optional": true } }
    },
    "shifts": { "fields": { "day": { "type": "date" }, "person": { "type": "string" } } }
  }
}
```

- `"scope": "link"` means: these records live in another app. The manifest never says which app. Not for device scope, and like every scope it is fixed after the first publish.
- **Field names and types must match the other app's records exactly.** Once the owner has linked the apps, this app's context says what was offered: `linked` is a list of `{ records, from, fields, level, shownAs }`, with the other app's field names and types in `fields`. Build from that. Before then, ask the owner which app and which records; if you built that app, read its manifest. A field the other app leaves optional must be `"optional": true` here too.
- Declare only the fields this app uses. The rest never reach it.
- No `ref` fields in a linked collection, and no `ref` in another collection may point at it. To point at a linked record, keep its `id` in a `string` field and show nothing when no record has that id: the other app can remove a record without this one knowing.
- A `select` or a list with options may hold a value this app does not list (the other app can add options). Show the value as it is.

## 2. Read and change them like any other collection

`nerd.data.list("people")`, `nerd.data.observe(...)`, `create`, `update`, `remove` and the kit's lists and forms work unchanged, and changes made in either app arrive live in both.

**What a person may do is not yours to decide, and differs per person.** The owner of the other app chooses the most this app's people may do: look only, add entries and change only their own, or change anything. So, exactly as for a shared collection:

- Offer add only when the collection is in `ctx.data.writable`.
- Offer edit and remove on a record only when `nerd.data.canChange(collection, record)` says so (`ctx.data.ownOnly` names the collections where a person changes only what they added).
- Build the screen to work with no add button at all: for many people a linked collection is look only.

A write is checked against the other app's own fields. Send only the fields you declared. If the other app requires a field you did not declare, people can change records here but not add them, so declare every required field.

In the other app, a record's history names this app on the changes made here (`via` on a history entry).

**Pictures.** A linked collection may declare a `file` field the other app has (set `"files": true` in `manifest.data`). `nerd.files.show(img, record.photo)` shows it, and the kit's forms and `nerd.ui.photo` show it without offering to change it: a linked record's picture is added and changed in the app the record lives in. Do not upload a file and save its id on a linked record; the save is refused.

Comments, reminders (`nerd.notify`) and record history are not available on linked records here.

## 3. Say where the link stands

`ctx.data.links.people` is `{ state, from }`, and `nerd.links.observe(fn)` follows it. Draw every state:

| state | What it means | What to show |
| --- | --- | --- |
| `"unbound"` | The owner has not linked an app yet. The collection is empty. | A plain line: "No app is linked yet. The owner links one on this app's page." |
| `"on"` | Linked. `from` is the other app's name. | The records. Say where they come from once: "From Roster". |
| `"paused"` | Linked, and stopped for now (the other app is not live). Empty. | "Not available right now." |
| `"hidden"` | Linked, and this person is not one of the people the records are shared with. Empty. | "You don't have access to these." |

Never treat an empty linked collection as "no records yet": check the state first.

## 4. Previews and the check

In a preview a linked collection is an ordinary list. Give it sample records in `sampleData` under its own name; the preview reports it as `"on"`, from "Another app".

Look at the other states too: the check's `{ "link": ["people", "unbound"] }` step empties the collection and sets that state (also `"paused"`, `"hidden"`, and `"on"` to bring the records back). Take a screenshot after it. An app that shows a blank screen there is not done.

## 5. Tell the owner what to do

Nothing shows until the owner links the apps, and you cannot do it for them. After they publish, tell them in these words:

1. "Open the page of the app the records live in, find "Linked apps", choose the records and this app, and press Link apps." If that app belongs to someone else, its owner does this and chooses "Someone else's app": they get an address to send, and the owner of this app opens it and accepts.
2. "Then open this app's page, find "Linked apps", and choose where to show them."

It works in either order: the apps can be linked first, and you then build to fit what `linked` says.

They choose what people here may do with the records (look only, add their own, or change anything) and who sees them: only people who are in both apps, or everyone invited to this one. Nobody sees linked records without signing in, and either app's owner can unlink at any time. If the owner does not see "Linked apps" on the app's page, linking apps is not turned on for them yet: say so, and build the screen so it reads well empty.

The build guide is https://nerdout.com/workshop/sdk/guide.md.
