Open History to see the last commit
1import { Resource } from "alchemy/Resource";
2import { type BaseBranchAttributes, type BaseBranchProps, makeBranchProvider } from "../Branch.ts";
3import type { Providers } from "../Providers.ts";
4import type { PostgresClusterSize } from "./PostgresClusterSize.ts";
5import type { PostgresDatabase } from "./PostgresDatabase.ts";
6import { runPostgresImports, runPostgresMigrations } from "./PostgresMigrations.ts";
7
8/**
9 * Properties for creating or updating a {@link PostgresBranch}.
10 */
11export interface PostgresBranchProps extends BaseBranchProps {
12 /**
13 * The Postgres database. Either a string database name or a
14 * {@link PostgresDatabase} resource.
15 */
16 database: string | PostgresDatabase;
17
18 /**
19 * PostgreSQL cluster size for the branch. Required if `backupId` is provided.
20 * Short NAS sizes are expanded using the parent branch's region and CPU
21 * architecture (e.g. `"PS_DEV"` off an ARM parent becomes `"PS_DEV_AWS_ARM"`). Metal
22 * requires the full SKU (e.g. `"M1_10_AWS_ARM_D_METAL_10"`).
23 */
24 clusterSize?: PostgresClusterSize;
25
26 /**
27 * Total number of replicas for the branch. `0` creates or converges the
28 * branch to non-HA/single-node; `2+` enables HA.
29 */
30 replicas?: number;
31
32 /**
33 * Parent branch — either a string name or another {@link PostgresBranch}.
34 * @default "main"
35 */
36 parentBranch?: string | PostgresBranch;
37}
38
39/**
40 * Output attributes of a deployed {@link PostgresBranch}.
41 */
42export interface PostgresBranchAttributes extends BaseBranchAttributes {}
43
44/**
45 * A PlanetScale branch of a {@link PostgresDatabase}. For MySQL branches
46 * use {@link MySQLBranch} instead.
47 *
48 * ### Creating a Branch
49 * **Example:** Branch from main
50 * ```typescript
51 * const branch = yield* Planetscale.PostgresBranch("Feature123", {
52 * database: "my-db",
53 * parentBranch: "main",
54 * });
55 * ```
56 *
57 * **Example:** Branch from a PostgresDatabase resource
58 * ```typescript
59 * const db = yield* Planetscale.PostgresDatabase("MyDb", { clusterSize: "PS_10" });
60 * const branch = yield* Planetscale.PostgresBranch("Feature456", {
61 * database: db,
62 * parentBranch: "main",
63 * });
64 * ```
65 *
66 * ### Migrations and seed data
67 * **Example:** Apply migrations on a branch
68 * ```typescript
69 * const branch = yield* Planetscale.PostgresBranch("Feature123", {
70 * database: db,
71 * parentBranch: "main",
72 * migrationsDir: "./migrations",
73 * importFiles: ["./seed.sql"],
74 * });
75 * ```
76 */
77export type PostgresBranch = Resource<
78 "Planetscale.PostgresBranch",
79 PostgresBranchProps,
80 PostgresBranchAttributes,
81 never,
82 Providers
83>;
84
85/** @resource */
86export const PostgresBranch = Resource<PostgresBranch>("Planetscale.PostgresBranch");
87
88export const PostgresBranchProvider = () =>
89 makeBranchProvider({
90 resource: PostgresBranch,
91 expectedKind: "postgresql",
92 engineLabel: "PostgresBranch",
93 runners: {
94 runMigrations: runPostgresMigrations,
95 runImports: runPostgresImports,
96 },
97 });