From 9ab4b6580c0d4df1e68877ed17c082217fcbab8e Mon Sep 17 00:00:00 2001 From: Wanjohi <71614375+wanjohiryan@users.noreply.github.com> Date: Mon, 3 Mar 2025 13:36:13 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9C=20fix(db):=20Fix=20database=20stat?= =?UTF-8?q?e=20issues=20(#203)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Description This attempts to fix all database issues for all teammates ## Related Issues 1. Getting funny errors while trying to run `sst dev` 2. The neon pulumi stuff trying to create a db each time on push ## Type of Change - [x] Bug fix (non-breaking change) - [ ] New feature (non-breaking change) - [ ] Breaking change (fix or feature that changes existing functionality) - [ ] Documentation update - [ ] Other (please describe): ## Checklist - [ ] I have updated relevant documentation - [ ] My code follows the project's coding style - [x] My changes generate no new warnings/errors ## Notes for Reviewers ## Screenshots/Demo ## Additional Context --- infra/database.ts | 14 ++++++++------ packages/core/drizzle.config.ts | 10 +++++++++- packages/core/src/drizzle/index.ts | 10 +++++++++- packages/functions/src/auth.ts | 12 ++++++------ 4 files changed, 32 insertions(+), 14 deletions(-) diff --git a/infra/database.ts b/infra/database.ts index e758ab42..8a96287a 100644 --- a/infra/database.ts +++ b/infra/database.ts @@ -1,27 +1,29 @@ //Created manually from the dashboard and shared with the whole team/org const dbProject = neon.getProjectOutput({ - id:"red-mud-17843368" + id: "little-band-76544985", }) const dbBranchId = $app.stage !== "production" ? - new neon.Branch("DatabaseBranch", { + new neon.Branch("NeonBranch", { parentId: dbProject.defaultBranchId, projectId: dbProject.id, name: $app.stage, }).id : dbProject.defaultBranchId -const dbEndpoint = new neon.Endpoint("NestriEndpoint", { +const dbEndpoint = new neon.Endpoint("NeonEndpoint", { projectId: dbProject.id, - branchId: dbBranchId + branchId: dbBranchId, + poolerEnabled: true, + type: "read_write", }) -const dbRole = new neon.Role("AdminRole", { +const dbRole = new neon.Role("NeonRole", { name: "admin", branchId: dbBranchId, projectId: dbProject.id, }) -const db = new neon.Database("NestriDatabase", { +const db = new neon.Database("NeonDatabase", { branchId: dbBranchId, projectId: dbProject.id, ownerName: dbRole.name, diff --git a/packages/core/drizzle.config.ts b/packages/core/drizzle.config.ts index 98e3f64a..efff367e 100644 --- a/packages/core/drizzle.config.ts +++ b/packages/core/drizzle.config.ts @@ -1,12 +1,20 @@ import { Resource } from "sst"; import { defineConfig } from "drizzle-kit"; +function addPoolerSuffix(original: string): string { + const firstDotIndex = original.indexOf('.'); + if (firstDotIndex === -1) return original + '-pooler'; + return original.slice(0, firstDotIndex) + '-pooler' + original.slice(firstDotIndex); + } + +const dbHost = addPoolerSuffix(Resource.Database.host) + export default defineConfig({ schema: "./src/**/*.sql.ts", out: "./migrations", dialect: "postgresql", verbose: true, dbCredentials: { - url: `postgresql://${Resource.Database.user}:${Resource.Database.password}@${Resource.Database.host}/${Resource.Database.name}?sslmode=require`, + url: `postgresql://${Resource.Database.user}:${Resource.Database.password}@${dbHost}/${Resource.Database.name}?sslmode=require`, }, }); \ No newline at end of file diff --git a/packages/core/src/drizzle/index.ts b/packages/core/src/drizzle/index.ts index 47d0e4d1..32d53ff2 100644 --- a/packages/core/src/drizzle/index.ts +++ b/packages/core/src/drizzle/index.ts @@ -7,7 +7,15 @@ import { Pool, neonConfig } from "@neondatabase/serverless"; neonConfig.webSocketConstructor = ws; -const client = new Pool({ connectionString: `postgres://${Resource.Database.user}:${Resource.Database.password}@${Resource.Database.host}/${Resource.Database.name}?sslmode=require` }) +function addPoolerSuffix(original: string): string { + const firstDotIndex = original.indexOf('.'); + if (firstDotIndex === -1) return original + '-pooler'; + return original.slice(0, firstDotIndex) + '-pooler' + original.slice(firstDotIndex); + } + +const dbHost = addPoolerSuffix(Resource.Database.host) + +const client = new Pool({ connectionString: `postgres://${Resource.Database.user}:${Resource.Database.password}@${dbHost}/${Resource.Database.name}?sslmode=require` }) export const db = neonDrizzle(client, { logger: diff --git a/packages/functions/src/auth.ts b/packages/functions/src/auth.ts index 41db11cf..513800e0 100644 --- a/packages/functions/src/auth.ts +++ b/packages/functions/src/auth.ts @@ -64,12 +64,12 @@ const app = issuer({ PasswordUI({ sendCode: async (email, code) => { console.log("email & code:", email, code) - await Email.send( - "auth", - email, - `Nestri code: ${code}`, - `Your Nestri login code is ${code}`, - ) + // await Email.send( + // "auth", + // email, + // `Nestri code: ${code}`, + // `Your Nestri login code is ${code}`, + // ) }, }), ),