Last active
June 16, 2021 19:29
-
-
Save katowulf/5b9a0d9f0489873e9b6806ab19c8359a to your computer and use it in GitHub Desktop.
Convert Cloud Storage json file to event stream and store output in Firestore
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
JSONStream = require('JSONStream'); | |
es = require('event-stream'); | |
fileStream = storage.bucket('your-bucket').file('your-JSON-file').createReadStream(); | |
db = admin.firestore(); | |
return new Promise( (resolve, reject) => { | |
batchPromises = []; | |
batchSize = 0; | |
batch = db.batch(); | |
fileStream.pipe(JSONStream.parse(...) | |
.pipe(es.map(function (data) { | |
// transform the data here | |
}) | |
.pipe(menuItem => { | |
ref = <get a ref>; | |
batch.set( ref, menuItem ); | |
batchSize++; | |
if( batchSize === 500 ) { | |
batchPromises.push( batch.commit() ); | |
batch = db.batch(); | |
batchSize = 0; | |
} | |
}); | |
.on("end", () => { | |
if( batchSize > 0 ) { | |
batchPromises.push( batch.commit() ); | |
} | |
Promise.all(batchPromises).then(resolve).catch(reject) | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment