store<K> method

  1. @override
Future<K> store<K>(
  1. dynamic entity
)
override

Stores an entity in this Box. The entity is persisted if this Box implementation is persistent.

Implementation

@override
Future<K> store<K>(dynamic entity) => _autoRecover(() async {
      var entitySupport = registry.lookup(entity.runtimeType);
      var document =
          _wrapKey(entitySupport.serialize(entity), entitySupport.keyFields);
      var collection = await _collectionFor(entity.runtimeType);
      var result = await collection.replaceOne(
        {'_id': document['_id']},
        document,
        upsert: true,
      );
      if (result.isFailure) {
        throw StateError('Failed to upsert: ${result.writeError!.errmsg}');
      }
      var id = document['_id'];
      return (id is ObjectId ? id.toHexString() : id) as K;
    });