Skip to content

Commit

Permalink
added write methods to collection.ts, metrics.ts, and projects.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
blahkheart committed Jun 15, 2024
1 parent 0d82fcd commit a3b004f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
1 change: 0 additions & 1 deletion packages/nextjs/next-env.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/// <reference types="next" />
/// <reference types="next/image-types/global" />
/// <reference types="next/navigation-types/compat/navigation" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
17 changes: 16 additions & 1 deletion packages/nextjs/services/database/collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
7 changes: 6 additions & 1 deletion packages/nextjs/services/database/metrics.ts
Original file line number Diff line number Diff line change
@@ -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();
6 changes: 5 additions & 1 deletion packages/nextjs/services/database/projects.ts
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit a3b004f

Please sign in to comment.