Specify condition and limit for firebase realtime database request

青春壹個敷衍的年華 提交于 2020-01-06 18:44:51

问题


I'm trying to store some certain information in the firebase realtime database. My database structure looks like this:

    {
  "contacts" : {
    "-KdD1f0ecmVXHZ3H3abZ" : {
      "email" : "ksdsd@sdsd.com",
      "first_name" : "John",
      "last_name" : "Smith",
      "organization_id" : 1
    },
    "-KdG4iHEYjInv7ljBhgG" : {
      "email" : "superttest@sds213123d.com",
      "first_name" : "Max1",
      "last_name" : "Rosse13131313l",
      "organization_id" : 1
    },
    "-KdGAZ8Ws6weXWo0essF" : {
      "email" : "superttest@sds213123d.com",
      "first_name" : "Max1",
      "last_name" : "Rosse13131313l",
      "organization_id" : 1
    }
  }
}

I need to be able to specify condition like this SQL example:

SELECT * FROM contacts WHERE organization_id = :id LIMIT :start, :end

is there a way to do that in the database using Ruby or at least their REST API?


回答1:


The solution can be divided into two steps.

Step 1) You have to update rules in firebase Database if not already done.

{
  "rules": {
     "read": "auth !=null",
     "write": "auth !null",
     "REPLACE_WITH_ROOT_NODE_NAME": {
       "contacts": {
          "indexOn": ["organization_id"]
       }
     }
  }
}

Step 2) To fetch data from firebase Database. The rest API request should be something like this

https://app-name.firebaseio.com/REPLACE_WITH_ROOT_NODE_NAME/contacts.json?auth=REPLACE_WITH_DATABASE_PASSWORD&orderBy="organization_id"&equalTo="REPLACE_WITH_ORGANIZATION_ID"&LimitToFirst=N


来源:https://stackoverflow.com/questions/42315302/specify-condition-and-limit-for-firebase-realtime-database-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!