2014-10-23 13:38:50 +02:00
|
|
|
---
|
|
|
|
title: Firebase
|
2017-08-30 15:29:08 +02:00
|
|
|
prism_languages: [coffeescript]
|
|
|
|
tags: [WIP]
|
2014-10-23 13:38:50 +02:00
|
|
|
---
|
|
|
|
|
2017-08-30 15:29:08 +02:00
|
|
|
### Authenticating
|
2014-10-23 13:38:50 +02:00
|
|
|
|
2017-08-30 15:29:08 +02:00
|
|
|
```js
|
|
|
|
FB = new Firebase('https://xxx.firebase.io')
|
|
|
|
FB.auth(TOKEN, (err, result) => { ···})
|
2014-10-23 13:38:50 +02:00
|
|
|
```
|
|
|
|
|
2017-08-30 15:29:08 +02:00
|
|
|
```js
|
|
|
|
FB.authAnonymously(···)
|
|
|
|
FB.authWithPassword(···)
|
|
|
|
FB.authWithOAuthPopup(···)
|
|
|
|
FB.authWithOAuthToken(···)
|
|
|
|
```
|
2014-10-23 13:38:50 +02:00
|
|
|
|
2017-08-30 15:29:08 +02:00
|
|
|
### Using
|
2014-10-23 13:38:50 +02:00
|
|
|
|
2017-08-30 15:29:08 +02:00
|
|
|
```js
|
|
|
|
Users = FB.child('users')
|
|
|
|
```
|
2014-10-23 13:38:50 +02:00
|
|
|
|
2017-08-30 15:29:08 +02:00
|
|
|
```js
|
|
|
|
// Create
|
|
|
|
user = Users.push(first: "Frank", last: "Sinatra")
|
|
|
|
```
|
2014-10-23 13:38:50 +02:00
|
|
|
|
2017-08-30 15:29:08 +02:00
|
|
|
```js
|
|
|
|
// Retrieve
|
|
|
|
user = Users.child('alan') // gets `users/alan`
|
|
|
|
```
|
2014-10-23 13:38:50 +02:00
|
|
|
|
2017-08-30 15:29:08 +02:00
|
|
|
```js
|
|
|
|
// Update
|
|
|
|
user.set(first: "Miles", last: "Davis")
|
|
|
|
user.update(first: "Miles")
|
|
|
|
user.setWithPriority({ ··· }, priority)
|
|
|
|
```
|
2014-10-23 13:38:50 +02:00
|
|
|
|
2017-08-30 15:29:08 +02:00
|
|
|
```js
|
|
|
|
// Destroy
|
|
|
|
user.remove()
|
|
|
|
```
|
2014-10-23 13:38:50 +02:00
|
|
|
|
2017-08-30 15:29:08 +02:00
|
|
|
```js
|
|
|
|
// Getting
|
|
|
|
user.name() // primary id
|
2014-10-23 13:38:50 +02:00
|
|
|
|
2017-08-30 15:29:08 +02:00
|
|
|
user.once('value', (snap) => {
|
|
|
|
snap.name() // primary id
|
|
|
|
snap.val() // value
|
|
|
|
}, (err) => {
|
|
|
|
···
|
|
|
|
})
|
|
|
|
```
|
2014-10-23 13:38:50 +02:00
|
|
|
|
2017-08-30 15:29:08 +02:00
|
|
|
```js
|
|
|
|
// traversal
|
|
|
|
user.parent()
|
2014-10-23 13:38:50 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
### Querying
|
|
|
|
|
2017-08-30 15:29:08 +02:00
|
|
|
```coffeescript
|
|
|
|
Users = FB.child('users')
|
2014-10-23 13:38:50 +02:00
|
|
|
Users
|
|
|
|
.startAt(1000)
|
|
|
|
.limit(50)
|
|
|
|
.equalTo(priority, [name])
|
2017-08-30 15:29:08 +02:00
|
|
|
.on 'child_added', (snap) -> ···
|
2014-10-23 13:38:50 +02:00
|
|
|
```
|
|
|
|
### Lists
|
|
|
|
|
2017-08-30 15:29:08 +02:00
|
|
|
```coffeescript
|
|
|
|
Posts = FB.child('posts')
|
2014-10-23 13:38:50 +02:00
|
|
|
post = Posts.push({ title: "How to do things", author: "alan" })
|
|
|
|
```
|
|
|
|
|
2017-08-30 15:29:08 +02:00
|
|
|
## References
|
|
|
|
{: .-one-column}
|
2014-10-23 13:38:50 +02:00
|
|
|
|
2017-08-30 15:29:08 +02:00
|
|
|
* <https://www.firebase.com/docs/web/api/>
|
|
|
|
* <https://www.firebase.com/docs/web/recipes.html>
|