Skip to main content

Project Model

The project model is a Mongoose model that defines the basic structure of the project data in the database. The project model is used to interact with the project data in the database.

For each project type (Ex. animal projects, shooting, etc.), a new model should be created within the src/app/_db/models/projects file. By separating the models into different files, we can then define schemas for the project dependent models.

Schema

export const projectModel = mongoose.Schema({
year: String,
projectName: String,
description: String,
type: String,
startDate: Date,
endDate: Date,
uid: {
type: mongoose.Schema.Types.ObjectId,
ref: "users"
}
});

export const Project = mongoose.models.projects || mongoose.model("projects", projectModel);

Example Usage

import { Project } from "../models/project";

export default async function createProject(data) {
try {
const project = new Project({
year: data.year,
projectName: data.projectName,
description: data.description,
type: data.type,
startDate: data.startDate,
endDate: data.endDate,
uid: data.uid

});

await project.save();
return project;
} catch (error) {
throw new Error(error.message);
}
}