Skip to content

Commit 79baf62

Browse files
committed
feat: DATA-12840 Implement lazy loading for DB
1 parent 52504ef commit 79baf62

File tree

1 file changed

+28
-19
lines changed

1 file changed

+28
-19
lines changed

src/lib/db.ts

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import { initializeApp } from 'firebase/app';
1+
import { FirebaseApp, initializeApp } from 'firebase/app';
22
import {
33
deleteDoc,
44
doc,
55
getDoc,
66
getFirestore,
77
setDoc,
88
Timestamp,
9+
Firestore
910
} from 'firebase/firestore';
1011
import { env } from '~/env.mjs';
1112

@@ -26,21 +27,29 @@ export interface AuthProps {
2627
user: User;
2728
}
2829

29-
const { FIRE_API_KEY, FIRE_DOMAIN, FIRE_PROJECT_ID } = env;
30-
31-
const firebaseConfig = {
32-
apiKey: FIRE_API_KEY,
33-
authDomain: FIRE_DOMAIN,
34-
projectId: FIRE_PROJECT_ID,
35-
};
36-
3730
export interface ClientTokenData {
3831
token: string;
3932
expiresAt: Timestamp;
4033
}
4134

42-
const app = initializeApp(firebaseConfig);
43-
const db = getFirestore(app);
35+
let app: FirebaseApp;
36+
let db: Firestore;
37+
38+
function getDb() {
39+
if (!db) {
40+
const { FIRE_API_KEY, FIRE_DOMAIN, FIRE_PROJECT_ID } = env;
41+
42+
const firebaseConfig = {
43+
apiKey: FIRE_API_KEY,
44+
authDomain: FIRE_DOMAIN,
45+
projectId: FIRE_PROJECT_ID,
46+
};
47+
48+
app = initializeApp(firebaseConfig);
49+
db = getFirestore(app);
50+
}
51+
return db;
52+
}
4453

4554
// Firestore data management functions
4655

@@ -49,7 +58,7 @@ export async function setUser(user: User) {
4958
if (!user) return Promise.resolve();
5059

5160
const { email, id, username } = user;
52-
const ref = doc(db, 'users', String(id));
61+
const ref = doc(getDb(), 'users', String(id));
5362
const data: UserData = { email };
5463

5564
if (username) {
@@ -70,7 +79,7 @@ export async function setStore(props: AuthProps) {
7079
if (!accessToken || !scope) return null;
7180

7281
const storeHash = context?.split('/')[1] || '';
73-
const ref = doc(db, 'store', storeHash);
82+
const ref = doc(getDb(), 'store', storeHash);
7483
const data = { accessToken, adminId: id, scope };
7584

7685
await setDoc(ref, data);
@@ -88,27 +97,27 @@ export async function setStoreUser(session: AuthProps) {
8897

8998
const storeHash = context.split('/')[1];
9099
const documentId = `${userId}_${storeHash}`;
91-
const ref = doc(db, 'storeUsers', documentId);
100+
const ref = doc(getDb(), 'storeUsers', documentId);
92101

93102
await setDoc(ref, { storeHash });
94103
}
95104

96105
export async function deleteUser(storeHash: string, user: User) {
97106
const docId = `${user.id}_${storeHash}`;
98-
const ref = doc(db, 'storeUsers', docId);
107+
const ref = doc(getDb(), 'storeUsers', docId);
99108

100109
await deleteDoc(ref);
101110
}
102111

103112
export async function getStoreToken(storeHash: string): Promise<string | null> {
104113
if (!storeHash) return null;
105-
const storeDoc = await getDoc(doc(db, 'store', storeHash));
114+
const storeDoc = await getDoc(doc(getDb(), 'store', storeHash));
106115

107116
return storeDoc.data()?.accessToken;
108117
}
109118

110119
export async function deleteStore(storeHash: string) {
111-
const ref = doc(db, 'store', storeHash);
120+
const ref = doc(getDb(), 'store', storeHash);
112121
await deleteDoc(ref);
113122
}
114123

@@ -120,7 +129,7 @@ export async function saveClientToken(clientToken: string): Promise<string> {
120129
const exchangeToken = crypto.randomUUID();
121130
const expiresAt = Timestamp.fromMillis(Date.now() + 120 * 1000); // 2 minutes from now
122131

123-
const ref = doc(db, 'exchangeTokens', exchangeToken);
132+
const ref = doc(getDb(), 'exchangeTokens', exchangeToken);
124133
const data: Omit<ClientTokenData, 'expiresAt'> & { expiresAt: Timestamp } = {
125134
token: clientToken,
126135
expiresAt
@@ -132,7 +141,7 @@ export async function saveClientToken(clientToken: string): Promise<string> {
132141
}
133142

134143
export async function getClientTokenMaybeAndDelete(exchangeToken: string): Promise<string | false> {
135-
const ref = doc(db, 'exchangeTokens', exchangeToken);
144+
const ref = doc(getDb(), 'exchangeTokens', exchangeToken);
136145
const docSnap = await getDoc(ref);
137146

138147
await deleteDoc(ref);

0 commit comments

Comments
 (0)