Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added write methods to collection.ts, metrics.ts, and projects.ts #11

Merged
merged 1 commit into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Loading