# Services : Firebase : Database **Features** - multiple databases per project - flexible, expression-based rules language, called Firebase Realtime Database Security Rules - define how your data should be structured and when data can be read from or written to - when integrated with Firebase Auth -> can define who has access to what data, and how they can access it **Reference Examples** - "users/user:1234/phone_number" - "/users/$uid" In practice, it's best to keep your data structure as flat as possible... and learn to love denormalization. - When you fetch data at a node, you also retrieve all of its child nodes. - When you grant someone read or write access at a node, you also grant them access to all nodes under it. #### Reading Data is retrieved by attaching an asynchronous listener to a `firebase.database.Reference`. The listener is triggered once for the initial state of the data, and again anytime the data (including children) changes. The listener is passed a snapshot containing all data at that location, including child data. Most of the time, use `onValue()` to receive a snapshot and setup a listener. Use `get()` if you only need the initial snapshot. Use `once()` to get the data from local cache immediately. Pass a listener to `off()` to detach. Pass a reference to `off()` to detach all listeners at that reference (but not children). #### Writing Use `set()` to save data to a specified reference, replacing any existing data at that path. Use `update()` to simultaneously write to specific children of a node without overwriting other child nodes. Both take optional completion callbacks. Also, server-side increment with `increment()`. Use `push()` to generate a unique key/new node or append to list. (Values will order chronologically.) #### Deleting Simples way is call `remove()` on a reference. You can also specify `null` with a write operation like `set()` or `update()`. #### Transactions - given update function and optional completion callback - update function takes current state as argument and returns new state - if another client writes to it before you, update function called again with new current state #### Child Events - **child_added** — This event is triggered once for each existing child and then again every time a new child is added to the specified path. The listener is passed a snapshot containing the new child's data. - **child_changed** — This event is triggered any time a child node is modified. This includes any modifications to descendants of the child node. The snapshot passed to the event listener contains the updated data for the child. - **child_removed** — This event is triggered when an immediate child is removed. The snapshot passed to the callback block contains the data for the removed child. - **child_moved** — Listen for changes to the order of items in an ordered list. child_moved events always follow the `child_changed` event that caused the item's order to change (based on your current order-by method). Using `onChildAdded()`, `onChildChanged()`, `onChildRemoved()`, and `onChildMoved()`. #### Sorting ``` orderByChild() // order by value of specified path orderByKey() orderByValue() ``` #### Filtering ``` To filter data, you can combine one or more limit or range methods with an order-by method. limitToFirst() Sets the maximum number of items to return from the beginning of the ordered list of results. limitToLast() Sets the maximum number of items to return from the end of the ordered list of results. startAt() Return items greater than or equal to the specified key or value, depending on the order-by method chosen. startAfter() Return items greater than the specified key or value depending on the order-by method chosen. endAt() Return items less than or equal to the specified key or value, depending on the order-by method chosen. endBefore() Return items less than the specified key or value depending on the order-by method chosen. equalTo() Return items equal to the specified key or value, depending on the order-by method chosen. ```