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#find

DS#find(resourceName, id[, options])

The "R" in "CRUD", DS#find is for retrieving a single item via an adapter. With completely default settings, it's probably going to end up making a GET request to /resource/:id, where resource is the name of the resource and id is the primary key of the item to retrieve, e.g. store.find('user', 1). If you're working with the resource directly you can just do User.find(1).

DS#find is asynchronous and returns a promise.

DS#find first checks to see if the item is already in the store and if so immediately resolve the promise with the item. Otherwise, it delegates to the find method of whichever adapter is being used and then injects the resulting item into the data store.

bypassCache: true will force delegation to the adapter (ignore the item in the store).

cacheResponse: false will cause the result to not be injected into the store.

If DS#find ends up delegating to an adapter, the options argument (if you passed one) will also be passed to the adapter's find method, so you can pass options to the adapter as well.

If the result is to be injected into the store, the options argument will also be passed to DS#inject when it is called.

👍

You can call DS#find multiple ways

  • DS#find(resourceName, id[, options])
  • Resource#find(id[, options]) - Where Resource was created by DS#defineResource

Argument

Type

Description

resourceName

string

The name of the resource to use. Unnecessary if calling find directly on a Resource.

id

string or number

The primary key of the item to retrieve.

options (optional)

object

Configuration options. Settings are inherited from Resource and Global defaults. Will be passed through to the adapter's find method and DS#inject, if they are called.

options.adapter

string

The name of a registered adapter to use.

options.bypassCache

boolean

Bypass the cache and force the delegation to the adapter.

options.cacheResponse

boolean

Inject the resulting item into the store.

options.findStrategy

string

See findStrategy. Defaults to strategy.

options.findFallbackAdapters

array (of strings)

See findFallbackAdapters. Defaults to fallbackAdapters.

options.strategy

string

See strategy.

options.fallbackAdapters

array (of strings)

See fallbackAdapters.

Because options is passed through to an adapter's find method or DS#inject, then the options for those methods are valid options here as well.

Examples
// See Method Variants section below for different ways to call DS#find
Document.find(5)
  .then(function (document) {
    document; // { id: 5, author: 'John Anderson' }

    // The new document is already in the data store
    Document.get(document.id); // { id: 5, author: 'John Anderson' }

    // Force the request to be made again
    return Document.find(5, { bypassCache: true });
  })
  .then(function (document) {
    document; // { id: 5, author: 'John Anderson' }

    // The new document is still in the data store
    Document.get(document.id); // { id: 5, author: 'John Anderson' }
  });

Don't inject the result into the store

Document.find(5).then(function (document) {
  document; // { id: 5, author: 'John Anderson' }

  // The document is NOT in the data store
  Document.get(document.id); // undefined
});

Use a different adapter

Document.find(7, { adapter: 'localstorage' });

Pass some options to that will be used by the adapter

Document.find(7, {
  adapter: 'localstorage',
  params: {
    foo: 'bar' 
  }
});

Pass some options to that will be used by DS#inject

Document.find(7, {
  adapter: 'localstorage',
  beforeInject: function (Document, document) {
    document.foo = 'bar';
  }
});
Live Demos

js-data demo:

js-data + js-data-angular demo:

📘

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#find


DS#find(resourceName, id[, options])

Suggested Edits are limited on API Reference Pages

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