使用Firestore生成自动递增序列

After more than a year of reading and playing around on the surface with various aspects of Firebase it was time take the plunge and dive into using Firebase with my next project.

经过一年多的阅读和对Firebase各个方面的了解,现在是时候投入我的下一个项目,并开始使用Firebase了。

Firebase simplifies many of the initial starting hurdles when commencing a new project such as providing Authentication out of the box, Firestore’s CRUD operations are straight forward with its NoSQL database as a service plus many more features I’m still learning.

在开始新项目时,Firebase简化了许多初始的启动障碍,例如开箱即用地提供身份验证,Firestore的CRUD操作直接使用NoSQL数据库即服务以及我仍在学习中的许多其他功能。

How to generate your own sequence value?

如何生成自己的序列值

I know I’m possibly approaching this with traditional SQL database mindset, but who doesn’t want to be able to generate the next sequence value when creating an Order? The random sets of characters of the document id isn’t a user friendly order identifier when emailing a customer their latest purchase.

我知道我可能会以传统SQL数据库思维方式来实现这一目标,但是谁不想在创建Order时生成下一个序列值? 通过电子邮件向客户发送其最新购买信息时,文档ID的随机字符集不是用户友好的订单标识符。

Enter FieldValue.increment(), it allows for the incrementing or decrementing values atomically on an individual document with ease, according to the documentation and blog post here.

输入FieldValue.increment(),根据此处的文档和博客文章,它允许轻松地在单个文档上原子地递增或递减值。

How to read the incremented value and set this on another document?

如何读取增量值并将其设置在另一个文档上?

I hit a road block with FieldValue.increment(), it simply didn’t solve my use case and reading through the various features of the Firestore database there was no easy way to implement an auto-incrementing sequence value.

我用FieldValue.increment()遇到了障碍,它根本无法解决我的用例,并且通读Firestore数据库的各种功能,没有简单的方法来实现自动递增序列值。

This is where Firestore’s cloud functions and transaction come to the rescue. I was able to generate an auto-incrementing sequence, by storing a counter’s current value in a document and then splitting each counter into their own document, thus reducing locking contention.

这就是Firestore的云功能和交易得以拯救的地方。 通过将计数器的当前值存储在文档中,然后将每个计数器拆分为自己的文档,我能够生成一个自动递增序列,从而减少了锁定争用。

Let’s get started…

让我们开始吧…

First thing first, with Firestore cloud functions we need to initialise the instance of the Firestore admin SDK for globally. Here is a utility functions to ensure the admin SDK is only initialised once.

首先,使用Firestore云功能,我们需要为全球初始化Firestore admin SDK的实例。 这是一个实用程序功能,可确保仅将admin SDK初始化一次。

Next is implementing the Cloud function to listen to the creation of the Order documents to trigger the generation of the next order number and then updating the order itself.

接下来是实现Cloud函数,以侦听Order文档的创建以触发下一个订单编号的生成,然后更新订单本身。

Notice the usage of database runTransaction function, this is to ensure that the call to getIncrement locks the counter’s document while the reading the current value and then updating with the incremented value.

注意数据库runTransaction函数的用法,这是为了确保对getIncrement的调用在读取当前值并随后使用递增的值进行更新时锁定计数器的文档。

Note:- One nuance when working with transactions, Firebase requires all read operations need to happen before any write operations so order of execution is important.

注意:-与事务打交道时,Firebase要求所有读取操作必须在任何写入操作之前进行,因此执行顺序很重要。

Each named counter will have their own document in a collection reducing contention for an individual counter document, i.e. a counter document for orders and another for invoices.

每个命名柜台将在集合中拥有自己的文件,从而减少了对单个柜台文件的争用,即,针对订单的柜台文件和针对发票的柜台文件。

Image for post
Firestore Counters Collection
消防处柜台集合

The getIncrement() function could also be used via a Firebase Http Cloud function or a Firebase Callable function, if needing the sequence value prior to creating the document into the Firestore database.

如果在将文档创建到Firestore数据库之前需要序列值,则还可以通过Firebase Http Cloud函数或Firebase Callable函数使用getIncrement()函数。

结论 (Conclusion)

Firebase is truly an amazing database as a service allowing the developer to concentrate on delivering value without the need to build out an API layer to get started.

Firebase确实是一个了不起的数据库即服务,它使开发人员可以专注于交付价值,而无需构建API层即可上手。

A feature request extension for Firestore would be for FieldValue.increment to take a parameter for a counter name and all this additional plumbing code could be handled by Firebase itself.

Firestore的功能请求扩展名是FieldValue.increment,用于获取计数器名称的参数,并且所有这些其他管道代码都可以由Firebase本身处理。

翻译自: https://medium.com/swlh/generating-auto-incrementing-sequences-with-firestore-b51ab713c571

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值