使用Node js和Cloud Firestore确定性数据库播种

Did you notice that «testing» was mentioned before «development»?! 🤓

您是否注意到在“开发”之前提到了“测试”? 🤓

TL; DR (TL;DR)

The main goal of this post is to motivate developers to create and maintain programs to seed their databases accordingly to both the common use cases of the user journey and the edge cases.

这篇文章的主要目的是激发开发人员创建和维护程序,以根据用户旅程的常见用例和边缘情况来播种其数据库。

Deterministic Seeding allows you to pre-fill a database with fake but understandable data. No more { "firstName": "qqqq", "lastName": "wwww", "email": "qqqq.wwww@eeee.rt" }. You will have a reproducible account list with whatever you want.

确定性播种使您可以用伪造但可理解的数据预填充数据库。 不再使用{ "firstName": "qqqq", "lastName": "wwww", "email": "qqqq.wwww@eeee.rt" } 。 您将拥有可复制的帐户列表,其中包含您想要的任何内容。

You will (re-)discover that proper database seeding is also a powerful tool for end-to-end automatic testing because it allows you to run almost the same test cases that you could run manually.

您将(重新)发现正确的数据库种子也是端到端自动测试的强大工具,因为它允许您运行几乎可以手动运行的测试用例。

Almost all popular backend frameworks have a seeding system.In this tutorial, you will learn how to build one on your own. But if you don’t want to, you can skip the abstract seeder part and focus on the specialized seeders.

几乎所有流行的后端框架都有种子系统。在本教程中,您将学习如何自行构建。 但是,如果您不想这样做,则可以跳过抽象的播种器部分,而专注于专门的播种器。

不要再浪费时间了 (Don’t lose time anymore)

I’ve been in this situation in a lot of projects:

我在很多项目中都遇到过这种情况:

I’ve followed a succinct “README” file to install and launch the project. I open the homepage, create a new account and try to reproduce the issue written in the bug ticket. But, I can’t reproduce it because it depends on data which are complicated to obtain manually.

我遵循了一个简洁的“ README”文件来安装和启动该项目。 我打开主页,创建一个新帐户,然后尝试重现bug凭单中写的问题。 但是,我无法重现它,因为它取决于手动获取复杂的数据。

So I ask a colleague: “Can you send me a dump of your database, please? I can’t reproduce the context and you have a lot more data than me…”. 🤦‍♂️

因此,我问一个同事: “能否请您向我发送数据库的转储? 我无法复制上下文,并且您的数据比我多得多…… ”。 ♂‍♂️

什么是确定性播种? (What is a Deterministic Seeding?)

Seeding is the action of pre-filling a database with data that match the defined type. It’s commonly used to store all the attending enumeration values for both backend and frontend. 🦩

播种是使用与定义的类型匹配的数据预填充数据库的操作。 它通常用于存储后端和前端的所有出席枚举值。 🦩

Here we will talk about a programmatic and deterministic approach which allows us to reproduce the same dataset anywhere without hard-coding it. For this, we will use a library that generates human-readable data using a seed key. So, if multiple processes (local or not) use the same key, they will obtain the same dataset. This key is the only value that should be manually defined.

在这里,我们将讨论一种程序确定性的方法,该方法使我们可以在任何地方重现相同的数据集而无需对其进行硬编码。 为此,我们将使用一个使用种子密钥生成人类可读数据的库。 因此,如果多个进程(无论是否在本地)都使用相同的密钥,则它们将获得相同的数据集。 该键是应该手动定义的唯一值。

使您的API前端驱动开发 (Make your API frontend-driven developed)

Did somebody say GraphQL?! 🤩

有人说过GraphQL吗? 🤩

By doing such thing as Deterministic Seeding you will make your API more frontend-oriented because your data will reflect real use cases and allows you to facilitate your tests according to the User Stories you have.

通过执行确定性播种之类的操作,您将使您的API更加面向前端,因为您的数据将反映实际用例,并允许您根据所拥有的用户故事来方便进行测试。

If you use GraphQL, for instance, you will create as many data as necessary to cover the schema specifications.

例如,如果使用GraphQL,则将创建所需的尽可能多的数据以覆盖架构规范。

模式示例 (Schema example)

This is the GraphQL schema we will use during the example implementation.

这是在示例实现期间将使用的GraphQL模式。

