Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

146
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda