JavaScript + Firestore-vejledningen til 2020: Lær ved eksempel

Cloud Firestore er en lynhurtig, serverløs NoSQL-database, perfekt til at drive web- og mobilapps i enhver størrelse. Tag den komplette guide til at lære Firestore, der er oprettet for at vise dig, hvordan du bruger Firestore som motor til dine egne fantastiske projekter fra front til bag.

Indholdsfortegnelse

Kom godt i gang med Firestore

  • Hvad er Firestore? Hvorfor skal du bruge det?
  • Opsætning af Firestore i et JavaScript-projekt
  • Firestore-dokumenter og samlinger
  • Administration af vores database med Firebase Console

Henter data med Firestore

  • Få data fra en samling med .get ()
  • Abonnere på en samling med .onSnapshot ()
  • Forskel mellem .get () og .onSnapshot ()
  • Afmelding fra en samling
  • Få individuelle dokumenter

Ændring af data med Firestore

  • Tilføjelse af dokument til en samling med .add ()
  • Tilføjelse af et dokument til en samling med .set ()
  • Opdatering af eksisterende data
  • Sletning af data

Væsentlige mønstre

  • Arbejde med undersamlinger
  • Nyttige metoder til Firestore-felter
  • Forespørgsel med .where ()
  • Bestilling og begrænsning af data

Bemærk: Du kan downloade en PDF-version af denne vejledning, så du kan læse den offline.

Hvad er Firestore? Hvorfor skal du bruge det?

Firestore er en meget fleksibel, brugervenlig database til mobil-, web- og serverudvikling. Hvis du er fortrolig med Firebases realtidsdatabase, har Firestore mange ligheder, men med en anden (uden tvivl mere erklærende) API.

Her er nogle af de funktioner, som Firestore bringer til bordet:

⚡️Hent nemt data i realtid

Ligesom Firebase-realtidsdatabasen giver Firestore nyttige metoder såsom .onSnapshot (), der gør det til en leg at lytte efter opdateringer til dine data i realtid. Det gør Firestore til et ideelt valg til projekter, der lægger en præmie på visning og brug af de nyeste data (f.eks. Chatapplikationer).

? Fleksibilitet som en NoSQL-database

Firestore er en meget fleksibel mulighed for en backend, fordi den er en NoSQL-database. NoSQL betyder, at dataene ikke er gemt i tabeller og kolonner, som en standard SQL-database ville være. Det er struktureret som en nøgleværdibutik, som om det var et stort JavaScript-objekt.

Med andre ord er der intet skema eller behov for at beskrive, hvilke data vores database vil gemme. Så længe vi leverer gyldige nøgler og værdier, gemmer Firestore det.

↕️ Ubesværet skalerbar

En stor fordel ved at vælge Firestore til din database er den meget kraftige infrastruktur, som den bygger på, der gør det muligt for dig at skalere din applikation meget let. Både lodret og vandret. Uanset om du har hundreder eller millioner af brugere. Googles servere er i stand til at håndtere den belastning, du lægger på den.

Kort sagt, Firestore er en god mulighed for applikationer både små og store. For små applikationer er det kraftigt, fordi vi kan gøre meget uden meget opsætning og oprette projekter meget hurtigt med dem. Firestore er velegnet til store projekter på grund af dets skalerbarhed.

Opsætning af Firestore i et JavaScript-projekt

Vi bruger Firestore SDK til JavaScript. I hele dette cheatsheet vil vi dække, hvordan du bruger Firestore inden for rammerne af et JavaScript-projekt. På trods af dette kan de begreber, vi dækker her, let overføres til et hvilket som helst af de tilgængelige Firestore-klientbiblioteker.

For at komme i gang med Firestore går vi til Firebase-konsollen. Du kan besøge det ved at gå til firebase.google.com. Du skal have en Google-konto for at logge ind.

Når vi er logget ind, opretter vi et nyt projekt og giver det et navn.

