Skip to content

Commit

Permalink
feat: add support for neon DB
Browse files Browse the repository at this point in the history
Signed-off-by: Francisco Madeira <francisco.madeira@diconium.com>
  • Loading branch information
fgmadeira committed Feb 13, 2024
1 parent cdc2151 commit dd692cb
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 34 deletions.
21 changes: 15 additions & 6 deletions .github/workflows/dashboard-feature-env.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,25 @@ jobs:
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}

- name: Create env file
- name: Create Neon Branch
id: create-branch
uses: neondatabase/create-branch-action@v4
with:
project_id: ${{ env.NEON_PROJECT_ID }}
branch_name: preview/pr-${{ github.event.number }}
username: ${{ env.NEON_DATABASE_USERNAME }}
api_key: ${{ secrets.NEON_API_KEY }}

- name: Setup environment
run: |
echo "${{ secrets.AZURE_BLOB_STORAGE_ACCOUNT }}" >> ./ethereal-nexus-dashboard/.env
echo "${{ secrets.AZURE_BLOB_STORAGE_SECRET }}" >> ./ethereal-nexus-dashboard/.env
echo "${{ secrets.PGHOST }}" >> ./ethereal-nexus-dashboard/.env
echo "${{ secrets.PGUSER }}" >> ./ethereal-nexus-dashboard/.env
echo "${{ secrets.PGPORT }}" >> ./ethereal-nexus-dashboard/.env
echo "${{ secrets.PGPASSWORD }}" >> ./ethereal-nexus-dashboard/.env
echo "PGSSL=true" >> ./ethereal-nexus-dashboard/.env
echo "DRIZZLE_DATABASE_TYPE=neon" >> ./ethereal-nexus-dashboard/.env
echo "DRIZZLE_DATABASE_URL=${{ steps.create-branch.outputs.db_url }}?sslmode=require" >> ./ethereal-nexus-dashboard/.env
echo "${{ secrets.NEXT_AUTH_SECRET }}" >> ./ethereal-nexus-dashboard/.env
cd ./ethereal-nexus-dashboard
npx drizzle-kit push:pg
- name: Deploy Container App
uses: azure/container-apps-deploy-action@v1
Expand Down
7 changes: 1 addition & 6 deletions ethereal-nexus-dashboard/drizzle.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ export default {
out: "./drizzle",
driver: 'pg',
dbCredentials: {
host: process.env.PGHOST!,
port: Number(process.env.PGPORT),
user: process.env.PGUSER,
password: process.env.PGPASSWORD,
database: process.env.PGDATABASE || 'postgres',
ssl: process.env.PGSSL === 'true',
connectionString: process.env.DRIZZLE_DATABASE_URL!
},
} satisfies Config;
87 changes: 85 additions & 2 deletions ethereal-nexus-dashboard/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions ethereal-nexus-dashboard/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"@azure/storage-blob": "^12.15.0",
"@epic-web/remember": "^1.0.2",
"@hookform/resolvers": "^3.3.1",
"@neondatabase/serverless": "^0.8.1",
"@radix-ui/react-alert-dialog": "^1.0.4",
"@radix-ui/react-avatar": "^1.0.3",
"@radix-ui/react-checkbox": "^1.0.4",
Expand Down
2 changes: 2 additions & 0 deletions ethereal-nexus-dashboard/src/data/components/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export const components = pgTable("component", {
id: uuid('id').primaryKey().notNull().defaultRandom(),
slug: text('slug').unique(),
name: text("name").notNull(),
title: text("title"),
description: text("description"),
})
export const componentsRelations = relations(components, ({ many }) => ({
versions: many(componentVersions),
Expand Down
51 changes: 33 additions & 18 deletions ethereal-nexus-dashboard/src/db/index.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,45 @@
import { drizzle } from 'drizzle-orm/postgres-js';
import { drizzle as drizzlePg } from 'drizzle-orm/postgres-js';
import { drizzle as drizzleNeon } from 'drizzle-orm/neon-http';
import postgres from 'postgres';
import { neon } from '@neondatabase/serverless';
import { remember } from '@epic-web/remember';

import * as users from '@/data/users/schema';
import * as projects from '@/data/projects/schema';
import * as member from '@/data/member/schema';
import * as components from '@/data/components/schema';
import { remember } from '@epic-web/remember';

const queryClient = postgres(
`postgres://${process.env.PGUSER}:${process.env.PGPASSWORD}@${
process.env.PGHOST
}:${process.env.PGPORT}/${process.env.PGDATABASE || 'postgres'}`,
{
max: 30,
idle_timeout: 20,
ssl: process.env.PGSSL === 'true',
},
);
function clientFactory() {
let drizzle, client;

export const db = remember('db', () =>
drizzle(queryClient, {
switch (process.env.DRIZZLE_DATABASE_TYPE) {
case 'neon':
drizzle = drizzleNeon;
client = neon(process.env.DRIZZLE_DATABASE_URL!);
break;
default:
drizzle = drizzlePg;
let connectionString =
client = postgres(
process.env.DRIZZLE_DATABASE_URL!,
{
max: 30,
idle_timeout: 20,
ssl: process.env.PGSSL === 'true'
}
);
}

return drizzle(client, {
schema: {
...users,
...projects,
...member,
...components,
},
}),
);
...components
}
})
}

export const db = remember('db', () =>
clientFactory()
);
3 changes: 1 addition & 2 deletions ethereal-nexus-dashboard/src/db/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { drizzle } from "drizzle-orm/postgres-js";
import { migrate } from "drizzle-orm/postgres-js/migrator";
import postgres from "postgres";

const connectionString = `postgres://${process.env.PGUSER}:${process.env.PGPASSWORD}@${process.env.PGHOST}:${process.env.PGPORT}/postgres`
const sql = postgres(connectionString, { max: 1, ssl: true })
const sql = postgres(process.env.DRIZZLE_DATABASE_URL!, { max: 1, ssl: true })
const db = drizzle(sql);

await migrate(db, { migrationsFolder: "drizzle" });
Expand Down

0 comments on commit dd692cb

Please sign in to comment.