src/schema.graphql
src / schema.graphql

As you can see, you have required, optional and enum fields.

如您所见,您具有必填,可选和枚举字段。

In that case, you have to cover the most cases you can, keeping in mind the realistic aspect of your data.It will probably be useless to add a User without description and another without a job. A single User without both of these fields could be enough. It depends on your frontend specifications.

在这种情况下,您必须考虑到数据的实际情况,并尽可能地覆盖所有情况。添加没有说明的用户和没有工作的其他用户可能没有用。 没有这两个字段的单个用户就足够了。 这取决于您的前端规格。

生成的帖子示例 (Generated Post example)

According to the previous schema, a generated Post could be:

根据先前的架构,生成的Post可以是:

The only non-predictable values are the id and the author because they are both Firestore IDs.

唯一不可预测的值是idauthor因为它们都是Firestore ID。

And of course, you can use your seed program to seed all your enum tables/collections in production and development environments. Here: postTypes. It is not necessary in our example because we use a NoSQL database.

当然,您可以使用种子程序在生产和开发环境中为所有枚举表/集合添加种子。 此处: postTypes 。 在我们的示例中没有必要,因为我们使用NoSQL数据库。

有关测试的更多信息 (A bit more about Testing)

As well as you should create unit tests to cover newly founded bugs, you also should create new deterministic data that allow you to manually and automatically reproduce the issue.

除了应创建单元测试以覆盖新发现的错误外,还应创建新的确定性数据,以使您能够手动和自动重现问题。

Get into this habit and your application will be less error-prone.

养成这种习惯,您的应用程序将更不会出错。

实作 (Implementation)

We will implement our seeding program for a Cloud Firestore database using Chance and Firebase Admin in Node.js.

我们将使用Node.js中的ChanceFirebase Admin为Cloud Firestore数据库实现播种程序。

In this implementation, we won’t directly use the Firestore library. We will use a Model’s facade to avoid dealing with the Firestore verbosity.

在此实现中,我们将不直接使用Firestore库。 我们将使用模型的外观来避免处理Firestore的冗长性

You can retrieve the entire example at the following address: https://github.com/jeandesravines/medium/tree/master/df062eef387f-deterministic-database-seeding 🔗

您可以在以下地址检索整个示例: https: //github.com/jeandesravines/medium/tree/master/df062eef387f-deterministic-database-seeding🔗

播种者 (The Seeders)

src/seeders/index.js
src / seeders / index.js

This is the ordered list of seeders. The order ensures that weak entities will be seeded before the strong.

这是播种机的有序列表。 该命令确保了弱实体将在强实体之前被播种。

抽象种子 (The AbstractSeeder)

It is the abstract class used to create the Chance.js instance and define the structure of each Seeder.

它是用于创建Chance.js实例并定义每个Seeder结构的抽象类。

src/seeders/Seeder.ts
src / seeders / Seeder.ts

后裔 (The PostSeeder)

The PostSeeder will create 1 Post of each PostType for each User.There are 3 types. So, in the end, there will be 60 Posts because the UserSeeder has added 20 Users.

PostSeeder将为每个用户为每个PostType创建1个帖子。共有3种类型。 因此,最后,由于UserSeeder已添加20个用户,因此将有60个帖子。

src/seeders/PostSeeder.ts
src / seeders / PostSeeder.ts

剧本 (The Script)

In its seed function, the script will sequentially call the clean method of each seeder before their run method. This will ensure that weak entities will be created before strong ones.

在其seed函数中,脚本将在其run方法之前顺序调用每个种子的clean方法。 这将确保在创建强实体之前先创建弱实体。

src/scripts/seed.ts
src / scripts / seed.ts

A condition prevents this script to be run in the production environment.

条件导致此脚本无法在生产环境中运行。

(Run)

According to the package.json’s scripts, you can now run the following command to start the seeding procedure:

根据package.json的脚本,您现在可以运行以下命令以启动播种过程:

yarn seed

That’s it!

而已!

You can re-run this command and obtain the same result.

您可以重新运行此命令并获得相同的结果。

让我们播种! ‍💻 (Let’s Seed! 👨‍💻)

And test, of course!

和测试,当然!

翻译自: https://medium.com/@jeandesravines/deterministic-database-seeding-using-node-js-and-cloud-firestore-df062eef387f

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值