Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions bun.lock

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

6 changes: 3 additions & 3 deletions drizzle/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { InferSelectModel } from "drizzle-orm";
import type { NodePgDatabase } from "drizzle-orm/node-postgres";
import type { NeonHttpDatabase } from "drizzle-orm/neon-http";
import type * as dbSchema from "./schema";
import type {
activity,
blob,
Expand All @@ -10,9 +11,8 @@ import type {
taskList,
user,
} from "./schema";
import type * as dbSchema from "./schema";

export type Database = NodePgDatabase<typeof dbSchema>;
export type Database = NeonHttpDatabase<typeof dbSchema>;

export type User = InferSelectModel<typeof user>;
export type Project = InferSelectModel<typeof project>;
Expand Down
37 changes: 17 additions & 20 deletions lib/utils/useDatabase.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { neon } from "@neondatabase/serverless";
import { sql } from "drizzle-orm";
import { upstashCache } from "drizzle-orm/cache/upstash";
import { drizzle } from "drizzle-orm/node-postgres";
import { drizzle } from "drizzle-orm/neon-http";
import { err, ok, type Result } from "neverthrow";
import { cache } from "react";
import type { Database } from "@/drizzle/types";
import * as schema from "../../drizzle/schema";
import { getOwner } from "./useOwner";
Expand All @@ -13,14 +15,14 @@ export function getDatabaseName(ownerId: string): Result<string, string> {
return ok(ownerId.toLowerCase().replace(/ /g, "_"));
}

export async function database(): Promise<Database> {
export const database = cache(async (): Promise<Database> => {
const { ownerId } = await getOwner();
if (!ownerId) {
throw new Error("Owner ID not found");
}

return getDatabaseForOwner(ownerId);
}
});

export async function getDatabaseForOwner(ownerId: string): Promise<Database> {
const databaseName = getDatabaseName(ownerId).match(
Expand All @@ -31,20 +33,17 @@ export async function getDatabaseForOwner(ownerId: string): Promise<Database> {
);

const sslMode = process.env.DATABASE_SSL === "true" ? "?sslmode=require" : "";
const client = neon(`${process.env.DATABASE_URL}/${databaseName}${sslMode}`);

const tenantDb = drizzle(
`${process.env.DATABASE_URL}/${databaseName}${sslMode}`,
{
cache: upstashCache({
url: process.env.UPSTASH_REDIS_REST_URL!,
token: process.env.UPSTASH_REDIS_REST_TOKEN!,
global: true,
}),
schema,
},
);

return tenantDb;
return drizzle({
client,
cache: upstashCache({
url: process.env.UPSTASH_REDIS_REST_URL!,
token: process.env.UPSTASH_REDIS_REST_TOKEN!,
global: true,
}),
schema,
});
}

export async function deleteDatabase(ownerId: string) {
Expand All @@ -56,10 +55,8 @@ export async function deleteDatabase(ownerId: string) {
);

const sslMode = process.env.DATABASE_SSL === "true" ? "?sslmode=require" : "";

const ownerDb = drizzle(`${process.env.DATABASE_URL}/manage${sslMode}`, {
schema,
});
const client = neon(`${process.env.DATABASE_URL}/manage${sslMode}`);
const ownerDb = drizzle({ client, schema });

// Terminate all connections to the database before dropping
await ownerDb.execute(sql`
Expand Down
4 changes: 2 additions & 2 deletions ops/drizzle/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { NodePgDatabase } from "drizzle-orm/node-postgres";
import type { NeonHttpDatabase } from "drizzle-orm/neon-http";
import type * as dbSchema from "./schema";

export type OpsDatabase = NodePgDatabase<typeof dbSchema>;
export type OpsDatabase = NeonHttpDatabase<typeof dbSchema>;
11 changes: 4 additions & 7 deletions ops/useOps.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import type { UserJSON } from "@clerk/nextjs/server";
import { drizzle } from "drizzle-orm/node-postgres";
import { neon } from "@neondatabase/serverless";
import { drizzle } from "drizzle-orm/neon-http";
import * as schema from "./drizzle/schema";
import type { OpsDatabase } from "./drizzle/types";

export async function getOpsDatabase(): Promise<OpsDatabase> {
const sslMode = process.env.DATABASE_SSL === "true" ? "?sslmode=require" : "";

const ownerDb = drizzle(`${process.env.DATABASE_URL}/manage${sslMode}`, {
schema,
});

return ownerDb;
const client = neon(`${process.env.DATABASE_URL}/manage${sslMode}`);
return drizzle({ client, schema });
}

export async function addUserToOpsDb(userData: UserJSON) {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"@dnd-kit/sortable": "^8.0.0",
"@dnd-kit/utilities": "^3.2.2",
"@headlessui/react": "^2.2.0",
"@neondatabase/serverless": "^1.0.2",
"@radix-ui/react-avatar": "^1.1.0",
"@radix-ui/react-checkbox": "^1.1.1",
"@radix-ui/react-collapsible": "^1.1.2",
Expand Down Expand Up @@ -61,7 +62,7 @@
"date-fns-tz": "^3.2.0",
"dompurify": "^3.2.5",
"dotenv": "^16.4.7",
"drizzle-orm": "^0.44.5",
"drizzle-orm": "^0.44.6",
"es-toolkit": "^1.39.8",
"eslint-config-next": "15.5.5",
"ical-generator": "^8.0.1",
Expand Down