1

I'm working on a Firebase application which will contain a set of codes, which are only available for one-time use, and will then be bound to a user permanently.

I want to achieve the best querying and updating capabilities on these codes, and following some recommendations from Firebase itself and previous questions I have come up with the following structure:

Codes
  - Available 
    - 12345 
      - property 1
      - property 2
    - 32124
      - property 1
      - property 2
  - Taken
    - 23456
      - property 1
      - property 2
Users
  - UID
    - Codes
      - 23456

This makes it so that querying available codes will be easy, as well as matching a code bound to a user with one of the taken codes (for verification after they log in).

The issue I'm having is, in order to move a code object from "Available" to "Taken", I have to delete it from the former and insert it into the later. Is this the right approach to take? I get nervous about physically deleting the data in order to move it.

Any advice on how to properly structure this use case?

Community
  • 1
  • 1
AdmanStrong
  • 323
  • 1
  • 8

1 Answers1

5

Not moving the data might be easier:

Codes
  12345 
    claimedBy: false
    property 1
    property 2
  32124
    claimedBy: false
    property 1
    property 2
  23456
    claimedBy: '98-12-ad-1a-9c'
    property 1
    property 2
Users
  98-12-ad-1a-9c
    - Codes
      - 23456

This way you can easily:

  • show all the codes: ref.child('Codes')
  • show all the available codes: ref.child('Codes').orderByChild('claimedBy').equalTo(false)
  • show the codes for each user: ref.child('Users').child(auth.uid).child('Codes')
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thanks! One quick follow up, if there were two properties I had to order by (claimedBy and a DateAvailable lets say), is that still possible? – AdmanStrong Sep 09 '15 at 12:18
  • Isn't it better to separate branches for data transfer reason? Will the objects be filtered on the client side or on the server side? If the requested objects are filtered on the client side, then the data transfer will be high! – DoK May 13 '16 at 08:27