Slide 1

Slide 1 text

.droidconKe Otieno Rowland Android Mentor at Udacity @rowlandoti;

Slide 2

Slide 2 text

▪ Cloud Firestore is a NoSQL, horizontally scaling document based model database in the cloud. ▪ With Firestore, instead of storing data as rows and columns, it is stored as a collection of documents. ▪ Cloud Firestore is Firebase's new flagship NoSQL database for mobile app development which improves on the successes of the Realtime Database with a new, more intuitive data storage model. ▪ It also features richer, faster queries and scales better than the Realtime Database. This session discusses these benefits, how you can integrate it into mobile apps and benefit from these improvements. Presented By: Otieno Rowland

Slide 3

Slide 3 text

Realtime Database ▪ Simple data is very easy to store as JSON. ▪ Complex, hierarchical data is harder to organize at scale. Causes problems when data grows leading to scalability issues. ▪ Requires a lot of denormalization and data flattening. ▪ Queries are deep hence inefficient as data not needed is retrieved. Cloud Firestore ▪ Simple data is easy to store in documents collection, which are very similar to JSON. ▪ Complex, hierarchical data is easier to organize at scale, using subcollections within documents. ▪ Requires less denormalization and data flattening. ▪ Queries are shallow, faster and more scalable Presented By: Otieno Rowland

Slide 4

Slide 4 text

▪ Already using other Firebase products – Firestore integrates well with other existing products and services. ▪ Enables and empowers teams to build “Serverless” Apps. ▪ Cuts costs and reduces development time drastically – time spent building and deploying server ▪ Scalable and built with big data companies in mind. ▪ Supports imports and exports of data. Easily exported for use in predictive analytics, ML training on Google cloud. Presented By: Otieno Rowland

Slide 5

Slide 5 text

▪ And with the new querying structure, all Cloud Firestore queries scale to the size of your result set -- not the size of your data. ▪ This means that a search for the top 10 restaurants in Nairobi for a restaurant review app will take the same amount of time whether your database has 300 restaurants, 300 thousand or 30 million. Presented By: Otieno Rowland

Slide 6

Slide 6 text

Documents live in collections, which are simply containers for documents. For example, you could have a rooms collection to contain your various rooms, each represented by a document. The names of documents within a collection are unique. You can provide your own keys, such as room IDs, or you can let Cloud Firestore create random IDs for you automatically (for example, by calling add()). Presented By: Otieno Rowland

Slide 7

Slide 7 text

Add the Firestore library as a dependency in your project. Configure the Firebase backend. Presented By: Otieno Rowland

Slide 8

Slide 8 text

Notice the alternating pattern of collections and documents. Your collections and documents must always follow this pattern. You cannot reference a collection in a collection or a document in a document. Documents in subcollections can contain subcollections as well, allowing you to further nest data. You can nest data up to 100 levels deep. (with great power comes great responsibility – remain shallow) Presented By: Otieno Rowland // Document ids are autogenerated val message = Message("Let's do this!") val messageRef = mFirebaseFirestone .collection("rooms").document() .collection("messages").document() .set(message) // Document ids are pre-determined val message = Message("Let's do this!") val messageRef = mFirebaseFirestone .collection("rooms").document("roomA") .collection("messages").document("message1") .set(message)

Slide 9

Slide 9 text

When you use set() to create a document, you must specify an ID for the document to create. Use keys with semantic meaning. To let Firebase autogenerate an id for you use add(). Presented By: Otieno Rowland // User pre-defined document id val user = User() user.setId(“rowland”) mFirebaseFirestone.collection("users") .document(user.userId) .set(user) // Firebase will autogenerate document id val user = User(userId) User.setId(“rowland”) mFirebaseFirestone.collection("users") .add(user)

Slide 10

Slide 10 text

To update some fields of a document without overwriting the entire document, use the update() method. If your document contains nested objects, you can use "dot notation" to reference nested fields within the document when you call update() Presented By: Otieno Rowland // Assume the document contains: // { // name: “Rowland", // favorites: {color: "Blue", subject: "recess" } // age: 12 // } // To update age and favorite color: mFirebaseFirestone.collection("users") .document("rowland") .update("age", 25,"favorites.color", “Blue")

Slide 11

Slide 11 text

Deleting a document does not delete its subcollections. They can be deleted manually. Presented By: Otieno Rowland // Assume the document contains: // { // name: "rowland", // favorites: { food: "Pizza", color: "Blue", subject: "recess" } // age: 12 // } // To delete document mFirebaseFirestone.collection("users") .document("rowland") .delete()

Slide 12

Slide 12 text

Perform powerful query functionality for specifying which documents you want to retrieve from a collection. The where() method takes three parameters: a field to filter on, a comparison operation, and a value. The comparison can be <, <=, ==, >, >=, or array_contains. For iOS, Android, and Java, the comparison operator is explicitly named in the method. Presented By: Otieno Rowland val citiesRef = mFirebaseFirestone.collection("cities") // More complex query types citiesRef.whereEqualTo("state", "California"); citiesRef.whereLessThan("population", 100000); citiesRef.whereGreaterThanOrEqualTo("name", "San Francisco")

Slide 13

Slide 13 text

There may be more complex cases where you may need to retrieve data from two different collections. This is common with denormalized database structures. Denormalization helps to flatten the hierarchy and achieve shallow queries. This in turn improves performance.You should never have to go deep. Instead use indexes. In NoSQL data duplication is a necessary evil, as it helps to flatten the database structure. This improves performance and speed of data retrieval. Presented By: Otieno Rowland // Get all products published by given merchant val categoryCollectionRef = mFirebaseFirestone.collection("products") val query = categoryCollectionRef .whereEqualTo(String.format("merchants.%s", userUid), true) .whereEqualTo(String.format("categories.%s", productCategory), true)

Slide 14

Slide 14 text

➢Array ➢String, Null & Boolean ➢Date & Time ➢Numeric - Float, Int, Long ➢Object & Bytes ➢Map e.t.c Presented By: Otieno Rowland

Slide 15

Slide 15 text

Unsecured Firestore Rule Secured Firestore Rule Presented By: Otieno Rowland // Only logged in users are allowed read/write service cloud.firestore { match /databases/{database}/documents { match /{document=**} { allow read, write: if request.auth.uid != null; } } } // Allows anonymous users to access,modify data // Never use in production service cloud.firestore { match /databases/{database}/documents { match /{document=**} { allow read, write: if true; } } }

Slide 16

Slide 16 text

➢ The Cloud Firestore documentation at https://firebase.google.com/docs/firestore/ ➢How queries work in Firestore at https://www.youtube.com/watch?v=Ofux_4c94FI ➢Choose a database: Cloud Firestore or Realtime Database at https://firebase.google.com/docs/firestore/rtdb-vs-firestore ➢Cloud Firestore for Realtime Database Developers https://firebase.googleblog.com/2017/10/cloud-firestore-for-rtdb- developers.html Presented By: Otieno Rowland

Slide 17

Slide 17 text

THANK YOU Presented By: Otieno Rowland