1

I have a realtime database on Firebase that has something like:

  • Pets
    • UNIQUE_ID
      • name: Beethoven
      • breed: Labrador,Golden
      • category: Adoption, Temporally-adoption
      • age: ...
      • ...

Since I'm new on Firebase, I have few doubts on how to use Firebase database that I couldn't get a clue on their documentation, can you help me?

  1. I'm trying to create a Cloud Function (to be used as a REST API) where I could search across all pets from a particular category, for example, Adoption. Is that possible? I can create, update... but I don't see how I should do this search...
var ref = admin.database().ref('/pets');
ref.????
  1. Can I create something similar as pagination from Cloud Functions against this search?

  2. I'm not familiar with this type of NoSQL databases, but do I need to create an index to do those objects searchable? I'm completely lost here, too. :(

  3. It is weird to use this Cloud functions to do this type of searches? Or should I use their own SDKs per each platform? Do they provide this searches too? Looks to me that it is easier to create a Cloud functions and call it from any platform as a rest call, right?

Thanks!

Pataquer
  • 283
  • 1
  • 14
  • 1
    Your current data structure allows you to efficiently find the categories for a specific pet. It does not allow you to efficiently find the pets for a specific category. For that you'll want to add an additional data structure to your database, where you keep the pet IDs for each category. E.g. `/categories/Adoption: { UNIQUE_ID: true }`. See my longer answer here https://stackoverflow.com/questions/40656589/firebase-query-if-child-of-child-contains-a-value – Frank van Puffelen Jun 20 '18 at 21:01
  • Uhm... I just now realized that this only applies if `category` can contain multiple values for each pet. If that is not is the case let me know, and I'll reopen the question. – Frank van Puffelen Jun 20 '18 at 21:02
  • @FrankvanPuffelen, I didn't follow this approach (I thought about this "fake index" as you suggest) because let's imagine that I add a "description" field where through a search, users can search if specific words are contain in this description field (search across all objects), or an adopter is looking for a pet which name was "Beethoven" but it just remembers "Beetho". My idea was to have a "search" that could do also this. As far as you said before, looks that it is not possible, right? – Pataquer Jun 21 '18 at 08:41
  • There's nothing fake about the index my linked answer suggests. The fact that the application code maintains it, doesn't make it any less real than an automatically maintained index. :-) – Frank van Puffelen Jun 21 '18 at 13:47
  • Your question covers a range of very broad topics, so it's hard to answer concisely. Doing a full-text search on Firebase is not possible, see: https://stackoverflow.com/search?tab=votes&q=%5bfirebase-database%5d%20text%20search. But so-called prefix searches *are* possible, so "any pet whose name starts with 'Beetho'" is `[[queryOrderedByChild:@"name"] queryStartingAtValue: @"Beetho"]`. – Frank van Puffelen Jun 21 '18 at 13:51
  • I'm checking firestore, and I think that it fits better with my needs. Thank you very much for your help! – Pataquer Jun 21 '18 at 16:06

1 Answers1

1
  1. Yes, this is possible, and if your goal is to create an API then you will need to use Cloud Functions for this. I would recommend that you switch from Firebase's realtime database to firestore. The realtime database is older and lacks some of the rich queries, the Firestore database has better support for advanced queries...

  2. Yep, check out these Firestore docs for examples of pagination.

  3. Not unless you get a huge data set, as a beginner, I'd recommend you not worry about indexing.

  4. You can do it either way, but if you're looking for cross-platform, then you're right - creating an API like this could definitely save you some trouble down the road.

JeremyW
  • 5,157
  • 6
  • 29
  • 30