-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMIGRATION_EXAMPLES.ts
More file actions
200 lines (162 loc) · 4.84 KB
/
Copy pathMIGRATION_EXAMPLES.ts
File metadata and controls
200 lines (162 loc) · 4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Migration Guide: Firestore to PostgreSQL Query Examples
/*
===========================================
BEFORE: Firestore Queries
===========================================
*/
// OLD: Get user applications
/*
import { collection, query, where, orderBy, getDocs } from 'firebase/firestore';
import { db } from '@/lib/firebase';
const q = query(
collection(db, `users/${uid}/applications`),
orderBy('createdAt', 'desc')
);
const snapshot = await getDocs(q);
const applications = snapshot.docs.map(doc => ({ id: doc.id, ...doc.data() }));
*/
// OLD: Create application
/*
import { addDoc, collection, serverTimestamp } from 'firebase/firestore';
await addDoc(collection(db, `users/${uid}/applications`), {
title: 'Software Engineer',
company: 'Google',
status: 'Applied',
createdAt: serverTimestamp()
});
*/
// OLD: Update application
/*
import { updateDoc, doc } from 'firebase/firestore';
await updateDoc(doc(db, `users/${uid}/applications/${appId}`), {
status: 'Rejected',
lastActionAt: serverTimestamp()
});
*/
// OLD: Listen to real-time changes
/*
import { onSnapshot, query, collection } from 'firebase/firestore';
const unsubscribe = onSnapshot(
query(collection(db, `users/${uid}/applications`)),
(snapshot) => {
const apps = snapshot.docs.map(doc => ({ id: doc.id, ...doc.data() }));
setApplications(apps);
}
);
*/
/*
===========================================
AFTER: PostgreSQL with API Routes
===========================================
*/
// NEW: Get user applications
/*
import { apiClient } from '@/lib/api-client';
const response = await apiClient.get('/api/applications');
const { applications } = await response.json();
*/
// NEW: Create application
/*
import { apiClient } from '@/lib/api-client';
const newApp = {
title: 'Software Engineer',
company: 'Google',
status: 'Applied'
};
const response = await apiClient.post('/api/applications', newApp);
const { application } = await response.json();
*/
// NEW: Update application
/*
import { apiClient } from '@/lib/api-client';
const updates = {
status: 'Rejected'
};
const response = await apiClient.patch(`/api/applications?id=${appId}`, updates);
const { application } = await response.json();
*/
// NEW: Real-time updates (using polling or WebSocket)
/*
import { apiClient } from '@/lib/api-client';
import { useEffect, useState } from 'react';
function useApplications() {
const [applications, setApplications] = useState([]);
useEffect(() => {
const fetchApplications = async () => {
try {
const response = await apiClient.get('/api/applications');
const { applications: apps } = await response.json();
setApplications(apps);
} catch (error) {
console.error('Failed to fetch applications:', error);
}
};
fetchApplications();
// Poll for updates every 30 seconds (or use WebSocket)
const interval = setInterval(fetchApplications, 30000);
return () => clearInterval(interval);
}, []);
return applications;
}
*/
/*
===========================================
SERVER-SIDE: API Route Patterns
===========================================
*/
// Pattern 1: Using withAuth middleware
/*
import { withAuth, type AuthenticatedRequest } from '@/lib/auth-middleware';
import { db } from '@/lib/db';
export const GET = withAuth(async (req: AuthenticatedRequest) => {
const { user } = req; // user.id is the PostgreSQL user ID
const result = await db.query(
'SELECT * FROM applications WHERE user_id = $1 ORDER BY created_at DESC',
[user.id]
);
return NextResponse.json({ applications: result.rows });
});
*/
// Pattern 2: Manual authentication
/*
import { authenticate } from '@/lib/auth-middleware';
import { db } from '@/lib/db';
export async function POST(req: NextRequest) {
try {
const { user } = await authenticate(req);
const body = await req.json();
const result = await db.query(
'INSERT INTO applications (user_id, title_text, company) VALUES ($1, $2, $3) RETURNING *',
[user.id, body.title, body.company]
);
return NextResponse.json({ application: result.rows[0] });
} catch (error) {
if (error.message.includes('token')) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
return NextResponse.json({ error: 'Server error' }, { status: 500 });
}
}
*/
/*
===========================================
KEY SECURITY PRINCIPLE
===========================================
*/
// ❌ NEVER trust user_id from client
/*
const badQuery = `
SELECT * FROM applications
WHERE user_id = '${req.body.userId}' // DANGEROUS!
`;
*/
// ✅ ALWAYS derive user_id from verified token
/*
const { user } = await authenticate(req); // Verify token first
const goodQuery = `
SELECT * FROM applications
WHERE user_id = $1 // Use verified user.id
`;
await db.query(goodQuery, [user.id]);
*/
export {}; // Make this a module