1

Is it possible to save a new record using createRecord() and specify a custom key to save that new record under? At the moment when using createRecord() it's the same as using push() in the javascript framework.

Obviously the following:

var myObj = this.store.createRecord('myObj', {
    attr1: "Test"
})
myObj.save();

creates a new record with a key similiar to -KBCqhu4mhvl6xNfSRD3.

My problem is that there are some of my keys in different endpoints where I would like the keys to be the same as the user's id.

To clarify further, here is an example of what I would like my Firebase structure to end up as:

FB
|
|
users-- 
|       |
|       userID1--
|       |        |
|       |        userAttr1
|       |        |
|       |        userAttr2
|       |
|       userID2--...
|
drivers--
         |
         userID1--
                  |
                  driverAttr1
                  |
                  driverAttr2

Let me know if I can clarify further

Chris
  • 7,830
  • 6
  • 38
  • 72
  • I just want to make sure I understand the question. Could you use a separate foreign key to link the two? Do you need the key to be that specific thing? http://stackoverflow.com/questions/16638660/firebase-data-structure-and-url – Luke Schlangen Feb 23 '16 at 20:01
  • It would make things a bit easier yes, since data for different endpoints could then be fetched via the uid. Does that make sense? @LukeSchlangen – Chris Feb 23 '16 at 20:21
  • I'm trying to think of ways you could do this, but firebase uses the unique ID to do the saving which makes me think that if you were to remove it or change it, it wouldn't know what to save to. It might be possible, but I'm not sure it would make your life easier than the "foreign key" example. At least not anything I can think of. Good luck! – Luke Schlangen Feb 23 '16 at 22:13
  • Ok, thanks for having a look anyways @LukeSchlangen – Chris Feb 24 '16 at 10:53

1 Answers1

0

Include an id parameter in your createRecord request. That's it.

var myObj = this.store.createRecord('project', {
  id: 12345,
  name: "Big Project"
})
myObj.save()

Results in the data structure:

{projects: {
  12345: {
    name: "Big Project"
  }
}

(I had the same question and stumbled on the answer. It's not very well documented.)