Når vores projekt er oprettet, vælger vi det. Derefter vælger vi kodeknappen på vores projekts dashboard.

Dette giver os den kode, vi har brug for for at integrere Firestore med vores JavaScript-projekt.

Normalt hvis du opsætter dette i nogen form for JavaScript-applikation, vil du gerne placere dette i en dedikeret fil kaldet firebase.js. Hvis du bruger et JavaScript-bibliotek, der har en package.json-fil, skal du installere Firebase-afhængigheden med npm eller garn.

// with npm npm i firebase // with yarn yarn add firebase

Firestore kan bruges enten på klienten eller serveren. Hvis du bruger Firestore med Node, skal du bruge CommonJS-syntaksen med kræver. Ellers, hvis du bruger JavaScript i klienten, importerer du firebase ved hjælp af ES-moduler.

// with Commonjs syntax (if using Node) const firebase = require("firebase/app"); require("firebase/firestore"); // with ES Modules (if using client-side JS, like React) import firebase from 'firebase/app'; import 'firebase/firestore'; var firebaseConfig = { apiKey: "AIzaSyDpLmM79mUqbMDBexFtOQOkSl0glxCW_ds", authDomain: "lfasdfkjkjlkjl.firebaseapp.com", databaseURL: "//lfasdlkjkjlkjl.firebaseio.com", projectId: "lfasdlkjkjlkjl", storageBucket: "lfasdlkjkjlkjl.appspot.com", messagingSenderId: "616270824980", appId: "1:616270824990:web:40c8b177c6b9729cb5110f", }; // Initialize Firebase firebase.initializeApp(firebaseConfig);

Firestore samlinger og dokumenter

Der er to nøgleudtryk, der er vigtige for at forstå, hvordan man arbejder med Firestore: dokumenter og samlinger .

Documents are individual pieces of data in our database. 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 these fields can be strings, numbers, Booleans, objects, arrays, and even binary data.

document -> { key: value } 

Sets of these documents of these 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).

collection -> [{ id: doc }, { id: doc }]

Managing our database with the Firestore Console

Before we can actually start working with our database we need to create it.

Within our Firebase console, go to the 'Database' tab and create your Firestore database.

Once you've done that, we will start in test mode and enable all reads and writes to our database. In other words, we will have open access to get and change data in our database. If we were to add Firebase authentication, we could restrict access only to authenticated users.

After that, we'll be taken to our database itself, where we can start creating collections and documents. The root of our database will be a series of collections, so let's make our first collection.

We can select '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 users' favorite books. We'll give our first collection the id 'books'.

Next, we'll add our first document with our newly-created 'books' collection.

Each document is going to have an id as well, linking it to the collection in which it exists.

In most cases we're going to use 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.

For our first book, we'll make a 'title' field of type 'string', with the value 'The Great Gatsby', and hit save.

After that, we should see our first item in our database.

Getting data from a collection with .get()

To get access Firestore use all of the methods it provides, we use firebase.firestore(). This method need to be executed every time we want to interact with our Firestore database.

Jeg vil anbefale at oprette en dedikeret variabel til at gemme en enkelt reference til Firestore. Dette hjælper med at skære ned på mængden af ​​kode, du skriver på tværs af din app.

const db = firebase.firestore(); 
I dette cheatsheet vil jeg dog holde fast ved at bruge firestore-metoden hver gang for at være så klar som muligt.

For at henvise til en samling bruger vi .collection()metoden og angiver en samlings id som et argument. For at få en reference til den bøgeresamling, vi oprettede, skal du blot indsende strengen 'bøger'.

const booksRef = firebase.firestore().collection('books');

For at få alle dokumentdata fra en samling kan vi kæde på .get()metoden.

.get()returnerer et løfte, hvilket betyder, at vi kan løse det enten ved hjælp af .then()tilbagekald, eller vi kan bruge async-afventet syntaks, hvis vi udfører vores kode inden for en async-funktion.

