Skip to content

Commit

Permalink
Rename database_id to source_id in examples deployment (#2234)
Browse files Browse the repository at this point in the history
Also fix other problems with example deployments:

- new admin api endpoint
- nextjs example renamed
- add support for pooled connections
  • Loading branch information
magnetised authored Jan 21, 2025
1 parent 4d7b8ba commit f47378c
Show file tree
Hide file tree
Showing 18 changed files with 79 additions and 121 deletions.
16 changes: 8 additions & 8 deletions .github/workflows/deploy_examples.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
name: Deploy Examples

on:
push:
branches: ['main']
pull_request:
paths: ['examples/*/**']
push:
branches: ["main"]
pull_request:
paths: ["examples/*/**"]

concurrency:
group: ${{ github.event_name == 'push' && 'prod-deploy-group' || format('examples-pr-{0}', github.event.number) }}
Expand All @@ -25,6 +25,7 @@ jobs:
NEON_PROJECT_ID: ${{ secrets.NEON_PROJECT_ID }}
ELECTRIC_API: ${{ secrets.ELECTRIC_API }}
ELECTRIC_ADMIN_API: ${{ secrets.ELECTRIC_ADMIN_API }}
ELECTRIC_TEAM_ID: ${{ secrets.ELECTRIC_TEAM_ID }}
# HONEYCOMB_API_KEY: ${{ secrets.HONEYCOMB_API_KEY }} TODO

steps:
Expand All @@ -33,7 +34,7 @@ jobs:
- uses: actions/setup-node@v4
with:
node-version-file: ".tool-versions"
cache: 'pnpm'
cache: "pnpm"

- name: Install dependencies
run: pnpm install --frozen-lockfile
Expand All @@ -47,7 +48,7 @@ jobs:
sst-cache-main-${{ runner.os }}
- name: Deploy Linearlite Read Only
working-directory: examples/linearlite-read-only
working-directory: ./examples/linearlite-read-only
run: |
pnpm sst deploy --stage ${{ env.DEPLOY_ENV }}
if [ -f ".sst/outputs.json" ]; then
Expand All @@ -58,9 +59,8 @@ jobs:
exit 1
fi
- name: Deploy NextJs example
working-directory: examples/nextjs-example
working-directory: ./examples/nextjs
run: |
pnpm sst deploy --stage ${{ env.DEPLOY_ENV }}
if [ -f ".sst/outputs.json" ]; then
Expand Down
2 changes: 1 addition & 1 deletion examples/linearlite-read-only/src/pages/Issue/Comments.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function Comments(commentProps: CommentsProps) {
url: `${baseUrl}/v1/shape`,
params: {
token,
database_id: databaseId,
source_id: databaseId,
table: `comment`,
},
})! as Comment[]
Expand Down
2 changes: 1 addition & 1 deletion examples/linearlite-read-only/src/shapes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ export const issueShape: ShapeStreamOptions = {
params: {
table: `issue`,
token,
database_id: databaseId,
source_id: databaseId,
},
}
24 changes: 20 additions & 4 deletions examples/linearlite-read-only/sst.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default $config({
ownerName: `neondb_owner`,
})

