Skip to content

Commit

Permalink
Addressed review comments and more changes
Browse files Browse the repository at this point in the history
  • Loading branch information
balegas committed Dec 9, 2024
1 parent 1a7453a commit effca0a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 46 deletions.
24 changes: 13 additions & 11 deletions examples/nextjs-example/.sst/platform/config.d.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
import "./src/global.d.ts"
import "../types.generated"
import { AppInput, App, Config } from "./src/config"
import "./src/global.js";
import "../types.generated";
import { AppInput, App, Config } from "./src/config.js";
import * as _neon from "@sst-provider/neon";
import * as _aws from "@pulumi/aws";
import * as _cloudflare from "@pulumi/cloudflare";


declare global {
// @ts-expect-error
export import neon = _neon
export import neon = _neon;
// @ts-expect-error
export import aws = _aws
export import aws = _aws;
// @ts-expect-error
export import cloudflare = _cloudflare
export import cloudflare = _cloudflare;
interface Providers {
providers?: {
"neon"?: (_neon.ProviderArgs & { version?: string }) | boolean | string;
"aws"?: (_aws.ProviderArgs & { version?: string }) | boolean | string;
"cloudflare"?: (_cloudflare.ProviderArgs & { version?: string }) | boolean | string;
}
neon?: (_neon.ProviderArgs & { version?: string }) | boolean | string;
aws?: (_aws.ProviderArgs & { version?: string }) | boolean | string;
cloudflare?:
| (_cloudflare.ProviderArgs & { version?: string })
| boolean
| string;
};
}
export const $config: (
input: Omit<Config, "app"> & {
Expand Down
29 changes: 9 additions & 20 deletions examples/nextjs-example/app/api/items/route.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,21 @@
import { NextResponse } from "next/server"
import { Client } from "pg"
import { Pool } from "pg"

const client = new Client({
connectionString: process.env.DATABASE_POOLED_URL,
const client = new Pool({
connectionString: process.env.DATABASE_URL,
})

export async function POST(request: Request) {
const body = await request.json()

try {
await client.connect()
const result = await client.query(
`INSERT INTO items (id)
const result = await client.query(
`INSERT INTO items (id)
VALUES ($1) RETURNING id;`,
[body.uuid]
)
return NextResponse.json({ id: result.rows[0].id })
} finally {
await client.end()
}
[body.uuid]
)
return NextResponse.json({ id: result.rows[0].id })
}

export async function DELETE() {
try {
await client.connect()
await client.query(`DELETE FROM items;`)
} finally {
await client.end()
}
await client.query(`DELETE FROM items;`)
return NextResponse.json(`ok`)
}
24 changes: 9 additions & 15 deletions examples/nextjs-example/sst.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@

import { execSync } from "child_process"

const isProduction = (stage) => stage.toLowerCase() === `production`

export default $config({
app(input) {
return {
name: `nextjs-example`,
removal: input?.stage === `production` ? `retain` : `remove`,
removal: isProduction(input?.stage) ? `retain` : `remove`,
home: `aws`,
providers: {
cloudflare: `5.42.0`,
Expand All @@ -27,27 +29,21 @@ export default $config({

const db = new neon.Database(`nextjs-example`, {
...base,
name:
$app.stage === `Production`
? `nextjs-production`
: `nextjs-${$app.stage}`,
name: isProduction($app.stage)
? `nextjs-production`
: `nextjs-${$app.stage}`,
ownerName: `neondb_owner`,
})

const databaseUri = getNeonDbUri(project, db, false)
const databasePooledUri = getNeonDbUri(project, db, true)
try {
databaseUri.apply(applyMigrations)

const electricInfo = databaseUri.apply((uri) =>
addDatabaseToElectric(uri)
)

const website = deployNextJsExample(
electricInfo,
databaseUri,
databasePooledUri
)
const website = deployNextJsExample(electricInfo, databaseUri)
return {
databaseUri,
database_id: electricInfo.id,
Expand All @@ -71,19 +67,17 @@ function applyMigrations(uri: string) {

function deployNextJsExample(
electricInfo: $util.Output<{ id: string; token: string }>,
uri: $util.Output<string>,
pooledUri: $util.Output<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,
DATABASE_POOLED_URL: pooledUri,
},
domain: {
name: `nextjs${$app.stage === `production` ? `` : `-stage-${$app.stage}`}.electric-sql.com`,
name: `nextjs${isProduction($app.stage) ? `` : `-stage-${$app.stage}`}.examples.electric-sql.com`,
dns: sst.cloudflare.dns(),
},
})
Expand Down

0 comments on commit effca0a

Please sign in to comment.