Once our promises is resolved in one way or another, we get back what's known as a snapshot.

For a collection query that snapshot is going to consist of a number of individual documents. We can access them by saying snapshot.docs.

From each document, we can get the id as a separate property, and the rest of the data using the .data() method.

Here's what our entire query looks like:

const booksRef = firebase .firestore() .collection("books"); booksRef .get() .then((snapshot) => { const data = snapshot.docs.map((doc) => ({ id: doc.id, ...doc.data(), })); console.log("All data in 'books' collection", data); // [ { id: 'glMeZvPpTN1Ah31sKcnj', title: 'The Great Gatsby' } ] });

Subscribing to a collection with .onSnapshot()

The .get() method simply returns all the data within our collection.

To leverage some of Firestore's realtime capabilities we can subscribe to a collection, which gives us the current value of the documents in that collection, whenever they are updated.

Instead of using the .get() method, which is for querying a single time, we use the .onSnapshot() method.

firebase .firestore() .collection("books") .onSnapshot((snapshot) => { const data = snapshot.docs.map((doc) => ({ id: doc.id, ...doc.data(), })); console.log("All data in 'books' collection", data); });

In the code above, we're using what's known as method chaining instead of creating a separate variable to reference the collection.

What's powerful about using firestore is that we can chain a bunch of methods one after another, making for more declarative, readable code.

Within onSnapshot's callback, we get direct access to the snapshot of our collection, both now and whenever it's updated in the future. Try manually updating our one document and you'll see that .onSnapshot() is listening for any changes in this collection.

Difference between .get() and .onSnapshot()

The difference between the get and the snapshot methods is that get returns a promise, which needs to be resolved, and only then we get the snapshot data.

.onSnapshot, however, utilizes synchronous callback function, which gives us direct access to the snapshot.

This is important to keep in mind when it comes to these different methods--we have to know which of them return a promise and which are synchronous.

Unsubscribing from a collection with unsubscribe()

Note additionally that .onSnapshot() returns a function which we can use to unsubscribe and stop listening on a given collection.

This is important in cases where the user, for example, goes away from a given page where we're displaying a collection's data. Here's an example, using the library React were we are calling unsubscribe within the useEffect hook.

When we do so this is going to make sure that when our component is unmounted (no longer displayed within the context of our app) that we're no longer listening on the collection data that we're using in this component.

function App() { const [books, setBooks] = React.useState([]); React.useEffect(() => { const unsubscribe = firebase .firestore() .collection("books") .onSnapshot((snapshot) => { const data = snapshot.docs.map((doc) => ({ id: doc.id, ...doc.data(), })); setBooks(data); }); }, []); return books.map(book => ) }

Getting Individual Documents with .doc()

When it comes to getting a document within a collection., the process is just the same as getting an entire collection: we need to first create a reference to that document, and then use the get method to grab it.

After that, however, we use the .doc() method chained on to the collection method. In order to create a reference, we need to grab this id from the database if it was auto generated. After that, we can chain on .get() and resolve the promise.

const bookRef = firebase .firestore() .collection("books") .doc("glMeZvPpTN1Ah31sKcnj"); bookRef.get().then((doc) => { if (!doc.exists) return; console.log("Document data:", doc.data()); // Document data: { title: 'The Great Gatsby' } });

Notice the conditional if (!doc.exists) return; in the code above.

Once we get the document back, it's essential to check to see whether it exists.

If we don't, there'll be an error in getting our document data. The way to check and see if our document exists is by saying, if doc.exists, which returns a true or false value.

If this expression returns false, we want to return from the function or maybe throw an error. If doc.exists is true, we can get the data from doc.data.

Adding document to a collection with .add()

Next, let's move on to changing data. The easiest way to add a new document to a collection is with the .add() method.

All you need to do is select a collection reference (with .collection()) and chain on .add().

Going back to our definition of documents as being like JavaScript objects, we need to pass an object to the .add() method and specify all the fields we want to be on the document.

Let's say we want to add another book, 'Of Mice and Men':

firebase .firestore() .collection("books") .add({ title: "Of Mice and Men", }) .then((ref) => { console.log("Added doc with ID: ", ref.id); // Added doc with ID: ZzhIgLqELaoE3eSsOazu });

The .add method returns a promise and from this resolved promise, we get back a reference to the created document, which gives us information such as the created id.

The .add() method auto generates an id for us. Note that we can't use this ref directly to get data. We can however pass the ref to the doc method to create another query.

Adding a document to a collection with .set()

Another way to add a document to a collection is with the .set() method.

Where set differs from add lies in the need to specify our own id upon adding the data.

This requires chaining on the .doc() method with the id that you want to use. Also, note how when the promise is resolved from .set(), we don't get a reference to the created document:

firebase .firestore() .collection("books") .doc("another book") .set({ title: "War and Peace", }) .then(() => { console.log("Document created"); });

Additionally, when we use .set() with an existing document, it will, by default, overwrite that document.

If we want to merge, an old document with a new document instead of overwriting it, we need to pass an additional argument to .set() and provide the property merge set to true.

// use .set() to merge data with existing document, not overwrite const bookRef = firebase .firestore() .collection("books") .doc("another book"); bookRef .set({ author: "Lev Nikolaevich Tolstoy" }, { merge: true }) .then(() => { console.log("Document merged"); bookRef .get() .then(doc => { console.log("Merged document: ", doc.data()); // Merged document: { title: 'War and Peace', author: 'Lev Nikolaevich Tolstoy' } }); });

Updating existing data with .update()

When it comes to updating data we use the update method, like .add() and .set() it returns a promise.

Hvad der er nyttigt ved brug .update()er, at det i modsætning .set()til ikke overskriver hele dokumentet. Ligeledes .set()skal vi henvise til et individuelt dokument.

Når du bruger .update()det, er det vigtigt at bruge en vis fejlhåndtering, f.eks. .catch()Tilbagekald i tilfælde af, at dokumentet ikke findes.

const bookRef = firebase.firestore().collection("books").doc("another book"); bookRef .update({ year: 1869, }) .then(() => { console.log("Document updated"); // Document updated }) .catch((error) => { console.error("Error updating doc", error); }); 

Sletning af data med .delete ()

Vi kan slette en given dokumentsamling ved at henvise til den ved dens id og udføre .delete()metoden, simpel som den. Det giver også et løfte.

Her er et grundlæggende eksempel på at slette en bog med id'et "en anden bog":

firebase .firestore() .collection("books") .doc("another book") .delete() .then(() => console.log("Document deleted")) // Document deleted .catch((error) => console.error("Error deleting document", error));
Bemærk, at den officielle Firestore-dokumentation ikke anbefaler at slette hele samlinger, kun individuelle dokumenter.

Arbejde med undersamlinger

Let's say that we made a misstep in creating our application, and instead of just adding books we also want to connect them to the users that made them. T

The way that we want to restructure the data is by making a collection called 'users' in the root of our database, and have 'books' be a subcollection of 'users'. This will allow users to have their own collections of books. How do we set that up?

References to the subcollection 'books' should look something like this:

const userBooksRef = firebase .firestore() .collection('users') .doc('user-id') .collection('books');

Note additionally that we can write this all within a single .collection() call using forward slashes.

The above code is equivalent to the follow, where the collection reference must have an odd number of segments. If not, Firestore will throw an error.

const userBooksRef = firebase .firestore() .collection('users/user-id/books');

To create the subcollection itself, with one document (another Steinbeck novel, 'East of Eden') run the following.

firebase.firestore().collection("users/user-1/books").add({ title: "East of Eden", });

Then, getting that newly created subcollection would look like the following based off of the user's ID.

firebase .firestore() .collection("users/user-1/books") .get() .then((snapshot) => { const data = snapshot.docs.map((doc) => ({ id: doc.id, ...doc.data(), })); console.log(data); // [ { id: 'UO07aqpw13xvlMAfAvTF', title: 'East of Eden' } ] });

Useful methods for Firestore fields

There are some useful tools that we can grab from Firestore that enables us to work with our field values a little bit easier.

For example, we can generate a timestamp for whenever a given document is created or updated with the following helper from the FieldValue property.

We can of course create our own date values using JavaScript, but using a server timestamp lets us know exactly when data is changed or created from Firestore itself.

firebase .firestore() .collection("users") .doc("user-2") .set({ created: firebase.firestore.FieldValue.serverTimestamp(), }) .then(() => { console.log("Added user"); // Added user });

Additionally, say we have a field on a document which keeps track of a certain number, say the number of books a user has created. Whenever a user creates a new book we want to increment that by one.

An easy way to do this, instead of having to first make a .get() request, is to use another field value helper called .increment():

const userRef = firebase.firestore().collection("users").doc("user-2"); userRef .set({ count: firebase.firestore.FieldValue.increment(1), }) .then(() => { console.log("Updated user"); userRef.get().then((doc) => { console.log("Updated user data: ", doc.data()); }); }); 

Querying with .where()

What if we want to get data from our collections based on certain conditions? For example, say we want to get all of the users that have submitted one or more books?

We can write such a query with the help of the .where() method. First we reference a collection and then chain on .where().

The where method takes three arguments--first, the field that we're searching on an operation, an operator, and then the value on which we want to filter our collection.

Vi kan bruge en af ​​følgende operatorer, og de felter, vi bruger, kan være primitive værdier såvel som arrays.

<, <=, ==, >, >=, array-contains, in, Ellerarray-contains-any

For at hente alle de brugere, der har indsendt mere end en bog, kan vi bruge følgende forespørgsel.

Når .where()vi skal kæde videre .get(). Når vi løser vores løfte, får vi tilbage det, der er kendt som en forespørgsel Snapshot .

Ligesom at få en samling kan vi gentage over forespørgslenSnapshot med for .map()at få hvert dokument-id og data (felter):

firebase .firestore() .collection("users") .where("count", ">=", 1) .get() .then((querySnapshot) => { const data = querySnapshot.docs.map((doc) => ({ id: doc.id, ...doc.data(), })); console.log("Users with > 1 book: ", data); // Users with > 1 book: [ { id: 'user-1', count: 1 } ] });
Bemærk, at du kan kæde på flere .where()metoder for at oprette sammensatte forespørgsler.

Begrænsning og bestilling af forespørgsler

Another method for effectively querying our collections is to limit them. Let's say we want to limit a given query to a certain amount of documents.

If we only want to return a few items from our query, we just need to add on the .limit() method, after a given reference.

If we wanted to do that through our query for fetching users that have submitted at least one book, it would look like the following.

const usersRef = firebase .firestore() .collection("users") .where("count", ">=", 1); usersRef.limit(3)

Another powerful feature is to order our queried data according to document fields using .orderBy().

If we want to order our created users by when they were first made, we can use the orderBy method with the 'created' field as the first argument. For the second argument, we specify whether it should be in ascending or descending order.

For at få alle brugerne ordnet efter, da de blev oprettet fra nyeste til ældste, kan vi udføre følgende forespørgsel:

const usersRef = firebase .firestore() .collection("users") .where("count", ">=", 1); usersRef.orderBy("created", "desc").limit(3);

Vi kan kæde .orderBy()med .limit(). For at dette fungerer ordentligt, .limit()skal det kaldes sidste og ikke før .orderBy().

Vil du have din egen kopi? ?

Hvis du vil have denne vejledning til fremtidig reference, skal du downloade et cheatsheet af hele denne tutorial her.

Klik for at få fat i cheatsheetet