-
Notifications
You must be signed in to change notification settings - Fork 0
/
todo.js
58 lines (37 loc) · 1.26 KB
/
todo.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
const mongoose = require('mongoose');
mongoose.set('useFindAndModify', false);
const MONGO_URL = process.env.MONGODB_URI || 'mongodb://localhost:27017'
mongoose.connect(MONGO_URL, { useNewUrlParser: true });
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function(){
console.log('Connected to MongoDb!');
});
const todoSchema = new mongoose.Schema({
text: String,
done: Boolean
})
todoSchema.statics.createDocument = function(params, callback){
return Todo.create({ text: params.text, done: params.done },
function (err, todo) {
if (err) return handleError(err);
callback(todo);
}
)
}
todoSchema.statics.updateDocument = function(id, params, callback){
return Todo.findOneAndUpdate({ _id: id },
{
text: params.text,
done: params.done
}, function(err, todo){
callback(todo);
})
}
todoSchema.statics.all = function(callback){
return Todo.find({}, function(err, todos){
callback({ todos: todos });
})
}
const Todo = mongoose.model('Todo', todoSchema);
module.exports = Todo;