Skip to content

Relations do not appear to work without "cacheResponse: true" on Node.js #17

@techniq

Description

@techniq

So I've spent the good part of this evening trying to get familiar with js-data within my backend (Node.js) and how it handles relations. After a few problems on my side (I had 2 belongsTo objects under relations for defineResource to name one), I think I've stumbled across an issue:

Until I set cacheResponse: true in JSData.DS's configuration I am unable to get any of my relation properties (belongsTo / hasMany) to be populated. I tried populating using

...
const Race = store.defineResource({
  name: 'race',
  relations: {
    belongsTo: {
      address: {
        localField: 'address',
        localKey: 'address_id'
      }
    },
  }
});

const Address = store.defineResource({
  name: 'address',
  relations: {
    hasMany: {
      race: {
        localField: 'races',
        foreignKey: 'address_id'
      }
    }
  }
});
...

let address = yield new store.Address.create(fixtures.address.race1);
let raceData = fixtures.race.race1;
raceData.address_id = address.id;
let race = yield new store.Race.create(raceData);
expect(race.address).to.exist; // AssertionError

let race2 = yield store.Race.loadRelations(race, ['address']);
expect(race2.address).to.exist; // AssertionError

If I set cacheResponse: true, the assertions succeed. Due to the issues explained on JSData on the Server, it seems like enabling cacheResponse: true would not be advisable.

On a semi-related note, the docs under Configuration do not indicate some defaults being different when ran under Node (like the "JSData on the Server" page does). It would be helpful if this documentation indicated the different default values based on the environment. I also wasn't sure if items like findHasMany, findBelongsTo, etc were set to false by default on Node, or if this was just recommended per the docs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions