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

DS#update(resourceName, id, attrs[, options])

The "U" in "CRUD". Update the item of type resourceName and primary key id with attrs and inject the result into the data store. This is useful when you want to update an item that isn't already in the data store, or you don't want to update the item that's in the data store until the adapter operation succeeds. This differs from DS.save which simply saves items in their current state.

With completely default settings, it's probably going to end up making a PUT 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.update('user', 1, { foo: 'bar' }). If you're working with the resource directly you can just do User.update(1, { foo: 'bar' }).

update is asynchronous and returns a promise.

Delegates to the update method of whichever adapter is being used and then injects the updated item into the data store.

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

As update delegates to an adapter, the options argument (if you passed one) will also be passed to the adapter's update 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#update multiple ways

  • DS#update(resourceName, id, attrs[, options])
  • Resource#update(id, attrs[, options]) - Where Resource was created by DS#defineResource
  • Instance#DSUpdate(attrs[, options]) - Where Instance is an instance of a Resource.

Argument

Type

Description

resourceName

string

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

id

string or number

The primary key of the item to update.

attrs

object

The properties with which to update the item.

options (optional)

object

Settings are inherited from Resource and Global defaults. Will be passed through to the adapter's update method and DS#inject, if it is called.

options.adapter

string

The name of a registered adapter to use.

options.cacheResponse

boolean

Inject the updated item into the store.

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

Examples
Document.find(5).then(function (document) {
  document.name; // 'John'

  return Document.update(5, { name: 'Johnny' });
}).then(function (document) {
  document.name; // 'Johnny'
});

📘

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


DS#update(resourceName, id, attrs[, options])

Suggested Edits are limited on API Reference Pages

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