| 1 | --- |
| 2 | title: Branches |
| 3 | description: Prisma branches group databases under git-style names — preview branches per stage, default-branch promotion, and database attachment. |
| 4 | --- |
| 5 | |
| 6 | A `Prisma.Branch` groups databases and apps under a git-style name |
| 7 | inside a project. Prisma tracks two separate properties: |
| 8 | |
| 9 | - `role` controls whether Compute resolves `production` or `preview` |
| 10 | environment variables. |
| 11 | - `isDefault` identifies the branch new resources use when they omit |
| 12 | a branch. |
| 13 | |
| 14 | The first branch created with a project has the immutable |
| 15 | `production` role. Additional branches have the `preview` role. |
| 16 | Promoting a preview branch changes `isDefault`; it does not change |
| 17 | that branch's role. |
| 18 | |
| 19 | ```typescript |
| 20 | import * as Prisma from "alchemy/Prisma"; |
| 21 | |
| 22 | const branch = yield* Prisma.Branch("preview", { project }); |
| 23 | ``` |
| 24 | |
| 25 | `gitName` is optional — omitted, alchemy generates a stable physical |
| 26 | name. Pass one when the branch should mirror a real git branch: |
| 27 | |
| 28 | ```typescript |
| 29 | const branch = yield* Prisma.Branch("feature", { |
| 30 | project, |
| 31 | gitName: "feature/search", |
| 32 | }); |
| 33 | ``` |
| 34 | |
| 35 | ## Attaching databases |
| 36 | |
| 37 | A database joins a branch by `branchId` (or `branchGitName`): |
| 38 | |
| 39 | ```typescript |
| 40 | const previewDb = yield* Prisma.Postgres("preview-db", { |
| 41 | project, |
| 42 | branchId: branch.branchId, |
| 43 | }); |
| 44 | ``` |
| 45 | |
| 46 | Compute apps attach the same way. Environment-variable scope follows |
| 47 | the branch's `role`, not `isDefault`, so a promoted preview branch |
| 48 | continues to use the `preview` class. |
| 49 | |
| 50 | ## A branch per stage |
| 51 | |
| 52 | Branch names can come from the Stack's stage, giving every preview |
| 53 | stage its own branch — and the databases and apps attached to it — |
| 54 | that tears down with the stage: |
| 55 | |
| 56 | ```typescript |
| 57 | import { Stage } from "alchemy"; |
| 58 | |
| 59 | const stage = yield* Stage; |
| 60 | |
| 61 | const branch = yield* Prisma.Branch("stage", { |
| 62 | project, |
| 63 | gitName: `preview/${stage}`, |
| 64 | }); |
| 65 | ``` |
| 66 | |
| 67 | ## Default-branch promotion |
| 68 | |
| 69 | `isDefault: true` promotes the branch, atomically clearing the current |
| 70 | default flag. The Management API only supports promotion — demotion |
| 71 | happens by promoting another branch — and alchemy records the |
| 72 | displaced branch so destroying the resource can restore it: |
| 73 | |
| 74 | ```typescript |
| 75 | const release = yield* Prisma.Branch("release", { |
| 76 | project, |
| 77 | gitName: "release/next", |
| 78 | isDefault: true, |
| 79 | }); |
| 80 | ``` |
| 81 | |
| 82 | The original `production`-role branch is owned by the project and |
| 83 | cannot be deleted through `Prisma.Branch`. A preview-role |
| 84 | `Prisma.Branch` promoted to default remains owned by its Branch |
| 85 | resource: on destroy, alchemy restores the displaced default and then |
| 86 | deletes the promoted branch. |
| 87 | |
| 88 | ## Where next |
| 89 | |
| 90 | - [Postgres](/prisma/data/postgres) — projects and databases. |
| 91 | - [Connections](/prisma/data/connections) — credentials for the |
| 92 | databases on a branch. |
| 93 | |
| 94 | Reference: |
| 95 | |
| 96 | - [Branch API reference](/providers/prisma/branch) |