Skip to content

Instantly share code, notes, and snippets.

@dydx
Last active January 28, 2016 21:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dydx/f3c5413b59a9ec87df6d to your computer and use it in GitHub Desktop.
Save dydx/f3c5413b59a9ec87df6d to your computer and use it in GitHub Desktop.
Opened mongoose connection
Successfully created and inserted new records
---------
Black 2016 BMW 135is
Silver 2016 BMW 335is
Red 2016 BMW 535is
Alpine White 2016 BMW M6
---------
UPDATED:
Japan Red 2016 BMW M6
---------
All done!
Closed mongoose connection
'use strict';
var mongoose = require('mongoose');
mongoose.Promise = global.Promise;
var db = require('./db');
var Car = require('./car');
mongoose.connect('mongodb://localhost/cars');
function quit () {
mongoose.disconnect();
console.log('All done!');
}
// helper methods for our promises to call
const separator = () => {
console.log('---------');
};
const createCars = () => {
// `cars` here comes from our seed data
return Car.create(cars);
};
const successMessage = res => {
console.log('Successfully created and inserted new records');
separator();
return res;
};
const handleErrors = err => {
console.log('ERROR: ', err);
separator();
return err;
};
// our seed data
let cars = [
{make: 'BMW', model: '135is', year: 2016, color: 'Black'},
{make: 'BMW', model: '335is', year: 2016, color: 'Silver'},
{make: 'BMW', model: '535is', year: 2016, color: 'Red'},
{make: 'BMW', model: 'M6', year: 2016, color: 'Alpine White'},
];
// we need to bulk insert the records for all of these cars
Car.remove({}) // remove all previous records in the Cars collection
.then(createCars) // run our function for creating cars
.then(successMessage) // handle the success message?
.then((res) => { // iterate over our result set and print out the details
res.map(car => car.print());
})
.then(() => { // start an anonymous function for handling the update
let updates = { color: 'Japan Red' };
let options = { new: true };
// find a record and edit it, then return that record
return Car.findOneAndUpdate({model: 'M6'}, updates, options);
})
.then((res) => { // we are accepting the return of the last `then`
separator(); // this just prints out `---------` between actions
console.log('UPDATED: '); // more notifications about what happened
res.print(); // print details of edited record
separator(); // some more lines
return res; // return the result of all of this
})
.catch(handleErrors); // handle any errors that might have happened
setTimeout(function () {
quit();
}, 2000);
@dydx
Copy link
Author

dydx commented Jan 28, 2016

I don't think you'd normally do something like this, at least not with a web app-- insertions, edits, and deletes would be handled in separate controller actions and whatnot. But its still a neat exercise in passing around promises and return values.

@drmikeh
Copy link

drmikeh commented Jan 28, 2016

Very nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment