Skip to content

Commit 25aa6d8

Browse files
committed
feat: Add MongoDB connection utilities, connector/form persistence, and a data viewer UI.
1 parent b526e15 commit 25aa6d8

3 files changed

Lines changed: 41 additions & 21 deletions

File tree

apps/web/app/viewer/page.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import { useState, useEffect } from 'react';
44
import { useSearchParams } from 'next/navigation';
55

6-
export default function ViewerPage() {
6+
import { Suspense } from 'react';
7+
8+
function ViewerContent() {
79
const searchParams = useSearchParams();
810

911
const [url, setUrl] = useState('');
@@ -135,3 +137,11 @@ export default function ViewerPage() {
135137
</div>
136138
);
137139
}
140+
141+
export default function ViewerPage() {
142+
return (
143+
<Suspense fallback={<div>Loading...</div>}>
144+
<ViewerContent />
145+
</Suspense>
146+
);
147+
}

apps/web/lib/mongoose.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ import mongoose from 'mongoose';
22

33
const MONGODB_URI = process.env.MONGODB_URI;
44

5-
if (!MONGODB_URI) {
6-
throw new Error('Please define the MONGODB_URI environment variable inside .env');
7-
}
5+
// Removed top-level throw to allow build without env vars. Checks happen in connectToDatabase.
86

97
interface MongooseCache {
108
conn: typeof mongoose | null;
@@ -30,6 +28,10 @@ async function connectToDatabase() {
3028
}
3129

3230
if (!cached.promise) {
31+
if (!MONGODB_URI) {
32+
throw new Error('Please define the MONGODB_URI environment variable inside .env');
33+
}
34+
3335
const opts = {
3436
bufferCommands: false,
3537
};

apps/web/lib/server-db.ts

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@ export interface Form {
2323
}
2424

2525
// --- Persistence ---
26-
if (!process.env.MONGODB_URI) {
27-
throw new Error('Invalid/Missing environment variable: "MONGODB_URI"');
28-
}
26+
// --- Persistence ---
27+
// NOTE: We check this lazily or let it fail at runtime in functions to allow build-time execution without env vars.
2928

3029
const uri = process.env.MONGODB_URI;
3130
const dbName = process.env.MONGODB_DB_NAME || 'postpipe_core';
@@ -34,25 +33,34 @@ const options = {};
3433
let client;
3534
let clientPromise: Promise<MongoClient>;
3635

37-
if (process.env.NODE_ENV === 'development') {
38-
// In development mode, use a global variable so that the value
39-
// is preserved across module reloads caused by HMR (Hot Module Replacement).
40-
// @ts-ignore
41-
if (!global._mongoClientPromise) {
42-
client = new MongoClient(uri, options);
36+
function getClientPromise(): Promise<MongoClient> {
37+
if (clientPromise) return clientPromise;
38+
39+
if (!uri) {
40+
throw new Error('Invalid/Missing environment variable: "MONGODB_URI"');
41+
}
42+
43+
if (process.env.NODE_ENV === 'development') {
44+
// In development mode, use a global variable so that the value
45+
// is preserved across module reloads caused by HMR.
4346
// @ts-ignore
44-
global._mongoClientPromise = client.connect();
47+
if (!global._mongoClientPromise) {
48+
client = new MongoClient(uri, options);
49+
// @ts-ignore
50+
global._mongoClientPromise = client.connect();
51+
}
52+
// @ts-ignore
53+
clientPromise = global._mongoClientPromise;
54+
} else {
55+
// In production mode, it's best to not use a global variable.
56+
client = new MongoClient(uri, options);
57+
clientPromise = client.connect();
4558
}
46-
// @ts-ignore
47-
clientPromise = global._mongoClientPromise;
48-
} else {
49-
// In production mode, it's best to not use a global variable.
50-
client = new MongoClient(uri, options);
51-
clientPromise = client.connect();
59+
return clientPromise;
5260
}
5361

5462
async function getDB() {
55-
const c = await clientPromise;
63+
const c = await getClientPromise();
5664
return c.db(dbName);
5765
}
5866

0 commit comments

Comments
 (0)