2020年JavaScript+Firestore教程:通过示例学习

更新时间:2023-06-22 21:58:43 阅读: 评论:0

2020年JavaScript+Firestore教程:通过⽰例学习Cloud Firestore is a blazing-fast, rverless NoSQL databa, perfect for powering web and mobile apps of any size. , created to show you how to u Firestore as the engine for your own amazing projects from front to back.
Cloud Firestore是⼀个快速,⽆服务器的NoSQL数据库,⾮常适合为各种规模的Web和移动应⽤程序提供⽀持。 向您展⽰如何将Firestore⽤作⾃⼰从头到尾惊⼈项⽬的引擎。
⽬录 (Table of Contents)
Getting Started with Firestore
Firestore⼊门
What is Firestore? Why Should You U It?
什么是Firestore? 为什么要使⽤它?
Setting Up Firestore in a JavaScript Project
孕吐吐血怎么回事在JavaScript项⽬中设置Firestore
Firestore Documents and Collections
消防站⽂件和收藏
Managing our Databa with the Fireba Console
使⽤Fireba控制台管理我们的数据库
Fetching Data with Firestore
使⽤Firestore提取数据
Getting Data from a Collection with .get()
使⽤.get()从集合中获取数据
Subscribing to a Collection with .onSnapshot()
⽤.onSnapshot()订阅集合
Difference between .get() and .onSnapshot()
.
get()和.onSnapshot()之间的区别
Unsubscribing from a collection
取消订阅收藏
Getting individual documents
获取个⼈⽂件
Changing Data with Firestore
使⽤Firestore更改数据
Adding document to a collection with .add()
使⽤.add()将⽂档添加到集合中
Adding a document to a collection with .t()
使⽤.t()将⽂档添加到集合中
Updating existing data
更新现有数据
Deleting data
删除资料
Esntial Patterns
基本模式
Working with subcollections
处理⼦集合
Uful methods for Firestore fields
Firestore字段的有⽤⽅法
Querying with .where()
⽤.where()查询
思维体操Ordering and limiting data
订购和限制数据
.
什么是Firestore?为什么要使⽤它? (What is Firestore? Why Should You U It?)
Firestore is a very flexible, easy to u databa for mobile, web and rver development. If you're familiar with Fireba's realtime databa, Firestore has many similarities, but with a different (arguably more declarative) API.
Firestore是⼀个⾮常灵活,易于使⽤的数据库,⽤于移动,Web和服务器开发。 如果您熟悉Fireba的实时数据库,则Firestore具有许多相似之处,但是具有不同的(可能更具声明性)API。
Here are some of the features that Firestore brings to the table:
以下是Firestore带来的⼀些功能:
⚡ 轻松实时获取数据 (⚡ Easily get data in realtime )
Like the Fireba realtime databa, Firestore provides uful methods such as .onSnapshot() which make it a breeze to listen for updates to your data in real time. It makes Firestore an ideal choice for projects that place a premium on
displaying and using the most recent data (chat applications, for instance).
与Fireba实时数据库⼀样,Firestore提供了有⽤的⽅法,例如.onSnapshot(),使您可以轻松地实时侦听数据更新。 对于那些注重显⽰和使⽤最新数据(例如聊天应⽤程序)的项⽬,Firestore使其成为理想的选择。
作为NoSQL数据库的灵活性 (  Flexibility as a NoSQL Databa)
Firestore is a very flexible option for a backend becau it is a NoSQL databa. NoSQL means that the data isn't stored in tables and columns as a standard SQL databa would be. It is structured like a key-value store, as if it was one big JavaScript object.
Firestore是后端的⼀种⾮常灵活的选择,因为它是NoSQL数据库。 NoSQL意味着数据不像标准SQL数据库那样存储在表和列中。 它的结构类似于键值存储,就好像它是⼀个⼤型JavaScript对象⼀样。
In other words, there's no schema or need to describe what data  our databa will store. As long as we provide valid keys and values, Firestore will store it.
换句话说,没有架构,也不需要描述数据库将存储什么数据。 只要我们提供有效的键和值,Firestore就会存储它。
↕ 轻松扩展 (↕  Effortlessly scalable)
One great benefit of choosing Firestore for your databa is the very powerful infrastructure that it builds upon that enables you to scale your application very easily. Both vertically and horizontally. No matter whether you have hundreds or millions of urs. Google's rvers will be able to handle whatever load you place upon it.
为数据库选择Firestore的⼀⼤好处是,它基于强⼤的基础架构,使您可以轻松扩展应⽤程序。 垂直和⽔平⽅向。 ⽆论您有成百上千万的⽤户。 Google的服务器将能够处理您承受的任何负载。
In short, Firestore is a great option for applications both small and large. For small applications it's p
owerful becau we can do a lot without much tup and create projects very quickly with them. Firestore is well-suited for large projects due to it's scalability.
简⽽⾔之,Firestore是⼩型和⼤型应⽤程序的绝佳选择。 对于⼩型应⽤程序来说,它之所以强⼤是因为我们⽆需太多设置即可完成很多⼯作,并可以很快地创建项⽬。 Firestore具有可伸缩性,因此⾮常适合⼤型项⽬。
关于努力的名人名言
在JavaScript项⽬中设置Firestore (Setting Up Firestore in a JavaScript Project)
We're going to be using the Firestore SDK for JavaScript. Throughout this cheatsheet, we'll cover how to u Firestore within the context of a JavaScript project. In spite of this, the concepts we'll cover here are easily transferable to any of the available Firestore client libraries.
我们将使⽤Firestore SDK for JavaScript。 在整个备忘单中,我们将介绍如何在JavaScript项⽬的上下⽂中使⽤Firestore。 尽管如此,我们将在此处介绍的概念很容易转移到任何可⽤的Firestore客户端库中。
To get started with Firestore, we'll head to the Fireba console. You can visit that by going to . You'll need to have a Google account to sign in.
万象什么新
要开始使⽤Firestore,我们将转到Fireba控制台。 您可以访问进⾏访问。 您需要拥有⼀个Google帐户才能登录。
Once we're signed in, we'll create a new project and give it a name.
登录后,我们将创建⼀个新项⽬并为其命名。
Once our project is created, we'll lect it. After that, on our project's dashboard, we'll lect the code button.
创建我们的项⽬后,我们将选择它。 之后,在项⽬的仪表板上,我们将选择代码按钮。
This will give us the code we need to integrate Firestore with our JavaScript project.
这将为我们提供将Firestore与我们JavaScript项⽬集成所需的代码。
仔细的仔怎么组词Usually if you're tting this up in any sort of JavaScript application, you'll want to put this in a dedicated file called fireba.js. If you're using any JavaScript library that has a package.json file, you'll want to install the Fireba dependency with npm or yarn.
通常,如果您要在任何类型JavaScript应⽤程序中进⾏设置,都需要将其放在⼀个名为fireba.js的专⽤⽂件中。 如果您使⽤任何具有package.json⽂件JavaScript库,则需要使⽤npm或yarn安装Fireba依赖项。
// with npm
npm i fireba
// with yarn
yarn add fireba
Firestore can be ud either on the client or rver. If you are using Firestore with Node, you'll need to u the CommonJS syntax with require. Otherwi, if you're using JavaScript in the client, you'll import fireba using ES Modules.
Firestore可以在客户端或服务器上使⽤。 如果您将Firestore与Node⼀起使⽤,则需要将CommonJS语法与require⼀起使⽤。 否则,如果您在客户端中使⽤JavaScript,则将使⽤ES模块导⼊fireba。
// with Commonjs syntax (if using Node)
const fireba = require("fireba/app");
require("fireba/firestore");
// with ES Modules (if using client-side JS, like React)
import fireba from 'fireba/app';
import 'fireba/firestore';一帆风顺花
var firebaConfig = {
apiKey: "AIzaSyDpLmM79mUqbMDBexFtOQOkSl0glxCW_ds",
authDomain: "",
databaURL: "",
projectId: "lfasdlkjkjlkjl",
storageBucket: "",
messagingSenderId: "616270824980",
appId: "1:616270824990:web:40c8b177c6b9729cb5110f",
};
// Initialize Fireba
fireba.initializeApp(firebaConfig);
Firestore的收藏和⽂件 (Firestore Collections and Documents)
There are two key terms that are esntial to understanding how to work with Firestore: documents and collections.
机制建设
党锢之争对于了解如何使⽤Firestore⽽⾔,有两个必不可少的关键术语: ⽂档和集合 。
Documents are individual pieces of data in our databa. You can think of documents to be much like simple JavaScript objects. They consist of key-value pairs, which we refer to as fields. The values of the fields can be strings, numbers, Booleans, objects, arrays, and even binary data.
⽂件是我们资料库中的个别资料。 您可以认为⽂档⾮常像简单JavaScript对象。 它们由键值对组成,我们称其为字段 。 这些字段的值可以是字符串,数字,布尔值,对象,数组,甚⾄⼆进制数据。
document -> { key: value }
Sets of the documents of the documents are known as collections. Collections are very much like arrays of objects. Within a collection, each document is linked to a given identifier (id).
这些⽂档的这些⽂档集称为集合。 集合⾮常类似于对象数组。 在集合中,每个⽂档都链接到给定的标识符(id)。
collection -> [{ id: doc }, { id: doc }]
使⽤Firestore控制台管理我们的数据库 (Managing our databa with the Firestore Console)
Before we can actually start working with our databa we need to create it.
在实际开始使⽤数据库之前,我们需要创建数据库。
Within our Fireba console, go to the 'Databa' tab and create your Firestore databa.
在我们的Fireba控制台中,转到“数据库”选项卡并创建您的Firestore数据库。
Once you've done that, we will start in test mode and enable all reads and writes to our databa. In other words, we will have open access to get and change data in our databa. If we were to add Fireba authentication, we could restrict access only to authenticated urs.
完成此操作后,我们将以测试模式启动,并启⽤对数据库的所有读取和写⼊操作。 换句话说,我们将具有获取和更改数据库中数据的开放权限。 如果要添加Fireba⾝份验证,则只能将访问权限限制为已⾝份验证的⽤户。
After that, we'll be taken to our databa itlf, where we can start creating collections and documents. The root of our databa will be a ries of collections, so let's make our first collection.
之后,我们将进⼊数据库本⾝,在这⾥我们可以开始创建集合和⽂档。 数据库的根源将是⼀系列集合,因此让我们建⽴第⼀个集合。
We can lect 'Start collection' and give it an id. Every collection is going to have an id or a name. For our project, we're going to keep track of our urs' favorite books. We'll give our first collection the id 'books'.
我们可以选择“开始收集”并为其指定⼀个ID。 每个集合都有⼀个ID或名称。 对于我们的项⽬,我们将跟踪⽤户最喜欢的书。 我们将为我们的第⼀个收藏集赋予id“ books”。
Next, we'll add our first document with our newly-created 'books' collection.
接下来,我们将在第⼀个⽂档中添加新创建的“ books”集合。
Each document is going to have an id as well, linking it to the collection in which it exists.
每个⽂档也将具有⼀个ID,并将其链接到它所在的集合。
In most cas we're going to u an  option to give it an automatically generated ID. So we can hit the button 'auto id' to do so, after which we need to provide a field, give it a type, as well as a value.
在⼤多数情况下,我们将使⽤选项为它提供⼀个⾃动⽣成的ID。 因此,我们可以点击“⾃动ID”按钮,然后我们需要提供⼀个字段,为其指定类型和值。
For our first book, we'll make a 'title' field of type 'string', with the value 'The Great Gatsby', and hit save.
对于我们的第⼀本书,我们将创建⼀个'string'类型的'title'字段,其值为'The Great Gatsby',然后点击保存。
After that, we should e our first item in our databa.
之后,我们应该在数据库中看到第⼀个项⽬。
使⽤.get()从集合中获取数据 (Getting data from a collection with .get())
To get access Firestore u all of the methods it provides, we u fireba.firestore(). This method need to be executed every time we want to interact with our Firestore databa.
要使⽤Firestore提供的所有⽅法来访问Firestore,我们使⽤fireba.firestore() 。 每当我们想与Firestore数据库进⾏交互时,都需要执⾏此⽅法。
I would recommend creating a dedicated variable to store a single reference to Firestore. Doing so helps to cut down on the amount of code you write across your app.
我建议创建⼀个专⽤变量来存储对Firestore的单个引⽤。 这样做有助于减少您在整个应⽤程序中编写的代码量。
const db = fireba.firestore();
In this cheatsheet, however, I'm going to stick to using the firestore method each time to be as clear as possible.
但是,在本备忘单中,我将每次都尽量使⽤firestore⽅法,以使其尽可能清晰。
To reference a collection, we u the .collection() method and provide a collection's id as an argument. To get a reference to the books collection we created, just pass in the string 'books'.
要引⽤⼀个集合,我们使⽤.collection()⽅法并提供⼀个集合的id作为参数。 要获得对我们创建的books集合的引⽤,只需传递字符串“books”。

本文发布于:2023-06-22 21:58:43,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/1050408.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:数据库   需要   集合   数据   程序   创建
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图