Skip to content

Commit

Permalink
added callback functions
Browse files Browse the repository at this point in the history
  • Loading branch information
arvention committed Apr 2, 2020
1 parent b5c76a7 commit 1bbbedb
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions models/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ const database = {
/*
inserts a single `doc` to the database based on the model `model`
*/
insertOne: function(model, doc) {
insertOne: function(model, doc, callback) {
model.create(doc, function(error, result) {
if(error) throw error;
if(error) return callback(false);
console.log('Added ' + result);
return callback(true);
});
},

Expand All @@ -42,8 +43,9 @@ const database = {
*/
insertMany: function(model, docs) {
model.insertMany(docs, function(error, result) {
if(error) throw error;
if(error) return callback(false);
console.log('Added ' + result);
return callback(true);
});
},

Expand All @@ -55,7 +57,7 @@ const database = {
*/
findOne: function(model, query, projection, callback) {
model.findOne(query, projection, function(error, result) {
if(error) throw error;
if(error) return callback(false);
return callback(result);
});
},
Expand All @@ -68,7 +70,7 @@ const database = {
*/
findMany: function(model, query, projection, callback) {
model.find(query, projection, function(error, result) {
if(error) throw error;
if(error) return callback(false);
return callback(result);
});
},
Expand All @@ -80,8 +82,9 @@ const database = {
*/
updateOne: function(model, filter, update) {
model.updateOne(filter, update, function(error, result) {
if(error) throw error;
if(error) return callback(false);
console.log('Document modified: ' + result.nModified);
return callback(true);
});
},

Expand All @@ -92,8 +95,9 @@ const database = {
*/
updateMany: function(model, filter, update) {
model.updateMany(filter, update, function(error, result) {
if(error) throw error;
if(error) return callback(false);
console.log('Documents modified: ' + result.nModified);
return callback(true);
});
},

Expand All @@ -103,8 +107,9 @@ const database = {
*/
deleteOne: function(model, conditions) {
model.deleteOne(conditions, function (error, result) {
if(error) throw error;
if(error) return callback(false);
console.log('Document deleted: ' + result.deletedCount);
return callback(true);
});
},

Expand All @@ -114,8 +119,9 @@ const database = {
*/
deleteMany: function(model, conditions) {
model.deleteMany(conditions, function (error, result) {
if(error) throw error;
if(error) return callback(false);
console.log('Document deleted: ' + result.deletedCount);
return callback(true);
});
}

Expand Down

0 comments on commit 1bbbedb

Please sign in to comment.