Last active
August 29, 2015 14:13
-
-
Save konsumer/b1be89e9f9c182334991 to your computer and use it in GitHub Desktop.
Promise-based download EZ shows & upsert them into database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Promise = require('bluebird'); | |
var mongoose = require('mongoose'); | |
var ShowSchema = new mongoose.Schema({ | |
"id": Number, | |
"title": String, | |
"provider": {'type':String, 'default':'eztv'} | |
}); | |
/** | |
* Map function for EZTV-to-Show | |
* @param {Object} show EZTV show | |
* @return {Object} Mongoose Show object | |
*/ | |
ShowSchema.statics.mapEZ = function (show) { | |
return { | |
title: show.title, | |
id: Number(show.id), | |
provider: 'eztv' | |
}; | |
}; | |
var Show = mongoose.model('Show', ShowSchema); | |
/** | |
* Atomic connect Promise - not sure if I need this, might be in mongoose already.. | |
* @return {Priomise} | |
*/ | |
function connect(uri, options){ | |
return new Promise(function(resolve, reject){ | |
mongoose.connect(uri, options, function(err){ | |
if (err) return reject(err); | |
resolve(mongoose.connection); | |
}); | |
}); | |
} | |
/** | |
* Bulk-upsert an array of records | |
* @param {Array} records List of records to update | |
* @param {Model} Model Mongoose model to update | |
* @param {Object} match Database field to match | |
* @return {Promise} always resolves a BulkWriteResult | |
*/ | |
function save(records, Model, match){ | |
match = match || 'id'; | |
return new Promise(function(resolve, reject){ | |
var bulk = Model.collection.initializeUnorderedBulkOp(); | |
records.forEach(function(record){ | |
var query = {}; | |
query[match] = record[match]; | |
bulk.find(query).upsert().updateOne( record ); | |
}); | |
bulk.execute(function(err, bulkres){ | |
if (err) return reject(err); | |
resolve(bulkres); | |
}); | |
}); | |
} | |
var eztv = require('eztv'); | |
eztv.getShows({}, function(err, shows){ | |
if(err) return console.log('EZ Error:', err); | |
var shows = shows.map(Show.mapEZ); | |
console.log('found', shows.length, 'shows.'); | |
connect('mongodb://localhost/tv', {}).then(function(db){ | |
save(shows, Show).then(function(bulkRes){ | |
console.log('Bulk complete.', bulkRes); | |
db.close(); | |
}, function(err){ | |
console.log('Bulk Error:', err); | |
db.close(); | |
}); | |
}, function(err){ | |
console.log('DB Error:', err); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment