Skip to content

Commit d4d18d3

Browse files
committed
Added database to services
1 parent d458352 commit d4d18d3

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// import { firestore } from "firebase-admin";
2+
import getFirestoreConnector from "~~/services/database/firestoreDB";
3+
4+
// import getFirestoreConnector from "./firestoreDB.js";
5+
6+
// Firestore instance
7+
const firestoreDB = getFirestoreConnector();
8+
9+
// Function to create a collection
10+
export async function createCollection({
11+
name,
12+
symbol,
13+
tokenURI,
14+
userAddress,
15+
}: {
16+
name: string;
17+
symbol: string;
18+
tokenURI: string;
19+
userAddress: string;
20+
}) {
21+
try {
22+
console.log("Creating collection with data:", { name, symbol, tokenURI, userAddress });
23+
24+
const collectionRef = await firestoreDB.collection("collections").add({
25+
name,
26+
symbol,
27+
tokenURI,
28+
userAddress,
29+
// createdAt: new Date().toISOString(),
30+
});
31+
32+
console.log("Collection created with ID:", collectionRef.id);
33+
34+
return {
35+
id: collectionRef.id,
36+
name,
37+
symbol,
38+
tokenURI,
39+
userAddress,
40+
};
41+
} catch (error) {
42+
console.error("Error creating collection:", error);
43+
throw new Error("Failed to create collection");
44+
}
45+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { firestore } from "firebase-admin";
2+
import admin from "firebase-admin";
3+
4+
// Ensure Firebase Admin SDK is initialized
5+
if (!admin.apps.length) {
6+
admin.initializeApp({
7+
credential: admin.credential.applicationDefault(),
8+
});
9+
}
10+
11+
const getFirestoreConnector = () => {
12+
console.log("Initializing Firestore");
13+
return firestore(); // Correctly returns the Firestore instance
14+
};
15+
16+
export default getFirestoreConnector;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import getFirestoreConnector from "~~/services/database/firestoreDB";
2+
3+
// Firestore instance
4+
const firestoreDB = getFirestoreConnector();
5+
6+
// Find a user (or user) by their wallet address
7+
export async function findUserByAddress(address: string) {
8+
const userSnapshot = await firestoreDB.collection("users").where("walletAddress", "==", address).get();
9+
10+
if (!userSnapshot.empty) {
11+
const userDoc = userSnapshot.docs[0];
12+
return { exists: true, data: userDoc.data() };
13+
}
14+
15+
return { exists: false };
16+
}

0 commit comments

Comments
 (0)