-
Notifications
You must be signed in to change notification settings - Fork 201
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
examples: add public deployment of LinearLite
- Loading branch information
1 parent
3775f1c
commit 704b347
Showing
7 changed files
with
156 additions
and
11 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
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 |
---|---|---|
@@ -1 +1,3 @@ | ||
export const baseUrl = import.meta.env.ELECTRIC_URL ?? `http://localhost:3000` | ||
export const baseUrl = import.meta.env.VITE_ELECTRIC_URL ? new URL(import.meta.env.VITE_ELECTRIC_URL).origin : `http://localhost:3000` | ||
export const token = import.meta.env.VITE_ELECTRIC_TOKEN ?? `` | ||
export const databaseId = import.meta.env.VITE_DATABASE_ID ?? `` |
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 |
---|---|---|
@@ -1,5 +1,10 @@ | ||
import { baseUrl } from './electric' | ||
import { ShapeStreamOptions } from '@electric-sql/next' | ||
import { baseUrl, databaseId, token } from './electric' | ||
|
||
export const issueShape = { | ||
export const issueShape: ShapeStreamOptions = { | ||
url: `${baseUrl}/v1/shape/issue`, | ||
databaseId, | ||
headers: { | ||
Authorization: `Bearer ${token}`, | ||
}, | ||
} |
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,12 @@ | ||
/* This file is auto-generated by SST. Do not edit. */ | ||
/* tslint:disable */ | ||
/* eslint-disable */ | ||
/// <reference types="vite/client" /> | ||
interface ImportMetaEnv { | ||
readonly VITE_ELECTRIC_URL: string | ||
readonly VITE_ELECTRIC_TOKEN: string | ||
readonly VITE_DATABASE_ID: string | ||
} | ||
interface ImportMeta { | ||
readonly env: ImportMetaEnv | ||
} |
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,14 @@ | ||
/* This file is auto-generated by SST. Do not edit. */ | ||
/* tslint:disable */ | ||
/* eslint-disable */ | ||
/* deno-fmt-ignore-file */ | ||
import "sst" | ||
export {} | ||
declare module "sst" { | ||
export interface Resource { | ||
"linearlite-example": { | ||
"type": "sst.aws.StaticSite" | ||
"url": string | ||
} | ||
} | ||
} |
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,106 @@ | ||
/// <reference path="./.sst/platform/config.d.ts" /> | ||
|
||
import { execSync } from 'child_process' | ||
|
||
export default $config({ | ||
app(input) { | ||
return { | ||
name: `linearlite`, | ||
removal: input?.stage === `production` ? `retain` : `remove`, | ||
home: `aws`, | ||
providers: { 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(`linearlite`, { | ||
...base, | ||
ownerName: `neondb_owner`, | ||
}) | ||
|
||
const databaseUri = getNeonDbUri(project, db) | ||
|
||
databaseUri.apply(applyMigrations) | ||
|
||
const electricInfo = databaseUri.apply((uri) => addDatabaseToElectric(uri)) | ||
|
||
const website = deployLinearLite(electricInfo) | ||
|
||
return { | ||
databaseUri, | ||
database_id: electricInfo.id, | ||
electric_token: electricInfo.token, | ||
website: website.url, | ||
} | ||
}, | ||
}) | ||
|
||
function applyMigrations(uri: string) { | ||
execSync(`pnpm exec pg-migrations apply --directory ./db/migrations`, { | ||
env: { | ||
...process.env, | ||
DATABASE_URL: uri, | ||
}, | ||
}) | ||
} | ||
|
||
function deployLinearLite( | ||
electricInfo: $util.Output<{ id: string; token: string }> | ||
) { | ||
return new sst.aws.StaticSite(`linearlite-example`, { | ||
environment: { | ||
VITE_ELECTRIC_URL: process.env.ELECTRIC_API!, | ||
VITE_ELECTRIC_TOKEN: electricInfo.token, | ||
VITE_DATABASE_ID: electricInfo.id, | ||
}, | ||
build: { | ||
command: `pnpm run build`, | ||
output: `dist`, | ||
}, | ||
domain: { | ||
name: `linearlite-example${$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_HOST | ||
|
||
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() | ||
} |