This article is for LoopBack 2.x, which is no longer supported. Please see the corresponding article for 3.x.

Creating, updating, and deleting data

Edit this page
Page Contents
Warning: Methods of models in the AngularJS client have a different signature than those of the Node API. For more information, see AngularJS SDK API.

PersistedModel has a large set of methods for creating, updating, and deleting data.

Model data is also called a model instance; in database terminology, conceptually a model corresponds to a table, and a model instance corresponds to a row or record in the table.

Note: For information on model read operations, see Querying data.

Creating data (model instances)

Use the following PersistedModel methods to add data, that is to insert or create instances:

  • create - creates a new model instance (record).
  • upsert - checks if the instance (record) exists, based on the designated ID property, which must have a unique value; if the instance already exists, the method updates that instance. Otherwise, it inserts a new instance.
  • findOrCreate - Find one instance matching the filter object provided as the first parameter. If found, returns the object. If not found, creates a new instance (record).

    Important: Be sure to include a where clause in the filter object. Without the where, the findOrCreate finds and returns the first record in the collection, without error, which can lead to unintended behavior.

</div>

  • save - Save model instance. If the instance doesn’t have an ID, then calls create instead. Triggers: validate, save, update, or create.

Updating data (model instances)

Static method (called on the Model object):

Important: The where clause used with updateAll() is slightly different than that for queries. Omit { where : ... } from the where clause. Simply provide the condition as the first argument.

For more information, see Where filter.

Instance methods (called on a single model instance):

Performing bulk updates

REVIEW COMMENT from Rand
Describe why you would perform bulk updates. Used with sync, for example.</div>

Deleting data

Static methods (called on the Model object):

Important:

The where clause with destroyAll() is slightly different than that for queries. Omit { where : ... } from the where clause. Simply provide the condition as the first argument.

For more information, see Where filter.


AltStyle によって変換されたページ (->オリジナル) /