Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

148
Views
Prevent Map Update if Key Exists

I'm trying to create a backend in firestore to manage availability. I have an employeeAvailability collection with documents for each employee that each contain a map where the keys are start times of the appointment booked for that employeee. The data is used to generate timeslots on the client side that a user can select to book (an appointment means the timeslot is unavailable).

employee12345 {
    appointments = { "10:00 AM" : true,
                     "11:00 AM" : true,
                     "12:00 PM" : true,
                     "1:00 PM" : true, 
                     "2:00 PM" : true
    }
}

I wanted to create a firestore rule that prevents users from double booking an appointment. When the user tries to book a timeslot for 1:00 PM, in the above example, I'd need to do the following update.

db.collection("employeeAvailability").document("employee12345").updateDate([
     "appointments.1:00 PM": true
]) { err in
    if let err = err {
        print("Error updating document: \(err)")
    } else {
        print("Document successfully updated")
    }
}

I'd like this transaction to be rejected and the firebase err to say something along the lines of "This appointment slot is already booked".

Obviously, there are client side rules preventing people from choosing booked timeslots, but it's possible I could have users simultaneously trying to book the same open spot and I'd like the database to handle these race conditions by accepting the first write and rejecting the second.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I'd like the database to handle these race conditions by accepting the first write and rejecting the second.

You should then use a transaction, along the following lines:

  const db = firebase.firestore();

  const employeeDocRef = db
    .collection('employeeAvailability')
    .doc('employee12345');

  const timeSlot = '1:00 PM';

  db.runTransaction((transaction) => {
    return transaction.get(employeeDocRef).then((employeeDoc) => {
      const appointments = employeeDoc.get('appointments');

      if (appointments.hasOwnProperty(timeSlot)) {
        throw 'Slot already taken';
      }

      appointments[timeSlot] = true;
      transaction.update(employeeDocRef, { appointments });
    });
  })
    .then(() => {
      console.log('Transaction successfully committed!');
    })
    .catch((error) => {
      console.log('Transaction failed: ', error);
    });
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!