# Data model

> The ten PostgreSQL tables, what each holds, how a saved post's files relate to its row, and what to know about tables made by synchronize instead of migrations.

Source: https://crawl-docs.pdhq.win/advanced/data-model/
Last updated: 2026-12-02

The database holds **metadata and state**. The saved **files** of a post (pictures, videos, the offline page) are in the bundle storage, not here. See [Architecture](https://crawl-docs.pdhq.win/advanced/architecture.md).

All tables are listed in `apps/api/src/entities.ts`. TypeORM creates and alters them at start while `DB_SYNCHRONIZE` is on (it is in the compose file).

## Tables

| Table | Holds |
| --- | --- |
| `bookmarks` | One row per saved post. The card data the library needs (see below). |
| `restrict_rules` | The site and account rules that send saves to the restricted part (`host`, `path`; unique together). |
| `post_shares` | Temporary links to one post: label, `expiresAt`, SHA-256 of the secret, open and download counts, who fetched it last. |
| `collection_shares` | Temporary links to several posts: the post ids (a snapshot, up to 50) and the same counters. |
| `big_uploads` | A big video's tracks sent to WebDAV in parts: platform, post, track (`video` or `audio`), total bytes, part size, the finished parts with fingerprints, and the state of the one-file copy (`joinState`). |
| `big_conversions` | A smaller copy of a big video, one row per post and size: state, stage (`sample`, `full`, or `own` for a file you converted yourself and attached), percent, estimates, output size, layout (`file` or `parts`) and the checks. |
| `attachments` | What you attached to a post: `kind` is `post`, `file` or `link`, with its target, title, address, file name, size and where the bytes are. |
| `layouts` | A layout template per site: `domain` and `source`. |
| `app_settings` | One row (id 1): settings changed in the web app instead of the environment. Today: `maxVideoMb`. |
| `webdav_settings` | One row (id 1): the WebDAV address, user name, the password **sealed** with AES-256-GCM, the offload switch and the large-file threshold in MB. |

## The `bookmarks` table

| Column | Meaning |
| --- | --- |
| `id` | The UUID used in the web app's addresses (`/bookmarks/<id>`). |
| `platform`, `postId` | The post's platform and its id there. Unique together: saving the same post again replaces the row. |
| `url`, `authorName`, `preview` | The address, the author, and the start of the text for search and cards. |
| `bundleKey` | Where the files are: `<platform>/<id>`, for example `bluesky/3kabc`. |
| `tags` | The tags, as an array of text. |
| `note` | Your note. |
| `restricted` | True when the post is in the restricted part. |
| `sizeBytes`, `webdavBytes`, `bigBytes` | What the post takes: in total, how much of that is on WebDAV, and what its big-video tracks and copies take (they are not in the bundle, so they are kept apart and added back when the bundle is counted again). |
| Card columns | `coverPath`, `coverWidth`, `coverHeight`, `mediaCount`, `imageCount`, `videoCount`, `fileCount`, `avatarPath`, `siteName`, `siteLogoPath`, `coverVideoPath`, `hasSavedVideo`, `hasMadeSizes`, `hasOnlineOnlyMedia`. Computed from the saved post so the library does not read every bundle. |
| `authorKey`, `authorHandle` | The page or account the post comes from, in lower case, to group the library by. Older posts are filled in at start. |
| `metaVersion` | A post whose version is below the code's `META_VERSION` (11) has its card data computed again at start. |
| `createdAt`, `updatedAt` | Dates. |

## Bundles

A bundle is the folder `<platform>/<id>` in the bundle storage:

```text
index.html       the offline page (no scripts, a strict CSP)
post.json        the BundleManifest: version, the Post, and { path, bytes, sha256 } for every file
post.md          the Markdown version
text/post.txt    the plain text
images/ videos/ files/ transcripts/ ...
.webdav.json     only when some files are on WebDAV: which ones
```

`post.json` is the source of truth. `index.html` is rendered from it by `packages/render`, which is why a layout or a fix to the renderer can redraw old posts. See [The Post schema](https://crawl-docs.pdhq.win/advanced/post-schema.md).

## Tables are made by synchronize

There are **no migrations**. With `DB_SYNCHRONIZE=true`, TypeORM compares the entities to the database at start and changes the tables to match. That is convenient and also risky: a renamed column can be dropped and made again empty. For data you cannot lose:

- Take a backup of `pgdata` (or your hosted database) before each update.
- Use Settings → Database to copy to another database first if you want a second copy: see [Copy the database](https://crawl-docs.pdhq.win/guide/database-copy.md).
- If you add a column, `synchronize` adds it. If you turn `DB_SYNCHRONIZE` off, you must add columns by hand. For example the `bookmarks` columns `authorKey` and `authorHandle` were added this way.

## Moving the database

The database copy tool makes the new database's tables from the entity list, copies every table, and compares them. It needs the `uuid-ossp` add-on in the new database.
