0

I am using a mongo based DB schema where I have list of ReferenceFields (ObjectIds).

So assume I have two models : User, Group

User Object :

{
"_id" : ObjectId("554ba2897fdd66239457fb33"),
"name" : "something"
}

Group Object :

{
"_id" : ObjectId("453490sdkjskldfjlskdjf"),
"name" : "group_name",
users : [ ObjectId("554ba2897fdd66239457fb33"), ObjectId("554ba28a7fdd66239457fb34") ]
}

ExtJS User Model

Ext.define('Group',{
    extend: 'Ext.data.Model',
    idProperty : 'id',
    fields:[
        {name:'group_id', type : 'string', mapping : '_id', persist : false},
        {name:'name', type : 'string', mapping : 'name', persist : false}
    ],
    hasMany : { model : 'User', foreignKey:'id' , name : 'users'}
});

The User Model:

Ext.define('User',{
    extend: 'Ext.data.Model',
    idProperty : 'id',
    fields:[
        {name:'id', type : 'string', mapping : '_id', persist : false},
        {name: 'name', type: 'string'},
    ]
});

in EXTJS code when I access group.users();

Using the rest method in stores when I add associations to models by giving a foreign Key it fires a GET request to

/users?filter=[{"property":"group_id","value":"453490sdkjskldfjlskdjf","exactMatch":true}]

But this is MySQL kind of association.

How do I solve this to adapt to mongo style of associations?

SO I basically want this to happen. Right now when I call groups.users().load() it fires a GET request to the Users proxy with the filter params like this /users?filter=[{"property":"group_id","value":"453490sdkjskldfjlskdjf","exactMatch":true}]. But I don't have group_id in users! So it has to query the users proxy based on the ObjectId it finds inside the users attribute of the group object.

As this is very common in Document based DB, Is this possible by default ? Am I missing something ? Thanks.

Ashrith Sheshan
  • 654
  • 4
  • 17
  • @Saki : Hey! I want this to happen ->`group.users().load();` this should fetch the users and give me a store of users in that group. Also I have two stores already setup to have all the users and groups. Is there any way I can get them from the local store ? – Ashrith Sheshan May 08 '15 at 03:58
  • MongoDB is not a relational database, I don't think you will find ExtJS data module to work well with document databases. You could create an API that fronts mongo and handles the relations in a Compatible manner. I don't know why so many developers use mongo for relational data. There are better alternatives. You probably want such an API anyway to handle authorization? – oldwizard May 08 '15 at 04:48
  • See http://stackoverflow.com/questions/5841681/mongodb-normalization-foreign-key-and-joining – oldwizard May 08 '15 at 04:55

1 Answers1

0

Relationship 1:many in Ext is not implemented the same way as in DBMS. In DBMS we have two tables (a construct similar to store in Ext) and the relationship is defined by foreign keys that link records between tables.

In Ext, we (can) have a store on 1-side (Groups) but we do not have one single store for many-side (Users). Instead, each record of Groups contains a store for Users. It is not exactly a record field for the store but Ext defines a getter.

In your case group.users() returns a store with users that belong to that specific group. Ext behaves exactly same as with any other store when you try to load it: group.users().load() - it only adds filter for that specific foreign key so that you don't need to do it manually.

Saki
  • 5,827
  • 2
  • 15
  • 15
  • Yes I want exactly that to happen. I want a store of the users in that group. But the request that EXTJS generates is not compatible with my backend as I don't have a group_id in user schema. Instead I have a list of user_id in the group schema. Can I make EXTJS fetch a store of all users whose ObjectIds are there in the list attribute of Group ? – Ashrith Sheshan May 09 '15 at 06:35