node.js – Creating an atomic function in nodejs

The title is not accurate because, according to what I have found in my research, there seems to be no way to do an atomic function in nodejs, but I will expose my problem to see if you can find something that I could not think of.

I am trying to set up a programmer where I can set my appointment time intervals, say 1 hour each and when someone makes an appointment, I want to make sure that the time interval is not taken before scheduling it.

So, for example, I decide that I will work from 9 am to 2 pm with a schedule of one hour. So my schedule would be 9-10, 10-11, 11-12, 12-1, 1-2.

An appointment will arrive with a start time of 11 and an end time of 12. I need to make sure the space is not occupied.

I am using mongodb with nodejs and restify.

I understand that in my collection of quotes I can set an index on a combination of values ​​such as start time and end time, as described here Creating indexes of multiple fields in Mongoose / MongoDB.

But if I decide to change my time interval from 1 hour to 1.5 hours, then I will have scheduling conflicts since the start time and end time of the entries in the database will not match the new interval

I currently have a function that verifies to make sure that the new appointment will not conflict, but I am not sure if it will work well when several requests arrive. This is a nodejs and restify application, so basically an API with a mongodb with whom you speak, to handle appointments.

I am executing it with several workers, so I am concerned that at one time two applications arrive at the same time, handled by two different workers for the same time interval. When my conflict check function is executed, it will return saying that the space is open for both, since an appointment has not yet been made and then there will be a scheduling conflict.

Any ideas on how to combat this, or is there something in the way that JavaScript runs so you don't have to worry about this? All input will be appreciated

Thank you!