Jus10
Jus10

Reputation: 15669

Query Firebase for highest id, then add to it

I need help with a Firebase query. I'm using AngularFire2 at the moment.

I've been struggling with this for days. Read every document, watched every YouTube video, and asked many people, but I'm getting nowhere..

My Firebase structure is something like this:

ROOT
    jobs
        KYLigf4dFBvTTstR1Wy
            id: "1"
            customer: "Jeff F"
            description: "Repair a chair"
        KYLigACCViJVQffEoRl
            id: "2"
            customer: "Quick G"
            description: "Install a light bulb"
        KYLigSQasdCR3XfQ8WN
            id: "3"
            customer: "Justin S"
            description: "Do everything for everyone"
        KYLiddsfdsgWCKhjoiB
            id: "4"
            customer: "Heather D"
            description: "Have a baby"

So I need to fetch the highest ID. Then I need to add a new entry to the list using the next id. In this case it would be 5. That's it, that's all I need. And I'm pulling my hair out trying to do it.

I've tried a million different ways, but the problem comes when I try to use push(), everything falls apart, so I just can't find the proper path to go down here.

I guess another way of saying this is, that I would like to pull the highest ID from Firebase, then save it to a variable that changes with it. Then I could just access that variable whenever I needed to get the latest id - since the number will always be changing.

Upvotes: 1

Views: 867

Answers (2)

ProfDFrancis
ProfDFrancis

Reputation: 9411

Use Firebase's weird "KYLigf4dFBvTTst..." keys as your id

We are all more comfortable with conventional id's like 1, 2, 3, and therefore bend over backwards trying to find ways of generating them. However, the need to create a conventional id has created a complicated problem for you.

Firebase's weird-looking auto-generated keys are actually designed to solve your problem. They have the features of an incrementing id, i.e. they tell you the date order of creation, and also effectively provide uniqueness like a UUID.

Importantly, they allow multiple clients who are offline to still generate very-likely time-ordered and unique id's.

Firebase keys can be generated off-line

They are not just strange to look at, they are also magically able to be generated without an internet connection.

The process that generates them,

.push

is a pure client-side operation, that can run (for example) in your browser while you are not connected to the net. It does not send anything to the database.

The relevance of this is that the firebase key can be generated and discarded at will without penalty, and you can generate as many of them as you like: only when you actually write content into the key does anything actually happen in the database.

Let go of 1,2,3

They are from the pre-connected era. I am thinking of teaching my baby daughter to count in Firebase Id's instead...

Upvotes: 1

Gregg
Gregg

Reputation: 667

One possible solution would be to create a second node that tracks the next ID.

When you insert a new Job, follow up the insert with an update to the nextJobId node to increment the ID. This would simplify the need to retrieve the next ID.

Keep in mind that with Firebase the data structure is denormalized. Its okay to create additional nodes to make reads faster.

ROOT
jobs
    KYLigf4dFBvTTstR1Wy
        id: "1"
        customer: "Jeff F"
        description: "Repair a chair"
    KYLigACCViJVQffEoRl
        id: "2"
        customer: "Quick G"
        description: "Install a light bulb"
    KYLigSQasdCR3XfQ8WN
        id: "3"
        customer: "Justin S"
        description: "Do everything for everyone"
    KYLiddsfdsgWCKhjoiB
        id: "4"
        customer: "Heather D"
        description: "Have a baby"
nextJobId: 5 // Create an index that provides the next Id.

Upvotes: 2

Related Questions