js-data

Respect Your Data

Give your data the treatment it deserves with a framework-agnostic, datastore-agnostic JavaScript ORM built for ease of use and peace of mind. Works in Node.js and in the Browser.

Get Started

DS#loadRelations

DS#loadRelations(resourceName, instance|id, relations[, options])

Asynchronously load the indicated relations for the given item.

Returns a promise.

Arguments
nametypedescription
resourceNamestringThe name of the resource to use. Unnecessary if using the resource directly.
idstring or numberThe id of the instance for which to load relations.
relationsstring or arrayThe resource names or field names of relations to load. If not provided then load all relations.
optionsobjectConfiguration options. Also passed through to the adapter and (conditionally) to DS.inject.
options.adapterstringOverride the default adapter.
options.cacheResponsebooleanInject the result into the store. Default: true.
Examples
var store = new JSData.DS();

var User = store.defineResource({
  name: 'user',
  relations: {
    hasOne: {
      profile: {
        localField: 'profile',
        foreignKey: 'userId'
      }
    }
  }
});

var Profile = store.defineResource({
  name: 'profile',
  relations: {
    belongsTo: {
      user: {
        localField: 'user',
        localKey: 'userId'
      }
    }
  }
});
User.loadRelations(10, ['profile']).then(function (user) {
  user.profile; // object
  assert.deepEqual(user.profile, Profile.filter({ userId: 10 })[0]);
});
var user = store.get('user', 10);

User.loadRelations(user, ['profile']).then(function (user) {
  user.profile; // object
  assert.deepEqual(user.profile, Profile.filter({ userId: 10 })[0]);
});
User.loadRelations(10, ['profile'], { cacheResponse: false }).then(function (user) {
  user.profile; // object
  assert.equal(Profile.filter({ userId: 10 }).length, 0);
});

Additional reading:

📘

Need help?

Want more examples or have a question? Ask on the Slack channel or post on the mailing list then we'll get your question answered and probably update this wiki.

Updated less than a minute ago

DS#loadRelations


DS#loadRelations(resourceName, instance|id, relations[, options])

Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.