Skip to content

Commit

Permalink
Replace Neon SST provider by Postgres provider.
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-dp committed Dec 11, 2024
1 parent 4fc0145 commit 7f0afbd
Showing 1 changed file with 33 additions and 52 deletions.
85 changes: 33 additions & 52 deletions examples/nextjs/sst.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -83,47 +92,19 @@ function deployNextJsExample(
})
}

function getNeonDbUri(
project: $util.Output<neon.GetProjectResult>,
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, {
method: `PUT`,
headers: { "Content-Type": `application/json` },
body: JSON.stringify({
database_url: uri,
region: `eu-west-1`,
region,
}),
})

Expand Down

0 comments on commit 7f0afbd

Please sign in to comment.