Skip to content

Instantly share code, notes, and snippets.

@bookercodes
Created October 19, 2016 10:30
Show Gist options
  • Save bookercodes/ba6458b7f94993e75ae677d215eefc15 to your computer and use it in GitHub Desktop.
Save bookercodes/ba6458b7f94993e75ae677d215eefc15 to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
require('dotenv').config()
const db = require('sequelize-context')
const axios = require('axios')
db
.connect(
'./db/models/*.js',
process.env.DB_SCHEMA,
process.env.DB_USERNAME,
process.env.DB_PASSWORD, {
})
.then(function () {
return db
.models
.talk
.findAll()
})
.then(function (talks) {
const promises = talks.map(function (talk) {
return new Promise(function (resolve, reject) {
axios
.get(`https://api.wistia.com/v1/stats/medias/${talk.wistiaMediaId}.json?api_password=${process.env.WISTIA_PASSWORD}`)
.then(response => {
return db
.models
.talk
.update({
views: response.data.play_count
}, {
where: {
id: talk.id
}
})
})
.then(resolve)
})
})
return Promise.all(promises)
})
.then(function () {
console.log('success')
})
.catch(function (error) {
console.error('error', error)
})
@topliceanu
Copy link

The problem here is that you might get partial updates :(

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