Last active
June 10, 2018 20:11
-
-
Save tonyspiro/326cec4254f2fb0a14504bf561687452 to your computer and use it in GitHub Desktop.
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
require('dotenv').config() | |
const Cosmic = require('cosmicjs') | |
const api = Cosmic() | |
const async = require('async') | |
const algoliasearch = require('algoliasearch') | |
const client = algoliasearch(process.env.ALGOLIA_ACCOUNT, process.env.ALGOLIA_INDEX) | |
const index = client.initIndex('Stories') | |
const buckets = ['your-bucket-slug','another-bucket-slug'] // Add all Bucket slugs here | |
async.eachSeries(buckets, (bucket_slug, bucketEachCallback) => { | |
const bucket = api.bucket({ slug: bucket_slug }) | |
addRecords() | |
let added_count = 0 | |
const addRecords = (skip = 0) => { | |
console.log(skip) | |
const locals = {} | |
async.series([ | |
callback => { | |
const objects = bucket.getObjects({ | |
type: 'posts', | |
limit: 1000, | |
skip | |
}).then(data => { | |
const objects = data.objects | |
locals.objects = objects | |
locals.total = data.total | |
console.log('Total:', data.total) | |
console.log('Object Length', locals.objects.length) | |
callback() | |
}).catch(err => { | |
console.log(err) | |
}) | |
}, | |
() => { | |
async.eachSeries(locals.objects, (object, eachCallback) => { | |
// Save Algolia record | |
delete object.content | |
index.addObject(object, (err, content) => { | |
console.log('objectID=' + content.objectID) | |
console.log('ADD', object.slug) | |
added_count++ | |
eachCallback() | |
}) | |
}, () => { | |
console.log('Added ' + added_count, 'Total:' + locals.total) | |
if (added_count !== locals.total) { | |
addRecords(added_count) | |
} else { | |
bucketEachCallback() | |
console.log('All done FOR REAL!') | |
} | |
}) | |
} | |
]) | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment