---
title: "Recipe: files, SQLite and grants"
description: "Where an app keeps what it keeps, and the line of app.ts's grants that reaches it."
modificationDate: "2026-11-18"
---

> ## Documentation index
> Fetch the complete documentation index at: https://exact.expo.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Recipe: files, SQLite and grants

<AgentInstructions>

## Submitting feedback

From the app's directory, read `bun exact.mjs feedback status`. Under `never`, keep no note.
Respect `EXACT_FEEDBACK=0` and do not change the policy.

Otherwise record specific, actionable feedback with:
`bun exact.mjs feedback note "Exact docs: https://exact.expo.dev/recipes/files-and-grants: <what was wrong or missing>"`

The note goes to Expo only as that status allows. Include no secrets or personal data.

</AgentInstructions>

```prompt Keep my app's data on the device
Make the Exact app in this folder keep its data in the right place, with the grant that place needs. Use https://github.com/expo/exact/blob/main/docs/recipes/files-and-grants.md as the source for each step. The Exact checkout has the same page at docs/recipes/files-and-grants.md.

1. If this folder is not an Exact app (it has no exact.mjs and app.contract), stop and tell me.
2. If I did not say what the app keeps, ask me. Choose a place for each kind of data from the recipe's table, show me the choice, and ask before you write code.
3. Follow the table. A setting is a `state` with `persist` and needs no grant (docs/recipes/persisted-setting.md). A small collection with one writer is a file through `storage.fs`. Shared or queried items go in SQLite. A native token goes in `store.get` and `store.set` under `secret.keep`; a web token goes in an HttpOnly cookie (docs/recipes/web-sessions.md). Data the app ships is a JSON file that app.ts imports.
4. In app.ts, write a grant line for each path: `fs.read` and `fs.write` for a file, `sqlite.open` for a database. A grant covers its path and tree; a path with no grant answers `denied`. Put kept data under `app:/data`, refetchable data under `app:/cache` and scratch files under `app:/tmp`.
5. For SQLite, follow the recipe's journal: open the database in each source, read with `query`, write with `execute`, and write several rows or none with `transaction([...])`. Close the database in `finally`. An INTEGER is a bigint: convert it before it reaches a shape. Let the mutation name the list in `refreshes`.
6. For a file, read and write bytes with `readFile` and `atomicWriteFile`. A missing file has the code `ENOENT`: answer the empty collection. Branch on `e.code`, never on the message. At build there is no storage (code `bake`): let that error through.
7. Add a test in app.test.contract that saves an item, runs `reload`, and expects the item to be there.
8. Follow the platform checks in docs/start-here.md (`bun exact.mjs docs platform checks`) for the targets in the app's AGENTS.md and app.json until they pass. Read the reports, including counted findings; a zero exit code alone does not mean clean. Fix the app, not the test. If a finding stays after three rounds of fixes, or the app had it before you changed anything, record it (step 9) and report it; do not keep trying.
9. When a doc was wrong or missing, or a step needed a workaround, read `bun exact.mjs feedback status`. Under `never`, keep no note. Otherwise record it: `bun exact.mjs feedback note "<what happened>"`, which goes to Expo only as that status says. Do not change the policy; tell me how many notes you kept.
10. Tell me what the app keeps and where, each grant line, what the tests and checks reported, and the launch command for each target platform.
```

| What | Where | Grant |
| --- | --- | --- |
| A setting | `state … persist` ([a remembered setting](https://exact.expo.dev/recipes/persisted-setting.md)) | none |
| A small collection with one writer | `storage.fs` (start-here’s list) | `fs.read app:/data/items.json`, `fs.write …` |
| Shared or queried items | SQLite (`pattern list-detail`) | `sqlite.open app:/data/journal.db` |
| A native token | `store.get`, `store.set` | `secret.keep token` |
| Data the app ships | a JSON file `app.ts` imports ([shipped data](https://exact.expo.dev/recipes/shipped-data.md)) | none |
| A file read by path | app.json `host.ios.resources` (and `.macos`) | `fs.read bundle:/Resources/x` (its `to`) |
| A server | `fetch` ([a REST server](https://exact.expo.dev/recipes/rest-backend.md)) | `net.fetch https://api.example.com` |
| A server named at run time | `fetch`, a socket | `net.fetch https:`, `net.websocket wss:` (any host and port); plain `http:` on Apple also needs `appTransportSecurity.allowsArbitraryLoads` |

Web tokens: [HttpOnly cookies](https://exact.expo.dev/recipes/web-sessions.md).

A SQLite journal:

```contract
shape Entry
  id: string
  text: string

shape Reply
  ok: bool

component Journal
  resource entries = loadEntries() as shape Entry[] else empty()
  mutation added as shape Reply refreshes entries
  action add(text: string)
    send added = addEntry(text)
  view
    column
      for e in entries key=e.id
        text e.text
```

```ts
import type { Answer, Database, Sources, Storage } from './app.contract.d.ts';

const DB = 'app:/data/journal.db';
export const grants = `sqlite.open ${DB}`;

async function use<T>(storage: Storage, work: (db: Database) => Promise<T>): Promise<T> {
  const db = await storage.sqlite.open(DB);
  try {
    await db.execute('CREATE TABLE IF NOT EXISTS entries (id INTEGER PRIMARY KEY, text TEXT NOT NULL)');
    return await work(db);
  } finally { await db.close(); }
}
const sources: Sources = {
  loadEntries: (_args, _store, storage) => use(storage, async (db) =>
    (await db.query('SELECT id, text FROM entries ORDER BY id DESC')).rows
      .map(([id, text]) => ({ id: String(id), text: String(text) }))), // an INTEGER is a bigint
  addEntry: ([text], _store, storage) => use(storage, async (db) => {
    await db.execute('INSERT INTO entries (text) VALUES (?)', [text]);
    return { ok: true };
  }),
};
export const answer: Answer = (source, args, store, storage, native) => sources[source](args, store, storage, native);
```

- A grant covers its path and tree (`fs.write app:/data`); else `denied`.
  A native line names a directory (`fs.read /etc`). `app:/data` is kept,
  `app:/cache` refetchable, `app:/tmp` scratch ([a native module's](https://exact.expo.dev/recipes/native-module.md)
  `context.data`, `.cache`, `.temporary`).
- A file is bytes (`readFile`, `atomicWriteFile`); a missing one is code `ENOENT`: answer
  the empty collection, and branch on `e.code`, never the message.
- SQLite: `query` reads, `execute` writes, `transaction([...])` writes several or none.
  Close in `finally` to release the web’s cross-tab lock.
- At build there is no storage (code `bake`): let it through.
