-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
61 lines (54 loc) · 1.54 KB
/
index.js
File metadata and controls
61 lines (54 loc) · 1.54 KB
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
56
57
58
59
60
61
const mongoose=require('mongoose');
const Projects=require('./models/projectSchema.js');
const { info } = require('console');
const db=mongoose.connect('mongodb://localhost:27017/projectmanager', {
useNewUrlParser:true,
useUnifiedTopology:true
});
// Function: add project to db
const addProject = (project) => {
Projects.create(project).then(project => {
console.info('New Project Added');
mongoose.connection.close();
});
}
//Function: find project from db filtering by name
const findProject = (name) => {
const search = new RegExp(name, 'i');
Projects.find({project:search})
.then(project => {
console.info(project);
console.info(`${project.length} match(es) to the entered query`);
mongoose.connection.close();
});
}
//Function: update project by its _id
const updateProject =(_id,project)=>{
Projects.updateOne({_id:_id},project).then(()=>{
console.info('Project updated');
mongoose.connection.close();
})
}
//Function: delete project by its _id
const deleteProject=(_id)=>{
Projects.deleteOne({_id}).then((project)=>{
console.info(project)
console.info(`${project.deletedCount} Project(s) deleted`);
mongoose.connection.close();
})
}
//Function: list all the projects in db
const listProjects=()=>{
Projects.find().then((projects)=>{
console.info(projects);
console.info(`${projects.length} Projects Listed`);
mongoose.connection.close();
})
}
module.exports={
addProject,
findProject,
updateProject,
deleteProject,
listProjects,
}