diff --git a/packages/nextjs/next-env.d.ts b/packages/nextjs/next-env.d.ts index fd36f949..4f11a03d 100644 --- a/packages/nextjs/next-env.d.ts +++ b/packages/nextjs/next-env.d.ts @@ -1,6 +1,5 @@ /// /// -/// // NOTE: This file should not be edited // see https://nextjs.org/docs/basic-features/typescript for more information. diff --git a/packages/nextjs/services/database/collections.ts b/packages/nextjs/services/database/collections.ts index 25083586..bdb6c2ef 100644 --- a/packages/nextjs/services/database/collections.ts +++ b/packages/nextjs/services/database/collections.ts @@ -17,4 +17,19 @@ const listCollections = async () => { return collections; }; -export { listCollections }; +const addDocumentToCollection = async (collectionName: string, data: any) => { + const firestoreDB = getFirestoreConnector(); + + try { + // @notice Add a new document with a generated ID to the specified collection + // @dev Creates the collection if it doesn't already exist. + const docRef = await firestoreDB.collection(collectionName).add(data); + console.log("DocumentID: ", docRef.id); + return docRef.id; + } catch (error) { + console.error("Error adding document: ", error); + throw error; + } +}; + +export { listCollections, addDocumentToCollection }; diff --git a/packages/nextjs/services/database/metrics.ts b/packages/nextjs/services/database/metrics.ts index fac6487d..d0af08ee 100644 --- a/packages/nextjs/services/database/metrics.ts +++ b/packages/nextjs/services/database/metrics.ts @@ -1,5 +1,10 @@ +import { addDocumentToCollection } from "./collections"; import { getFirestoreConnector } from "./firestoreDB"; +import { Metric } from "./schema"; const firestoreDB = getFirestoreConnector(); -const getMetricDoc = (id: string) => firestoreDB.collection("metrics").doc(id); +const collectionName = "metrics"; +const getMetricDoc = (id: string) => firestoreDB.collection(collectionName).doc(id); +export const addMetricDoc = (metric: Metric) => addDocumentToCollection(collectionName, metric); + export const getMetricById = (id: string) => getMetricDoc(id).get(); diff --git a/packages/nextjs/services/database/projects.ts b/packages/nextjs/services/database/projects.ts index 20f9092a..c91cc80e 100644 --- a/packages/nextjs/services/database/projects.ts +++ b/packages/nextjs/services/database/projects.ts @@ -1,5 +1,9 @@ +import { addDocumentToCollection } from "./collections"; import { getFirestoreConnector } from "./firestoreDB"; +import { Project } from "./schema"; const firestoreDB = getFirestoreConnector(); -const getProjectDoc = (id: string) => firestoreDB.collection("projects").doc(id); +const collectionName = "projects"; +const getProjectDoc = (id: string) => firestoreDB.collection(collectionName).doc(id); export const getProjectById = (id: string) => getProjectDoc(id).get(); +export const addProjectDoc = (project: Project) => addDocumentToCollection(collectionName, project);