I have a firestore database as below
My firestore database
I would like to query the CASH AND CREDT amount for document 20191003.
I used the below REST api
POST https://firestore.googleapis.com/v1/projects/badansales/databases/(default)/documents/sales/20191003:runQuery?key={YOUR_API_KEY}
{
"structuredQuery": {
"select": {
"fields": [
{
"fieldPath": "CASH"
},
{
"fieldPath": "CREDT"
}
]
}
}
}
But the response is as below
{
"0": {
"readTime": "2019-10-04T02:03:57.366908Z"
}
}
Can someone help on this? Thanks
I'd advise reading the documentation here: https://firebase.google.com/docs/firestore/use-rest-api#making_rest_calls which recommends checking out the API Explorer. This can help you create a query.
For your example, maybe try:
GET https://firestore.googleapis.com/v1/projects/badansales/databases/(default)/documents/sales/20191003?key={YOUR_API_KEY}
Notice two things:
It's a GET and not a POST (there's no alteration of the data here).
Related
When using the Firestore REST API to order and filter results, I am unable to use a cursor/reference value for the startAt value. I feel this may be possible, seeing it is provided in examples of Firestore's cursor-based pagination is detailed in their SDK: https://firebase.google.com/docs/firestore/query-data/query-cursors
I have a query that uses orderBy on a integer field within a document. I can successfully start at a specific integer value for this query, like so:
"structuredQuery": {
"from": [
{
"collectionId": "objects"
}
],
"orderBy": [
{
"field": {
"fieldPath": "counter"
},
"direction": "DESCENDING"
}
],
"startAt": {
"values": [
{
"integerValue": 15
}
]
}
}
I'm hoping to benefit from cursor pagination, but unfortunately if I change the startAt value to referenceValue, the query results do not reflect this, even though the query is successfully performed.
"startAt": {
"values": [
{
"referenceValue": "projects/.../databases/(default)/documents/objects/OjvmBvvQ9TkyyQiJ4ExJ"
}
]
}
Am I missing something in the way this works differently to the SDK examples?
Note that it's not a document reference but a document snapshot that you can use as a query cursor. A snapshot includes the field values needed for startAt. The SDKs take a document snapshot, extract the field values, and pass those values to startAt.
You can see the Node.js client library doing this here in createCursor and extractFieldValues.
I have implemented api_platform in my symfony 4 project, the problem is that I have to use the paths provided by the Rest api to fetch data using graphql like this
{
user(id:"api/users/1")
{
id
}
}
rather than
{
user(id:1){
id
}
}
Went through the documentation and didn't find a solution for this. Using plain api paths in graph api isn't really worth moving to graphql. Any help.
libraries like Prisma Binding allows you to query GraphQL using APIs
example on fetch user with ID of 1:
const query = `
query ($userId: ID!){
user(id: $userId) {
id
name
}
}
`
const variables = { userId: '1' }
prisma.request(query, variables).then(result => console.log(result))
// {"data": { "user": { "id": "1", "name": "Sarah" } } }
From here, we can write custom wrappers to translate the REST endpoints to corresponding queries
You can handle that in the resolver function, so you will receive a string param in the PHP resolver function instead of int(id).
check this example: https://github.com/khaledalam/simple-system-with-graphql-react-php
I'm developing an app using Firebase's Realtime Database and need to allow multiple users to access the same data, but I'm having trouble figuring out a security rule that makes this work.
The database looks like this:
teams: {
teamID3ic3kic9w3jkck : {
userIDs: ["11111", "22222", "33333", "44444"]
teamData { ....}
}
}
where I want to allow users with an ID matching any of the IDs in the "userIDs" array to access "teamData". Would really appreciate help figuring this out.
Every time you're looking to do array.contains(), you're likely using the wrong data structure. For example, this seems more like a mathematical set to me: an unordered collection of unique items. In Firebase you'd model that as:
teams: {
teamID3ic3kic9w3jkck : {
userIDs: {
"11111": true,
"22222": true,
"33333": true,
"44444: true"
]
teamData { ....}
}
}
Now you can secure this with:
{
"rules": {
"teams": {
"$teamid": {
".read": {
".read": "data.child('userIDs').child(auth.uid).exists()"
}
}
}
}
}
I'm stuck with querying Firestore REST API. I can figure out how to filter by a regular key but I can't figure out how to query by nested object key.
Basically, in the browser client, I can do
firestore.collection("channels")
.where("members.[memberID]", "==", true)
.get()
However, when I try to do the same in the REST API, it said Invalid Argument. I was trying to do
{
"structuredQuery": {
"where": {
"fieldFilter": {
"field": {
"fieldPath": "members.[memberID]"
},
"op": "EQUAL",
"value": {
booleanValue: "true"
}
}
},
"from": [
{
"collectionId": "channels"
}
]
}
}
but it gives me "Invalid Argument" error on fieldPath. Does anybody know how to query Firestore REST API based on nested object?
Thank you!
I asked for Firebase support and they said special characters in fieldPath such as "-" in my case can be escaped using back tick or back slash. It is mentioned in the documentation. In my case, I'll need to do
members.`[memberID]`
I hope this is helpful for you guys.
I use FOSrestbundle in my API to get my objects as a JSON.
So my controller look like that:
public function getArticleAction($id)
{
$article = $this->getDoctrine()
->getRepository('ApiBundle:Article')
->find($id);
return $this->handleView($this->view($article));
}
And my issue is that an article contains comments, added by users so my json looks like that:
{
"title": "My Article",
"comments": [
{
"content": "my first comment",
"added_by": {
"username": "John"
}
},
{
"content": "my second comment",
"added_by": {
"username": "Smith"
}
}
]
}
and when I render it, there is too many queries, for each comment there is a query to get user informations.
Do I have to get my article object with a querybuilder and lot of join (because it is just an example but I have a lot more relations) to get all the informations in one query or is there an other trick to avoid it ?
You should use EAGER loading for added_by relation while querying your data using Doctrine. This way you will query all data you need for serialization at once.