LoginSignup
1
3

More than 5 years have passed since last update.

firebase realtime database入門メモ

Posted at

realtimedatabaseとは

JSONで保存するfirebaseのDBでリアルタイムで同期されるのでチャットなどに便利。最近cloudfirestoreという別のDBができているがまだベータ版なのでrealtimedatabaseを使ったほうが無難かも。

使い方

webでの設定

  // Set the configuration for your app
  // TODO: Replace with your project's config object
  var config = {
    apiKey: "apiKey",
    authDomain: "projectId.firebaseapp.com",
    databaseURL: "https://databaseName.firebaseio.com",
    storageBucket: "bucket.appspot.com"
  };
  firebase.initializeApp(config);

  // Get a reference to the database service
  var database = firebase.database();

上記で設定

ルールの設定

realtimedatabaseではルールでデータのアクセス許可を管理する。

{
  "rules": {
    "foo": {
      ".read": true,
      ".write": false
    }
  }
}

上記はfooという情報の読み書きを許可する記述。

$location変数

突然登場した$user_idですが、これは $location変数と呼ばれる特殊な変数です。頭に$を付けた好きな名前の変数を宣言すると、「その階層にあるノードのキー」がそこに入ります。
具体的に言うと、今回の例では$user_idにはusersの下にあるuser001user002が入ります。$location変数を使うとその階層にある子ノードをfor-eachして、
それぞれのキー文字列を条件式に組み込むことができるというイメージですね。

引用:https://qiita.com/lacolaco/items/b068ab0cf19a26f0992d

データの読み書き

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3