Skip to content

Latest commit

 

History

History
124 lines (91 loc) · 3.87 KB

5. Cloud Firestore.md

File metadata and controls

124 lines (91 loc) · 3.87 KB

Cloud Firestore

Cloud Firestore is a flexible, scalable database for mobile, web, and server development from Firebase and Google Cloud Platform. Like Firebase Realtime Database, it keeps your data in sync across client apps through realtime listeners and offers offline support for mobile and web so you can build responsive apps that work regardless of network latency or Internet connectivity.

Cloud Firestore's NoSQL data model, you store data in documents that contain fields mapping to values. These documents are stored in collections, which are containers for your documents that you can use to organize your data and build queries. Documents support many different data types, from simple strings and numbers, to complex, nested objects. You can also create subcollections within documents and build hierarchical data structures that scale as your database grows.

Create a Cloud Firestore project

  1. Open the Firebase Console and create a new project.

  2. In the Database section, click Try Firestore Beta.

  3. Click Enable.

Set up your development environment

Add the required dependencies and client libraries to your app.

  1. Follow the instructions to add Firebase to your Web app.
  2. Add the Firebase and Cloud Firestore libraries to your app:
<script src="https://www.gstatic.com/firebasejs/8.1.1/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.1.1/firebase-firestore.js"></script>

Initialize Cloud Firestore

Initialize an instance of Cloud Firestore:

  firebase.initializeApp({
    apiKey: '### FIREBASE API KEY ###',
    authDomain: '### FIREBASE AUTH DOMAIN ###',
    projectId: '### CLOUD FIRESTORE PROJECT ID ###'
  });

Add Data

Cloud Firestore stores data in Documents, which are stored in Collections. Cloud Firestore creates collections and documents implicitly the first time you add data to the document. You do not need to explicitly create collections or documents.

Create a new collection and a document using the following example code.

firebase.firestore().collection("users").add({
    first: "Ada",
    last: "Lovelace",
    born: 1815
})
.then(function(docRef) {
    console.log("Document written with ID: ", docRef.id);
})
.catch(function(error) {
    console.error("Error adding document: ", error);
});

Read Data

Read Document Data which Document id: docid
let docid = "dfgfsd45bb"
firebase.firestore().collection("users").doc(docid).get().then((doc) => {
    console.log(`${doc.id} => ${doc.data()}`);
});
Read All the Documents
firebase.firestore().collection("users").get().then((querySnapshot) => {
    querySnapshot.forEach((doc) => {
        console.log(`${doc.id} => ${doc.data()}`);
    });
});
Read All in RealTime
firebase.firestore().collection("users").onSnapshot(querySnapshot) => {
    querySnapshot.forEach((doc) => {
        console.log(`${doc.id} => ${doc.data()}`);
    });
});

Update Document

let docid = "dfgfsd45bb"
let updatedData= { name: 'updatedName' }
firebase.firestore().collection("users").doc(docid).update(updatedData).then(() => {
    console.log('Doc updated');
}).catch(e=>consple.log(e))

Delete Document

let docid = "dfgfsd45bb"
firebase.firestore().collection("users").doc(docid).delete().then(() => {
    console.log('Doc Removed');
}).catch(e=>consple.log(e))

Data Modals

alt text

Backup with Extensions

npx @firebaseextensions/fs-bq-import-collection

// In BigQuery
// https://github.com/firebase/extensions/blob/master/firestore-bigquery-export/guides/IMPORT_EXISTING_DOCUMENTS.md
SELECT
  JSON_EXTRACT(DATA, '$.name') as name,
  JSON_EXTRACT(DATA, '$.age') as age
FROM `testtest-mappp.firestore_export.data_raw_latest`