0

This might be a no brainer for some of you but I'm not sure if its even possible.

Using AngularFire, I'd like to shorten the generated uids that are created on push. Why? Because I want to build a function to share a link to a dynamic path in Firebase so instead of

https://url.firebaseio.com/lists/-KGhj0n_wN1xyhtu7HOR

I'd like something like

https://url.firebaseio.com/AbcDe

The "generator" would build a new uid but still refer to "-KGhj0n_wN1xyhtu7HOR".

Is it possible, is it worth it or are there better ways to approach it?

mtorn
  • 159
  • 3
  • 16

1 Answers1

2

Yup, sounds completely doable. Create a new top-level list, where you map you short-links to the list IDs.

shortlinks: {
  "AbcDe": "-KGhj0n_wN1xyhtu7HOR"
}

Now when somebody comes in to a short link, you can simply look up the actual list ID with:

ref.child('shortlinks').child(shortlinkFromUrl).once('value', function(snapshot) {
  if (snapshot.exists()) {
    // TODO: load the list with key snapshot.val()
  }
  else {
    console.error('That's a non-existing shortlink');
  }
})

Remaining problems you will have to figure out

  • how will you generate the shortlinks? Firebase push ids (what you get when you call $firebaseArray.$add() or Firebase.push()) are guaranteed to be unique, which is one of the reasons they're so long. Shorter links are more easily memorable, but you will have to find some way to guarantee their uniqueness. See the accepted answer to How do you prevent duplicate user properties in Firebase?, but probably also a dozen other questions that deal with this.
  • if you find a way to generate shorter links that are unique enough for you use-case, why don't you use them as the key for your lists? So instead of storing the list under -KGhj0n_wN1xyhtu7HOR, store it under AbcDe. It saves you a lookup.
Community
  • 1
  • 1
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Sounds nice! And thanks for examples! As for now I have a simple javascript snippet which generates a 6 figure uid with uppercase letters, lowercase letters and numbers. Should be enough (this product is just for demo so no need for 110% uniqueness). I tried to route it to a live page and it seemed to work at first but figured it doesn't yet know what firebase.uid it refers to. Could be my route settings that are faulty but I'm not sure. I'll give this a go later tonight! – mtorn May 02 '16 at 07:26