forked from freeCodeCamp/boilerplate-project-issuetracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crud.js
55 lines (48 loc) · 1.3 KB
/
crud.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
require("dotenv").config();
const mongoose = require("mongoose");
// Connects to database
mongoose.connect(process.env.DB, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
// Schemas
const Schema = mongoose.Schema;
const projectSchema = new Schema({
project_name: String,
issues: [{ type: Schema.Types.ObjectId, ref: "Issues" }],
});
const issueSchema = new Schema(
{
issue_title: String,
issue_text: String,
created_by: String,
assigned_to: String,
open: Boolean,
status_text: String,
project: { type: Schema.Types.ObjectId, ref: "Projects" },
},
{
timestamps: {
createdAt: "created_on",
updatedAt: "updated_on",
},
}
);
// Models
const Project = mongoose.model("Projects", projectSchema);
const Issue = mongoose.model("Issues", issueSchema);
/**
* Module for running CRUD operations once connected to the DB
* @module ./crud
*
*/
const crud = {
addProject: (data) => new Project(data).save(),
addIssue: (data) => new Issue(data).save(),
getProject: (title) => Project.findOne({ project_name: title }),
getAllIssues: (_id) => Issue.find({ project: _id }),
getIssues: (data) => Issue.find(data),
updateIssue: (_id, data) => Issue.updateOne({ _id: _id }, data),
deleteIssue: (_id) => Issue.deleteOne({ _id: _id }),
};
module.exports = crud;