-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
3,222 additions
and
3,476 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 21 additions & 2 deletions
23
examples/nextjs-ssr-example/app/shape-proxy/[...table]/route.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// eslint-disable-next-line @typescript-eslint/triple-slash-reference | ||
/// <reference path="./.sst/platform/config.d.ts" /> | ||
|
||
import { execSync } from "child_process" | ||
|
||
export default $config({ | ||
app(input) { | ||
return { | ||
name: `nextjs-ssr-example`, | ||
removal: input?.stage === `production` ? `retain` : `remove`, | ||
home: `aws`, | ||
providers: { | ||
cloudflare: `5.42.0`, | ||
aws: { | ||
version: `6.57.0`, | ||
}, | ||
neon: `0.6.3`, | ||
}, | ||
} | ||
}, | ||
async run() { | ||
const project = neon.getProjectOutput({ id: process.env.NEON_PROJECT_ID! }) | ||
const base = { | ||
projectId: project.id, | ||
branchId: project.defaultBranchId, | ||
} | ||
|
||
const db = new neon.Database(`nextjs-ssr-example`, { | ||
...base, | ||
ownerName: `neondb_owner`, | ||
}) | ||
|
||
const databaseUri = getNeonDbUri(project, db) | ||
try { | ||
databaseUri.apply(applyMigrations) | ||
|
||
const electricInfo = databaseUri.apply((uri) => | ||
addDatabaseToElectric(uri) | ||
) | ||
|
||
const website = deployNextJsExample(electricInfo, databaseUri) | ||
return { | ||
databaseUri, | ||
database_id: electricInfo.id, | ||
electric_token: electricInfo.token, | ||
website: website.url, | ||
} | ||
} catch (e) { | ||
console.error(`Failed to deploy nextjs ssr example stack`, e) | ||
} | ||
}, | ||
}) | ||
|
||
function applyMigrations(uri: string) { | ||
execSync(`pnpm exec pg-migrations apply --directory ./db/migrations`, { | ||
env: { | ||
...process.env, | ||
DATABASE_URL: uri, | ||
}, | ||
}) | ||
} | ||
|
||
function deployNextJsExample( | ||
electricInfo: $util.Output<{ id: string; token: string }>, | ||
uri: $util.Output<string> | ||
) { | ||
return new sst.aws.Nextjs(`nextjs`, { | ||
environment: { | ||
ELECTRIC_URL: process.env.ELECTRIC_API!, | ||
ELECTRIC_TOKEN: electricInfo.token, | ||
DATABASE_ID: electricInfo.id, | ||
DATABASE_URL: uri, | ||
}, | ||
domain: { | ||
name: `nextjs${$app.stage === `production` ? `` : `-stage-${$app.stage}`}.electric-sql.com`, | ||
dns: sst.cloudflare.dns(), | ||
}, | ||
}) | ||
} | ||
|
||
function getNeonDbUri( | ||
project: $util.Output<neon.GetProjectResult>, | ||
db: neon.Database | ||
) { | ||
const passwordOutput = neon.getBranchRolePasswordOutput({ | ||
projectId: project.id, | ||
branchId: project.defaultBranchId, | ||
roleName: db.ownerName, | ||
}) | ||
|
||
return $interpolate`postgresql://${passwordOutput.roleName}:${passwordOutput.password}@${project.databaseHost}/${db.name}?sslmode=require` | ||
} | ||
|
||
async function addDatabaseToElectric( | ||
uri: string | ||
): Promise<{ id: string; token: string }> { | ||
const adminApi = process.env.ELECTRIC_ADMIN_API | ||
|
||
const result = await fetch(`${adminApi}/v1/databases`, { | ||
method: `PUT`, | ||
headers: { "Content-Type": `application/json` }, | ||
body: JSON.stringify({ | ||
database_url: uri, | ||
region: `us-east-1`, | ||
}), | ||
}) | ||
|
||
if (!result.ok) { | ||
throw new Error( | ||
`Could not add database to Electric (${result.status}): ${await result.text()}` | ||
) | ||
} | ||
|
||
return await result.json() | ||
} |
Oops, something went wrong.