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

DS#create(resourceName, attrs[, options])

The "C" in "CRUD", create is for creating a single item via an adapter. With completely default settings, it's probably going to end up making a POST request to /resource, where resource is the name of the resource, e.g. store.create('user', { name: 'John' }). If you're working with the resource directly you can just do User.create({ name: 'John' }).

create asynchronous and returns a promise.

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

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

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

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

Argument

Type

Description

resourceName

string

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

attrs

object

The properties with which to create the item.

options (optional)

object

Settings are inherited from Resource and Global defaults. Will be passed through to the adapter's create 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.

options.upsert

boolean

If attrs already contains a primary key, then attempt to call DS#update instead.

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

Live 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 2 years ago

DS#create


DS#create(resourceName, 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.