I'm currently developing an application with React and Mongoose (MongoDB) where I upload a text file, that contains roughly around 9000+ lines, containing JSON. Each line is important and I need to be able to store it in the database to be accessible later.
I have set up two functions, one that reads the file and one that creates a model for each request and saves it to MongoDB.
Function to read each line
const createAndReadFile = async (filePath) => {
//create a read stream of the File
let fileStream = fs.createReadStream(filePath, 'utf8');
const rl = readline.createInterface({
input: fileStream,
crlfDelay: Infinity
});
for await (const line of rl) {
let parsedJSONLine = JSON.parse(line);
createAndAddToDB(parsedJSONLine);
}
};
Function to add to the database each line
const createAndAddToDB = async (jsonRequest) => {
try {
let request = new Request(jsonRequest);
await request.save();
} catch (error) {}
};
My question is, what's the most effective way to wait until all the lines have been converted to a model and saved in the database ?