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