Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

delete option and getAll added #2

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Enhancement 02 - Save random locations
Akila Nonis committed Feb 16, 2017
commit d61761a3107c5c7bef66f52551f1f818dceb0ec9
37 changes: 36 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -24,7 +24,7 @@ db.once('open', () => {
break;
}
default: {
saveSimple();
saveLocation();
}
}
});
@@ -41,6 +41,14 @@ function flush() {
});
}

function saveLocation(){
if(process.env.BULK_SAVE > 0){
saveMultiple(process.env.BULK_SAVE);
}else{
saveSimple();
};
}

function saveSimple() {
var location = new Location({
timestamp: new Date().getTime(),
@@ -57,3 +65,30 @@ function saveSimple() {
process.exit(1);
});
}

function saveMultiple(count){

var locations = [];

// Creating locations with random coordinates and adding them to an array
for(var i=0; i<count; i++){
var location = new Location({
timestamp: new Date().getTime(),
latitude: Math.round((Math.random() * 90) * 100) / 100 ,
longitude: Math.round((Math.random() * 180) * 100) / 100,
});
locations[i] = location;
}

// Bulk inserting the array of locations into the DB
Location.collection.insert(locations)
.then(locations => {
console.log(locations);
process.exit(0);
})
.catch(err => {
console.error(err);
process.exit(1);
});;
}