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

DS#defineResource(options)

Define a resource and register it with the data store. Takes just a single argument: a hash of options (object). As a convenient shorthand, you can just pass the name (string) if you don't need to specify any options.

Returns the new resource.

var store = new JSData.DS();

// DS#defineResource takes a single argument: a hash of options
var Document = store.defineResource({
  name: 'document',
  endpoint: 'documents'
});

// convenient shorthand when you don't need to specify any options
// just pass the name
var User = store.defineResource('user');

🚧

Note!

The resource definition can override any option configured on the whole data store. Here is the complete list of data store options: Configuration

Options

Only some of the available options are listed here. See the note directly above.

nametypedescription
namestringRequired. The name of the new resource.
idAttributestringThe name of the field to use as the primary key for instances of this resource. Computed properties are supported as ids. Default: "id".
basePathstringOverride the default basePath for this resource. Default: DS#defaults.basePath
endpointstringOverride the default endpoint for this resource. Default: name.
useClassbooleanWhether to use a wrapper class created from the ProperCase name of the resource. Must be true for computed properties and instance methods to work. Default: true.
keepChangeHistorybooleanWhether to keep a history of changes for items in the data store. Default: false.
resetHistoryOnInjectbooleanWhether to reset the history of changes for items when they are injected or re-injected into the data store. This will also reset an item's previous attributes. Default: true.
defaultFilterfunctionOverride the filtering used internally by DS.filter with your own function here. Default: See the source code.
metaobjectPut anything you want here. It will never be used by the API.
methodsobjectSee Instance Methods (Custom instance behavior).
actionsobjectSee Configuration (actions).
computedobjectSee Computed Properties.
beforeValidatefunctionSee Model Lifecycle Hooks.
validatefunctionSee Model Lifecycle Hooks.
afterValidatefunctionSee Model Lifecycle Hooks.
beforeCreatefunctionSee Model Lifecycle Hooks.
afterCreatefunctionSee Model Lifecycle Hooks.
beforeUpdatefunctionSee Model Lifecycle Hooks.
afterUpdatefunctionSee Model Lifecycle Hooks.
beforeDestroyfunctionSee Model Lifecycle Hooks.
afterDestroyfunctionSee Model Lifecycle Hooks.
beforeInjectfunctionSee Model Lifecycle Hooks.
afterInjectfunctionSee Model Lifecycle Hooks.
relationsobjectSee Relations.
Live Demo

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


DS#defineResource(options)

Suggested Edits are limited on API Reference Pages

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