I'm practicing NodeJS and working on a small application that has an endpoint that stores json objects to text files and also has the ability to search for objects in files. I'm using a single file for that purpose, I know I should be using multiple files for many considerations, but lets consider the case of having a single storage text file.
As far as I understand, (Not 100% sure) that NodeJS is single threaded, and it is not possible that two simultaneous API calls would update the file at the same time since each process of them will be in a single separate thread.
So is my understanding correct? or there is a possiblitiy that a file gets updated simultaneously which will cause data integrity violation? and if yes, how to handle this ? Is there a way to lock a file until the process completes ?
Yes, it can happen. While NodeJS is single-threaded, I/O is not. That is one of its core tenets.
You could mitigate such problems by locking files before writing to them (the link is just one example how to do this).
Another approach would be to use SQLite. There's no database server set up or administer (like MySQL, for example). The entire database is contained in a single file, but it handles things such as locking in case of multiple writes, crashes while writing and so on.
"Think of SQLite not as a replacement for Oracle [a database server] but as a replacement for fopen() [working with plain files]"