Firestore:使用预订应用程序处理并发 [英] Firestore: handling concurrency with booking app

查看:55
本文介绍了Firestore:使用预订应用程序处理并发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个 bookings 和一个 bookable 集合. bookings 中的每个文档都有一个日期范围( check-out check-in )和一个对 bookable 文档.

I've currently got a bookings and a bookable collection. Each document in bookings holds a date range (check-out and check-in) and an array of references to bookable documents.

我对于如何保证两个 bookings 对于同一个 bookables 不会同时编写而感到困惑.据我了解,我不能从技术上锁定(例如通过事务处理)收集的集合,所以我想知道我的选择是什么(也许重新构造了存储数据的方式,等等).

I'm a bit stumped at how to guarantee two overlapping bookings for the same bookables aren't written at the same time. From what I understand I can't technically lock a collection via something like a transaction, so I'm wondering what my options are (perhaps restructuring how I'm storing data, etc).

任何指针或建议将不胜感激.

Any pointers or advice would be much appreciated.

用户A 要预订与用户B 相同的两个项目,并且预订相同的时间范围.他们俩都在大约同一时间加载了预订UI,并确认了他们的选择.

Say User A wants to make a booking for the same two items as User B does and for the same time range. They both load the booking UI at around the same time and confirm their selection.

在为每个请求的 bookings 集合内创建一个新文档之前,该应用程序将执行get查询以检查是否有任何重叠,如果不存在任何重叠,则插入新的预订文档.在应用检查 booking 集合中的重叠与创建新文档之间的那部分时间似乎为不一致打开了一个窗口(例如,可能允许两个重叠时间范围和项目的文档被创建).

Prior to creating a new document inside the bookings collection for each of their requests, the app would perform a get query to check for any overlaps and if none exist insert the new booking documents. That fraction of time between the app's check for overlaps across the booking collection and the creation of new documents is what seems to open up a window for inconsistencies (e.g. potentially allowing two documents with overlapping time ranges and items to be created).

交易是否可以防止新文档写入集合中,因为该文档中是否存在符合特定条件的其他文档?

Could a transaction help prevent a new document being written to a collection based the existance of other documents in that collection that fit a specific criteria?

推荐答案

为防止用户意外覆盖彼此的数据,您需要

To prevent users from accidentally overwriting each other's data, you'll want to use a transaction.

为防止用户有意覆盖彼此的数据,您需要使用安全规则.这样做的关键是将要唯一的信息用作文档的ID.

To prevent users from intentionally overwriting each other's data, you'll want to use security rules. Key to this is to use the information that you want to be unique as the ID of the documents.

因此,假设您通过日期和开始时间来标识时隙,则可以具有文档ID "20210420T0900&" .如果用户正在尝试对该文档进行写操作,则可以在数据库的安全规则中拒绝该写操作.

So say you identify time slots by the date and start time, you could have a document ID "20210420T0900". If a user is trying to write to that document when it already exists, you can reject that write in the security rules of your database.

这篇关于Firestore:使用预订应用程序处理并发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