const databaseUri = getNeonDbUri(project, db)
const databaseUri = getNeonDbUri(project, db, false)
try {
databaseUri.apply(applyMigrations)
databaseUri.apply(loadData)
Expand Down Expand Up @@ -96,28 +96,44 @@ function deployLinearLite(

function getNeonDbUri(
project: $util.Output<neon.GetProjectResult>,
db: neon.Database
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

return $interpolate`postgresql://${passwordOutput.roleName}:${passwordOutput.password}@${project.databaseHost}/${db.name}?sslmode=require`
return $interpolate`postgresql://${passwordOutput.roleName}:${passwordOutput.password}@${databaseHost}/${db.name}?sslmode=require`
}

async function addDatabaseToElectric(
uri: string
): Promise<{ id: string; token: string }> {
const adminApi = process.env.ELECTRIC_ADMIN_API
const teamId = process.env.ELECTRIC_TEAM_ID

const result = await fetch(`${adminApi}/v1/databases`, {
const result = await fetch(`${adminApi}/v1/sources`, {
method: `PUT`,
headers: { 'Content-Type': `application/json` },
body: JSON.stringify({
database_url: uri,
region: `us-east-1`,
team_id: teamId,
}),
})

Expand Down
4 changes: 2 additions & 2 deletions examples/linearlite/src/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ async function startSyncToDatabase(pg: PGliteWithExtensions) {
url: issueUrl.toString(),
params: {
table: 'issue',
database_id: ELECTRIC_DATABASE_ID,
source_id: ELECTRIC_DATABASE_ID,
},
},
table: 'issue',
Expand Down Expand Up @@ -103,7 +103,7 @@ async function startSyncToDatabase(pg: PGliteWithExtensions) {
url: commentUrl.toString(),
params: {
table: 'comment',
database_id: ELECTRIC_DATABASE_ID,
source_id: ELECTRIC_DATABASE_ID,
},
},
table: 'comment',
Expand Down
4 changes: 3 additions & 1 deletion examples/linearlite/sst.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,15 @@ async function addDatabaseToElectric(
uri: string
): Promise<{ id: string; token: string }> {
const adminApi = process.env.ELECTRIC_ADMIN_API
const teamId = process.env.ELECTRIC_TEAM_ID

const result = await fetch(`${adminApi}/v1/databases`, {
const result = await fetch(`${adminApi}/v1/sources`, {
method: `PUT`,
headers: { 'Content-Type': `application/json` },
body: JSON.stringify({
database_url: uri,
region: `us-east-1`,
team_id: teamId,
}),
})

Expand Down
2 changes: 1 addition & 1 deletion examples/nextjs/app/shape-proxy/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export async function GET(request: Request) {
})

if (process.env.DATABASE_ID) {
originUrl.searchParams.set(`database_id`, process.env.DATABASE_ID)
originUrl.searchParams.set(`source_id`, process.env.DATABASE_ID)
}

const headers = new Headers()
Expand Down
30 changes: 23 additions & 7 deletions examples/nextjs/sst.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,18 @@ export default $config({
ownerName: `neondb_owner`,
})

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

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

const website = deployNextJsExample(electricInfo, databaseUri)
const website = deployNextJsExample(electricInfo, pooledDatabaseUri)
return {
databaseUri,
pooledDatabaseUri,
database_id: electricInfo.id,
electric_token: electricInfo.token,
website: website.url,
Expand Down Expand Up @@ -84,28 +85,43 @@ function deployNextJsExample(

function getNeonDbUri(
project: $util.Output<neon.GetProjectResult>,
db: neon.Database
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

return $interpolate`postgresql://${passwordOutput.roleName}:${passwordOutput.password}@${project.databaseHost}/${db.name}?sslmode=require`
return $interpolate`postgresql://${passwordOutput.roleName}:${passwordOutput.password}@${databaseHost}/${db.name}?sslmode=require`
}

async function addDatabaseToElectric(
uri: string
): Promise<{ id: string; token: string }> {
const adminApi = process.env.ELECTRIC_ADMIN_API
const teamId = process.env.ELECTRIC_TEAM_ID

const result = await fetch(`${adminApi}/v1/databases`, {
const result = await fetch(`${adminApi}/v1/sources`, {
method: `PUT`,
headers: { "Content-Type": `application/json` },
body: JSON.stringify({
database_url: uri,
region: `us-east-1`,
team_id: teamId,
}),
})

Expand Down
2 changes: 1 addition & 1 deletion examples/proxy-auth/app/shape-proxy/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export async function GET(request: Request) {
})

if (process.env.DATABASE_ID) {
originUrl.searchParams.set(`database_id`, process.env.DATABASE_ID)
originUrl.searchParams.set(`source_id`, process.env.DATABASE_ID)
}

if (process.env.ELECTRIC_TOKEN) {
Expand Down
4 changes: 3 additions & 1 deletion examples/proxy-auth/sst.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,15 @@ async function addDatabaseToElectric(
token: string
}> {
const adminApi = process.env.ELECTRIC_ADMIN_API
const url = new URL(`/v1/databases`, adminApi)
const teamId = process.env.ELECTRIC_TEAM_ID
const url = new URL(`/v1/sources`, adminApi)
const result = await fetch(url, {
method: `PUT`,
headers: { "Content-Type": `application/json` },
body: JSON.stringify({
database_url: uri,
region,
team_id: teamId,
}),
})
if (!result.ok) {
Expand Down
2 changes: 1 addition & 1 deletion examples/todo-app/src/routes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default function Index() {
url: new URL(`${import.meta.env.VITE_ELECTRIC_URL}/v1/shape/`).href,
params: {
table: `todos`,
database_id: import.meta.env.VITE_ELECTRIC_DATABASE_ID,
source_id: import.meta.env.VITE_ELECTRIC_DATABASE_ID,
token: import.meta.env.VITE_ELECTRIC_TOKEN,
},
})
Expand Down
4 changes: 3 additions & 1 deletion examples/todo-app/sst.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,15 @@ async function addDatabaseToElectric(
uri: string
): Promise<{ id: string; token: string }> {
const adminApi = process.env.ELECTRIC_ADMIN_API
const teamId = process.env.ELECTRIC_TEAM_ID

const result = await fetch(`${adminApi}/v1/databases`, {
const result = await fetch(`${adminApi}/v1/sources`, {
method: `PUT`,
headers: { "Content-Type": `application/json` },
body: JSON.stringify({
database_url: uri,
region: `us-east-1`,
team_id: teamId,
}),
})

Expand Down
4 changes: 2 additions & 2 deletions examples/write-patterns/shared/app/config.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
export const ELECTRIC_URL =
import.meta.env.VITE_ELECTRIC_URL || 'http://localhost:3000'

export const envParams: { database_id?: string; token?: string } =
export const envParams: { source_id?: string; token?: string } =
import.meta.env.VITE_ELECTRIC_TOKEN &&
import.meta.env.VITE_ELECTRIC_DATABASE_ID
? {
database_id: import.meta.env.VITE_ELECTRIC_DATABASE_ID,
source_id: import.meta.env.VITE_ELECTRIC_DATABASE_ID,
token: import.meta.env.VITE_ELECTRIC_TOKEN,
}
: {}
4 changes: 3 additions & 1 deletion examples/write-patterns/sst.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,15 @@ async function addDatabaseToElectric(
region: 'us-east-1' | 'eu-west-1' = 'us-east-1'
): Promise<{ id: string; token: string }> {
const adminApi = process.env.ELECTRIC_ADMIN_API
const teamId = process.env.ELECTRIC_TEAM_ID

const result = await fetch(new URL('v1/databases', adminApi), {
const result = await fetch(new URL('v1/sources', adminApi), {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
database_url,
region,
team_id: teamId,
}),
})

Expand Down
2 changes: 1 addition & 1 deletion examples/yjs/app/shape-proxy/[...table]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export async function GET(request: Request) {
})

if (process.env.DATABASE_ID) {
originUrl.searchParams.set(`database_id`, process.env.DATABASE_ID)
originUrl.searchParams.set(`source_id`, process.env.DATABASE_ID)
}

const headers = new Headers()
Expand Down
4 changes: 3 additions & 1 deletion examples/yjs/sst.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,15 @@ async function addDatabaseToElectric(
uri: string
): Promise<{ id: string; token: string }> {
const adminApi = process.env.ELECTRIC_ADMIN_API
const teamId = process.env.ELECTRIC_TEAM_ID

const result = await fetch(`${adminApi}/v1/databases`, {
const result = await fetch(`${adminApi}/v1/sources`, {
method: `PUT`,
headers: { "Content-Type": `application/json` },
body: JSON.stringify({
database_url: uri,
region: `us-east-1`,
team_id: teamId,
}),
})

Expand Down
2 changes: 1 addition & 1 deletion website/docs/api/clients/typescript.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ Note that certain parameter names are reserved for Electric's internal use and c
- `handle`
- `live`
- `cursor`
- `database_id`
- `source_id`
The following PostgreSQL-specific parameters should be included within the `params` object:
- `table` - The root table for the shape
Expand Down
Loading

0 comments on commit f47378c

Please sign in to comment.