I'm creating my first Node.js REST API test project, which retrieves prizes. The logic is meant to do the following:
- Retrieve a CSV from the Google Cloud Storage bucket associated with the project
- Parse the CSV
- Find the first row where the column "Claimed" isn't populated
- Update the "Claimed" column to "claimed!"
- Overwrite the data in the CSV file in Google Cloud Storage
- Return the prize name associated with that row
The logic I have is currently working locally, but I'm wondering if there is a better, more scalable way to write the code. I'll be testing it at a 500/min rate limit for around 100k users per day and would eventually pass an external user-id to be stored in the "Claimed" column. There will be a total of 500k rows (prizes) in the CSV.
The code I'm using is below. Any suggestions for making it scalable would be much appreciated! Thank you in advance.
const csv = require('csv-parser');
const fs = require('fs');
const jsonexport = require('jsonexport');
const bucketName = 'MY-BUCKET';
const filename = 'MY-CSV';
const {Storage} = require('@google-cloud/storage');
const storage = new Storage({keyFilename: "MY-KEY.json"});
const myBucket = storage.bucket(bucketName);
const file = myBucket.file(filename);
let dataArray = [];
file.createReadStream()
.pipe(csv())
.on('data', function (data) {
dataArray.push(data);
})
.on('end', function(){
let prize = dataArray.find(element => element.Claimed == "");
prizeName = prize.Prize_name;
prize.Claimed = "claimed!";
jsonexport(dataArray,function(err, transformedData){
if(err) return console.log(err);
file.save(transformedData, function(err) {
if(err) return console.log(err);
});
});
return prizeName
});