From 7f0afbdd8459630454d7040d0c06a53a87928322 Mon Sep 17 00:00:00 2001 From: Kevin De Porre Date: Wed, 11 Dec 2024 09:36:33 +0100 Subject: [PATCH] Replace Neon SST provider by Postgres provider. --- examples/nextjs/sst.config.ts | 85 ++++++++++++++--------------------- 1 file changed, 33 insertions(+), 52 deletions(-) diff --git a/examples/nextjs/sst.config.ts b/examples/nextjs/sst.config.ts index 494cd6000d..f90cab0ad5 100644 --- a/examples/nextjs/sst.config.ts +++ b/examples/nextjs/sst.config.ts @@ -15,37 +15,46 @@ export default $config({ cloudflare: `5.42.0`, aws: { version: `6.57.0`, + region: `eu-west-1`, }, - neon: `0.6.3`, + postgresql: "3.14.0", }, } }, async run() { - const project = neon.getProjectOutput({ id: process.env.NEON_PROJECT_ID! }) - const base = { - projectId: project.id, - branchId: project.defaultBranchId, - } + if (!process.env.ELECTRIC_API || !process.env.ELECTRIC_ADMIN_API) + throw new Error( + `Env variables ELECTRIC_API and ELECTRIC_ADMIN_API must be set` + ) - const db = new neon.Database(`nextjs-example`, { - ...base, - name: isProduction($app.stage) - ? `nextjs-production` - : `nextjs-${$app.stage}`, - ownerName: `neondb_owner`, - }) + if (!process.env.EXAMPLES_DATABASE_HOST || !process.env.EXAMPLES_DATABASE_PASSWORD) { + throw new Error( + `Env variables EXAMPLES_DATABASE_HOST and EXAMPLES_DATABASE_PASSWORD must be set` + ) + } - const databaseUri = getNeonDbUri(project, db, false) try { - databaseUri.apply(applyMigrations) + const provider = new postgresql.Provider("neon", { + host: process.env.EXAMPLES_DATABASE_HOST, + database: `neondb`, + username: `neondb_owner`, + password: process.env.EXAMPLES_DATABASE_PASSWORD, + }) - const electricInfo = databaseUri.apply((uri) => - addDatabaseToElectric(uri) - ) + const dbName = isProduction($app.stage) ? `nextjs-production` : `nextjs-${$app.stage}` + const pg = new postgresql.Database(dbName, {}, { provider }) + + const pgUri = $interpolate`postgresql://${provider.username}:${provider.password}@${provider.host}/${pg.name}?sslmode=require` + const electricInfo = pgUri.apply((uri) => { + return addDatabaseToElectric(uri, `eu-west-1`) + }) + + const website = deployNextJsExample(electricInfo, pgUri) + + pgUri.apply((uri) => applyMigrations(uri)) - const website = deployNextJsExample(electricInfo, databaseUri) return { - databaseUri, + pgUri, database_id: electricInfo.id, electric_token: electricInfo.token, website: website.url, @@ -83,39 +92,11 @@ function deployNextJsExample( }) } -function getNeonDbUri( - project: $util.Output, - db: neon.Database, - pooled: boolean -) { - const passwordOutput = neon.getBranchRolePasswordOutput({ - projectId: project.id, - branchId: project.defaultBranchId, - roleName: db.ownerName, - }) - - const endpoint = neon.getBranchEndpointsOutput({ - projectId: project.id, - branchId: project.defaultBranchId, - }) - - const databaseHost = pooled - ? endpoint.endpoints?.apply((endpoints) => - endpoints![0].host.replace( - endpoints![0].id, - endpoints![0].id + `-pooler` - ) - ) - : project.databaseHost - - const url = $interpolate`postgresql://${passwordOutput.roleName}:${passwordOutput.password}@${databaseHost}/${db.name}?sslmode=require` - return url -} - async function addDatabaseToElectric( - uri: string + uri: string, + region: `eu-west-1` | `us-east-1` ): Promise<{ id: string; token: string }> { - const adminApi = process.env.ELECTRIC_ADMIN_API + const adminApi = process.env.ELECTRIC_ADMIN_API! const electricUrl = new URL(`/v1/databases`, adminApi) const result = await fetch(electricUrl, { @@ -123,7 +104,7 @@ async function addDatabaseToElectric( headers: { "Content-Type": `application/json` }, body: JSON.stringify({ database_url: uri, - region: `eu-west-1`, + region, }), })