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

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

The "U" in "CRUD". Persist an item already in the data store in its current state through whichever adapter is being used and inject the result into the data store.

Returns a promise.

Arguments
nametypedescription
resourceNamestringThe name of the resource to use. Unnecessary if using the resource directly.
idstring or numberThe primary key of the item to save.
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.
options.changesOnlybooleanOnly send changed and added values to the adapter. Default: false.
options.alwaysarrayProperty names which are always sent, even when not changed. Only relevant when changesOnly is true.
options.beforeValidatefunctionOverride the default beforeValidate hook.
options.validatefunctionOverride the default validate hook.
options.afterValidatefunctionOverride the default afterValidate hook.
options.beforeUpdatefunctionOverride the default beforeUpdate hook.
options.afterUpdatefunctionOverride the default afterUpdate hook.
Examples
Document.find(5).then(function (document) {
  document.name; // 'John'

  document.name = 'Johnny';
  
  // See the Method Variants section below for different ways to call DS#save
  return Document.save(5);
}).then(function (document) {
  document.name; // 'Johnny'
});

👍

Method Variants

You can call DS#save in multiple ways:

  • DS#save(resourceName, id);
  • Resource#save(id); // where Resource was created by DS#defineResource
  • Instance#DSSave(); // where instance is an instance of a Resource

📘

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


DS#save(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.