1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Firebase Database NoSDK

Last updated at Posted at 2021-08-29

エンドポイントの設定

base='https://osaka5-default-rtdb.asia-southeast1.firebasedatabase.app'

Realtime Databaseを開き、赤枠内をbase変数に設定
Firebaseプロジェクト作成(80秒) Firebaseは無料で利用できます。クレカは登録してはいけません
console.firebase.google.com_project_osaka5_database_osaka5-default-rtdb_data_hl=ja2.png

データを追加

curl

curl $base/users.json -d '{"名前":"山田太郎","出身地":"沖縄"}'

JavaScript

await fetch(base+'/users.json', {
  method: 'POST',
  body: '{"名前":"山田花子","出身地":"北海道"}'
})

Google Apps Script

UrlFetchApp.fetch(base+'/users.json', {
  method: 'POST',
  payload: '{"名前":"JohnDoe","出身地":"米国"}'
})

データを取得

curl

curl $base/users.json

JavaScript

await fetch(base+'/users.json').then(r => r.json())

Google Apps Script

JSON.parse(UrlFetchApp.fetch(base+'/users.json'))

すべてのデータを取得

curl

curl $base/.json

JavaScript

await fetch(base+'/.json').then(r => r.json())

Google Apps Script

JSON.parse(UrlFetchApp.fetch(base+'/.json'))

データを上書き

curl

curl $base/users/1.json -X PUT -d '{"名前":"山田太郎","出身地":"石垣島"}'

JavaScript

await fetch(base+'/users/2.json', {
  method: 'PUT',
  body: '{"名前":"山田花子","出身地":"択捉島"}'
})

Google Apps Script

UrlFetchApp.fetch(base+'/users/3.json', {
  method: 'PUT',
  payload: '{"名前":"JohnDoe","出身地":"カナダ"}'
})

データを更新

curl

curl $base/users/1.json -X PATCH -d '{"出身地":"沖ノ鳥島"}'

JavaScript

await fetch(base+'/users/2.json', {
  method: 'PATCH',
  body: '{"出身地":"弁天島"}'
})

Google Apps Script

UrlFetchApp.fetch(base+'/users/3.json', {
  method: 'PATCH',
  payload: '{"出身地":"ハワイ"}'
})

データを削除

curl

curl $base/users/1.json -X DELETE

JavaScript

await fetch(base+'/users/2.json', {method: 'DELETE'})

Google Apps Script

UrlFetchApp.fetch(base+'/users/3.json', {method: 'DELETE'})

キャッシュ

curl

curl -i $base/users.json -H 'X-Firebase-ETag: true'

ETagが付与される
Screenshot from 2021-08-29 21-05-51.png

JavaScript

await fetch(base+'/users.json', {
  headers: {'X-Firebase-ETag': true}
}).then(r => r.json())

キャッシュによりデータ転送量が節約できる
Screenshot from 2021-08-29 21-07-03.png

Google Apps Script

JSON.parse(UrlFetchApp.fetch(base+'/users.json', {
  headers: {'X-Firebase-ETag': true}
}))

上書き防止

curl

curl $base/users.json -H 'X-Firebase-ETag: true' -H 'if-match: u/abcdZ64BrbTEOhyb2V5TWQM2o=' -iX PUT -d '{"名前":"山田太郎","出身地":"石垣島"}'

上記ETag: u/nNaTZ64BrbTEOhyb2V5TWQM2o=と異なるため上書きできない。データのコンフリクトを回避できる
Screenshot from 2021-08-29 21-19-51.png

JavaScript

await fetch(base+'/users.json', {
  method: 'PUT',
  headers: {'if-match': 'u/abcdZ64BrbTEOhyb2V5TWQM2o='},
  body: '{"名前":"山田花子","出身地":"択捉島"}'
})

Screenshot from 2021-08-29 21-24-44.png

Google Apps Script

UrlFetchApp.fetch(base+'/users.json', {
  method: 'PUT',
  headers: {'if-match': 'u/abcdZ64BrbTEOhyb2V5TWQM2o='},
  payload: '{"名前":"JohnDoe","出身地":"カナダ"}'
})

Screenshot from 2021-08-29 21-26-26.png

クエリパラメータ

プリティープリント

プリティー

curl $base/.json?print=pretty

Screenshot from 2021-08-29 21-37-03.png

Not プリティー

curl $base/.json

Screenshot from 2021-08-29 21-37-35.png

一階層目のみ取得(Not再帰的)

curl $base/.json?shallow=true

Screenshot from 2021-08-29 21-41-47.png

JSONP

<script>
  function gotData(data) {
    console.log(data);
  }
</script>
<script src="https://osaka5-default-rtdb.asia-southeast1.firebasedatabase.app/.json?callback=gotData"></script>

Screenshot from 2021-08-29 21-43-59.png

Download

https://osaka5-default-rtdb.asia-southeast1.firebasedatabase.app/.json?download=db.json
Content-Dispositionが付与され、ダウンロードできます
Screenshot from 2021-08-29 21-48-48.png

Playground

Access-Control-Allow-Origin: *のため、どこからでもAPIを実行可能です

F12を押してQiita上やTampermonkeyでも実行できます。

See the Pen Firebase Realtime Database by John Doe (@04) on CodePen.

引用

1
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?