1
- import {
2
- update as dbUpdate ,
3
- endAt ,
4
- get ,
5
- getDatabase ,
6
- limitToLast ,
7
- orderByChild ,
8
- query ,
9
- ref ,
10
- startAt ,
11
- } from 'firebase/database' ;
12
- import { app } from '../base' ;
1
+ import { update as dbUpdate , endAt , get , limitToLast , orderByChild , query , ref , startAt } from 'firebase/database' ;
2
+ import { getFirebaseDatabase } from '../utils/firebaseDatabase' ;
13
3
import {
14
4
type EnrichedBlueprintSummary ,
15
5
type RawBlueprint ,
@@ -161,7 +151,7 @@ export const fetchBlueprint = async (
161
151
}
162
152
163
153
// Fall back to Firebase if CDN failed or data was stale
164
- const blueprintRef = ref ( getDatabase ( app ) , `/blueprints/${ blueprintId } /` ) ;
154
+ const blueprintRef = ref ( getFirebaseDatabase ( ) , `/blueprints/${ blueprintId } /` ) ;
165
155
const snapshot = await get ( blueprintRef ) ;
166
156
167
157
if ( ! snapshot . exists ( ) ) {
@@ -179,7 +169,7 @@ export const fetchBlueprint = async (
179
169
180
170
export const fetchBlueprintTags = async ( blueprintId : string ) : Promise < string [ ] > => {
181
171
try {
182
- const tagsRef = ref ( getDatabase ( app ) , `/blueprints/${ blueprintId } /tags/` ) ;
172
+ const tagsRef = ref ( getFirebaseDatabase ( ) , `/blueprints/${ blueprintId } /tags/` ) ;
183
173
const snapshot = await get ( tagsRef ) ;
184
174
return snapshot . exists ( ) ? snapshot . val ( ) : [ ] ;
185
175
} catch ( error ) {
@@ -192,7 +182,7 @@ export const fetchBlueprintTags = async (blueprintId: string): Promise<string[]>
192
182
193
183
export const fetchTags = async ( ) : Promise < Record < string , string [ ] > > => {
194
184
try {
195
- const tagsRef = ref ( getDatabase ( app ) , '/tags/' ) ;
185
+ const tagsRef = ref ( getFirebaseDatabase ( ) , '/tags/' ) ;
196
186
const snapshot = await get ( tagsRef ) ;
197
187
198
188
if ( ! snapshot . exists ( ) ) {
@@ -220,7 +210,7 @@ export const fetchByTagData = async (tagId: string): Promise<Record<string, bool
220
210
}
221
211
222
212
try {
223
- const snapshot = await get ( ref ( getDatabase ( app ) , `/byTag/${ tagId } ` ) ) ;
213
+ const snapshot = await get ( ref ( getFirebaseDatabase ( ) , `/byTag/${ tagId } ` ) ) ;
224
214
return snapshot . val ( ) || { } ;
225
215
} catch ( error ) {
226
216
if ( isNetworkError ( error ) ) {
@@ -232,7 +222,7 @@ export const fetchByTagData = async (tagId: string): Promise<Record<string, bool
232
222
233
223
export const fetchModerator = async ( userId : string ) : Promise < boolean > => {
234
224
try {
235
- const moderatorRef = ref ( getDatabase ( app ) , `/moderators/${ userId } ` ) ;
225
+ const moderatorRef = ref ( getFirebaseDatabase ( ) , `/moderators/${ userId } ` ) ;
236
226
const snapshot = await get ( moderatorRef ) ;
237
227
238
228
return Boolean ( snapshot . val ( ) ) ;
@@ -246,7 +236,7 @@ export const fetchModerator = async (userId: string): Promise<boolean> => {
246
236
247
237
export const fetchUserDisplayName = async ( userId : string ) : Promise < string | null > => {
248
238
try {
249
- const userRef = ref ( getDatabase ( app ) , `/users/${ userId } /displayName` ) ;
239
+ const userRef = ref ( getFirebaseDatabase ( ) , `/users/${ userId } /displayName` ) ;
250
240
const snapshot = await get ( userRef ) ;
251
241
252
242
return snapshot . val ( ) ;
@@ -260,7 +250,7 @@ export const fetchUserDisplayName = async (userId: string): Promise<string | nul
260
250
261
251
export const fetchUserBlueprints = async ( userId : string ) : Promise < Record < string , boolean > > => {
262
252
try {
263
- const snapshot = await get ( ref ( getDatabase ( app ) , `/users/${ userId } /blueprints` ) ) ;
253
+ const snapshot = await get ( ref ( getFirebaseDatabase ( ) , `/users/${ userId } /blueprints` ) ) ;
264
254
return snapshot . val ( ) || { } ;
265
255
} catch ( error ) {
266
256
if ( isNetworkError ( error ) ) {
@@ -272,7 +262,7 @@ export const fetchUserBlueprints = async (userId: string): Promise<Record<string
272
262
273
263
export const fetchUserFavorites = async ( userId : string ) : Promise < Record < string , boolean > > => {
274
264
try {
275
- const snapshot = await get ( ref ( getDatabase ( app ) , `/users/${ userId } /favorites` ) ) ;
265
+ const snapshot = await get ( ref ( getFirebaseDatabase ( ) , `/users/${ userId } /favorites` ) ) ;
276
266
return snapshot . val ( ) || { } ;
277
267
} catch ( error ) {
278
268
if ( isNetworkError ( error ) ) {
@@ -284,7 +274,7 @@ export const fetchUserFavorites = async (userId: string): Promise<Record<string,
284
274
285
275
export const fetchUser = async ( userId : string ) : Promise < RawUser | null > => {
286
276
try {
287
- const userRef = ref ( getDatabase ( app ) , `/users/${ userId } ` ) ;
277
+ const userRef = ref ( getFirebaseDatabase ( ) , `/users/${ userId } ` ) ;
288
278
const snapshot = await get ( userRef ) ;
289
279
290
280
if ( ! snapshot . exists ( ) ) {
@@ -309,7 +299,7 @@ export const fetchUser = async (userId: string): Promise<RawUser | null> => {
309
299
310
300
export const fetchAllUsers = async ( ) : Promise < UserData [ ] > => {
311
301
try {
312
- const usersRef = ref ( getDatabase ( app ) , '/users/' ) ;
302
+ const usersRef = ref ( getFirebaseDatabase ( ) , '/users/' ) ;
313
303
const snapshot = await get ( usersRef ) ;
314
304
315
305
if ( ! snapshot . exists ( ) ) {
@@ -338,13 +328,13 @@ export const fetchAllUsers = async (): Promise<UserData[]> => {
338
328
} ;
339
329
340
330
export const reconcileFavoritesCount = async ( blueprintId : string ) : Promise < ReconcileResult > => {
341
- const favoritesRef = ref ( getDatabase ( app ) , `/blueprints/${ blueprintId } /favorites` ) ;
331
+ const favoritesRef = ref ( getFirebaseDatabase ( ) , `/blueprints/${ blueprintId } /favorites` ) ;
342
332
const favoritesSnapshot = await get ( favoritesRef ) ;
343
333
const favorites = favoritesSnapshot . exists ( ) ? favoritesSnapshot . val ( ) : { } ;
344
334
345
335
const actualCount = Object . values ( favorites ) . filter ( Boolean ) . length ;
346
336
347
- const summaryRef = ref ( getDatabase ( app ) , `/blueprintSummaries/${ blueprintId } /numberOfFavorites` ) ;
337
+ const summaryRef = ref ( getFirebaseDatabase ( ) , `/blueprintSummaries/${ blueprintId } /numberOfFavorites` ) ;
348
338
const summarySnapshot = await get ( summaryRef ) ;
349
339
const currentSummaryCount = summarySnapshot . exists ( ) ? summarySnapshot . val ( ) : 0 ;
350
340
@@ -355,7 +345,7 @@ export const reconcileFavoritesCount = async (blueprintId: string): Promise<Reco
355
345
[ `/blueprintSummaries/${ blueprintId } /numberOfFavorites` ] : actualCount ,
356
346
} ;
357
347
358
- await dbUpdate ( ref ( getDatabase ( app ) ) , updates ) ;
348
+ await dbUpdate ( ref ( getFirebaseDatabase ( ) ) , updates ) ;
359
349
}
360
350
361
351
return {
@@ -370,7 +360,7 @@ export const reconcileFavoritesCount = async (blueprintId: string): Promise<Reco
370
360
371
361
// TODO 2025-04-12: Move this out of firebase.js, and refactor it to use react query hooks from the hooks/ dir. The problem with the current implementation is that it performs many queries but doesn't cache anything. /users/${userId}/favorites already has a hook useUserFavorites in useUser. But `/blueprints/${blueprintId}/favorites/${userId}` doesn't have a hook or a mutation yet, so we need to add them.
372
362
export const reconcileUserFavorites = async ( userId : string ) : Promise < UserReconcileResult > => {
373
- const userFavoritesRef = ref ( getDatabase ( app ) , `/users/${ userId } /favorites` ) ;
363
+ const userFavoritesRef = ref ( getFirebaseDatabase ( ) , `/users/${ userId } /favorites` ) ;
374
364
const userFavoritesSnapshot = await get ( userFavoritesRef ) ;
375
365
const userFavorites = userFavoritesSnapshot . exists ( ) ? userFavoritesSnapshot . val ( ) : { } ;
376
366
@@ -386,7 +376,7 @@ export const reconcileUserFavorites = async (userId: string): Promise<UserReconc
386
376
continue ;
387
377
}
388
378
389
- const blueprintFavoritesRef = ref ( getDatabase ( app ) , `/blueprints/${ blueprintId } /favorites/${ userId } ` ) ;
379
+ const blueprintFavoritesRef = ref ( getFirebaseDatabase ( ) , `/blueprints/${ blueprintId } /favorites/${ userId } ` ) ;
390
380
const blueprintFavoriteSnapshot = await get ( blueprintFavoritesRef ) ;
391
381
392
382
if ( ! blueprintFavoriteSnapshot . exists ( ) || ! blueprintFavoriteSnapshot . val ( ) ) {
@@ -406,7 +396,7 @@ export const reconcileUserFavorites = async (userId: string): Promise<UserReconc
406
396
}
407
397
408
398
if ( Object . keys ( updates ) . length > 0 ) {
409
- await dbUpdate ( ref ( getDatabase ( app ) ) , updates ) ;
399
+ await dbUpdate ( ref ( getFirebaseDatabase ( ) ) , updates ) ;
410
400
}
411
401
412
402
return {
@@ -419,7 +409,7 @@ export const reconcileUserFavorites = async (userId: string): Promise<UserReconc
419
409
420
410
export const cleanupInvalidUserFavorite = async ( userId : string , blueprintId : string ) : Promise < boolean > => {
421
411
try {
422
- const summaryRef = ref ( getDatabase ( app ) , `/blueprintSummaries/${ blueprintId } ` ) ;
412
+ const summaryRef = ref ( getFirebaseDatabase ( ) , `/blueprintSummaries/${ blueprintId } ` ) ;
423
413
const summarySnapshot = await get ( summaryRef ) ;
424
414
425
415
if ( summarySnapshot . exists ( ) ) {
@@ -430,7 +420,7 @@ export const cleanupInvalidUserFavorite = async (userId: string, blueprintId: st
430
420
[ `/users/${ userId } /favorites/${ blueprintId } ` ] : null ,
431
421
} ;
432
422
433
- await dbUpdate ( ref ( getDatabase ( app ) ) , updates ) ;
423
+ await dbUpdate ( ref ( getFirebaseDatabase ( ) ) , updates ) ;
434
424
435
425
return true ;
436
426
} catch {
@@ -440,7 +430,7 @@ export const cleanupInvalidUserFavorite = async (userId: string, blueprintId: st
440
430
441
431
export const fetchBlueprintSummary = async ( blueprintId : string ) : Promise < RawBlueprintSummary | null > => {
442
432
try {
443
- const summaryRef = ref ( getDatabase ( app ) , `/blueprintSummaries/${ blueprintId } ` ) ;
433
+ const summaryRef = ref ( getFirebaseDatabase ( ) , `/blueprintSummaries/${ blueprintId } ` ) ;
444
434
const snapshot = await get ( summaryRef ) ;
445
435
446
436
if ( ! snapshot . exists ( ) ) {
@@ -469,14 +459,14 @@ export const fetchPaginatedSummaries = async (
469
459
try {
470
460
if ( lastKey && lastValue ) {
471
461
summariesQuery = query (
472
- ref ( getDatabase ( app ) , '/blueprintSummaries/' ) ,
462
+ ref ( getFirebaseDatabase ( ) , '/blueprintSummaries/' ) ,
473
463
orderByChild ( orderByField ) ,
474
464
endAt ( lastValue , lastKey ) ,
475
465
limitToLast ( pageSize + 1 ) ,
476
466
) ;
477
467
} else {
478
468
summariesQuery = query (
479
- ref ( getDatabase ( app ) , '/blueprintSummaries/' ) ,
469
+ ref ( getFirebaseDatabase ( ) , '/blueprintSummaries/' ) ,
480
470
orderByChild ( orderByField ) ,
481
471
limitToLast ( pageSize + 1 ) ,
482
472
) ;
@@ -544,7 +534,7 @@ export const fetchSummariesNewerThan = async (
544
534
) : Promise < RawBlueprintSummary [ ] > => {
545
535
try {
546
536
const summariesQuery = query (
547
- ref ( getDatabase ( app ) , '/blueprintSummaries/' ) ,
537
+ ref ( getFirebaseDatabase ( ) , '/blueprintSummaries/' ) ,
548
538
orderByChild ( 'lastUpdatedDate' ) ,
549
539
startAt ( highWatermark + 1 ) ,
550
540
limitToLast ( pageSize ) ,
0 commit comments