0

v3.rc5 js-data-http rc.2

  • my API responds with a nested resource e.g /user responds with a nested profile.

  • "profile" and "user" are two mappers with respective relations

"user": { "id": 1, "name": "Peter", "profile": { "id": 5 "dob": "today" } }

"profile" doesn't get added to the cache. I can call user.profile, but store.cachedFind('profile', 5) returns undefined.

calling manually store.addToCache('profile', user.profile) would not throw any errors but also wouldn't add it to the cache.

What am I doing wrong? Please help. Thanks.

toto11
  • 1,552
  • 1
  • 17
  • 18

1 Answers1

1

Here's a working example (the key is defining the relationship between user and profile):

import { DataStore } from 'js-data';

const store = new DataStore();

store.defineMapper('user', {
  relations: {
    hasOne: {
      profile: {
        foreignKey: 'userId',
        localField: 'profile'
      }
    }
  }
});

store.defineMapper('profile', {
  relations: {
    belongsTo: {
      user: {
        foreignKey: 'userId',
        localField: 'user'
      }
    }
  }
});

const user = store.add('user', {
  id: 123,
  profile: {
    id: 345,
    userId: 123
  }
});

console.log(store.get('user', 123));
console.log(store.get('profile', 345));
console.log(user.profile === store.get('profile', 345));
jdobry
  • 1,041
  • 6
  • 17