put method

int put(
  1. T object, {
  2. PutMode mode = PutMode.put,
})

Puts the given object and returns its (new) ID.

This means that if its Id property is 0 or null, it is inserted as a new object and assigned the next available ID. For example, if there is an object with ID 1 and another with ID 100, it will be assigned ID 101. The new ID is also set on the given object before this returns.

If instead the object has an assigned ID set, if an object with the same ID exists it is updated. Otherwise, it is inserted with that ID.

If the ID was not assigned before a StorageException is thrown.

When the object contains ToOne or ToMany relations, they are created (or updated) to point to the (new) target objects. The target objects themselves are typically not updated or removed. To do so, put or remove them using their Box. However, for convenience, if a target object is new, it will be inserted and assigned an ID in its Box before creating or updating the relation. Also, for ToMany relations based on a Backlink from a ToOne, the target objects are updated (to store changes in the linked ToOne relation).

Change mode to specify explicitly that only an insert or update should occur.

See putMany to put several objects at once with better performance.

See putAsync for an asynchronous version.

Implementation

int put(T object, {PutMode mode = PutMode.put}) {
  if (_hasRelations) {
    return InternalStoreAccess.runInTransaction(
        _store, TxMode.write, (Transaction tx) => _put(object, mode, tx));
  } else {
    return _put(object, mode, null);
  }
}