-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo.js
68 lines (57 loc) · 1.28 KB
/
mongo.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
56
57
58
59
60
61
62
63
const mongoose = require('mongoose')
// korvaa url oman tietokantasi urlilla. ethän laita salasanaa Githubiin!
const url = process.env.MONGODB_URI
mongoose.connect(url)
const Person = mongoose.model('Person', {
name: String,
number: String
})
const showPersons = () => {
Person
.find({})
.then(result => {
console.log('puhelinluettelo: ')
result.forEach(person => {
console.log(person.name + ' ' + person.number)
})
mongoose.connection.close()
})
}
const deletePerson = (id) => {
Person.findByIdAndDelete(id)
.then(response => {
console.log(response)
mongoose.connection.close()
})
}
const savePerson = (name, number) => {
const person = new Person({
name: name,
number: number
})
person
.save()
.then(response => {
console.log('lisätään henkilö ' + person.name + ' numero ' + person.number + ' luetteloon')
mongoose.connection.close()
})
}
const findPersonById = (id) => {
Person
.findById(id)
.then(person => {
if(person) {
console.log(person)
} else {
console.log('somethings fucked now')
}
})
}
switch(process.argv.length){
case 2: showPersons()
break;
case 3: findPersonById(process.argv[2])
break;
case 4: savePerson(process.argv[2],process.argv[3])
break;
}