generated from scaffold-eth/scaffold-eth-2
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Firebase (firestore connection) (#9)
- Loading branch information
Showing
9 changed files
with
1,366 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"emulators": { | ||
"firestore": { | ||
"port": 8080 | ||
}, | ||
"ui": { | ||
"enabled": true, | ||
"port": 4000 | ||
}, | ||
"singleProjectMode": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,10 @@ | ||
# Template for NextJS environment variables. | ||
# NEXT_PUBLIC_ALCHEMY_API_KEY= | ||
# NEXT_PUBLIC_WALLET_CONNECT_PROJECT_ID= | ||
|
||
# For local development, copy this file, rename it to .env.local, and fill in the values. | ||
# When deploying live, you'll need to store the vars in Vercel/System config. | ||
# Firebase live vs local emulator. | ||
# Important: Only one of the following lines should be uncommented at a time. | ||
|
||
# If not set, we provide default values (check `scaffold.config.ts`) so developers can start prototyping out of the box, | ||
# but we recommend getting your own API Keys for Production Apps. | ||
|
||
# To access the values stored in this env file you can use: process.env.VARIABLENAME | ||
# You'll need to prefix the variables names with NEXT_PUBLIC_ if you want to access them on the client side. | ||
# More info: https://nextjs.org/docs/pages/building-your-application/configuring/environment-variables | ||
NEXT_PUBLIC_ALCHEMY_API_KEY= | ||
NEXT_PUBLIC_WALLET_CONNECT_PROJECT_ID= | ||
# If you want to connecto to a live firebase project, you can use download the service account key json file and add the path here | ||
GOOGLE_APPLICATION_CREDENTIALS="/<your>/<path>/<to>/firebaseServiceAccountKey.json" | ||
# If you want to connect to the firebase emulator | ||
FIRESTORE_EMULATOR_HOST=localhost:8080 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Just to test Firebase Functions. | ||
import { firestore } from "firebase-admin"; | ||
import { getFirestoreConnector } from "~~/services/database/firestoreDB"; | ||
|
||
import CollectionReference = firestore.CollectionReference; | ||
|
||
const listCollections = async () => { | ||
const firestoreDB = getFirestoreConnector(); | ||
let collections: CollectionReference[] = []; | ||
|
||
try { | ||
collections = await firestoreDB.listCollections(); | ||
} catch (error) { | ||
console.error("Error listing collections:", error); | ||
} | ||
|
||
return collections; | ||
}; | ||
|
||
export { listCollections }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { applicationDefault, getApps, initializeApp } from "firebase-admin/app"; | ||
import { getFirestore } from "firebase-admin/firestore"; | ||
|
||
const getFirestoreConnector = () => { | ||
// Not sure if this is the best way to do this | ||
// But if not, the app gives an error that the app has already been initialized. | ||
if (getApps().length > 0) { | ||
return getFirestore(); | ||
} | ||
|
||
if (process.env.GOOGLE_APPLICATION_CREDENTIALS) { | ||
console.log("Initializing LIVE Firestore"); | ||
initializeApp({ | ||
credential: applicationDefault(), | ||
}); | ||
} else { | ||
// ToDo. Something is not working. Getting "Error: Could not load the default credentials." | ||
console.log("Initializing local Firestore instance"); | ||
initializeApp({ | ||
projectId: "buidlguidl-v3", | ||
}); | ||
} | ||
|
||
return getFirestore(); | ||
}; | ||
|
||
export { getFirestoreConnector }; |
Oops, something went wrong.