mutateIn method

Future<MutateInResult> mutateIn(
  1. String key,
  2. List<MutateInSpec> specs, [
  3. MutateInOptions? options
])

Performs a Mutate-In operation against a document.

Allows atomic modification of specific fields within a document. Also enables access to document extended-attributes.

key is the key of the document to mutate.

specs is a list of MutateInSpecs describing the operations to perform

Implementation

Future<MutateInResult> mutateIn(
  String key,
  List<MutateInSpec> specs, [
  MutateInOptions? options,
]) async {
  options ??= const MutateInOptions();

  if (specs.isEmpty) {
    throw ArgumentError.value(
      specs,
      'specs',
      'must not be empty',
    );
  }

  final id = _documentId(key);
  final expiry = options.expiry ?? Duration.zero;
  final cas = options.cas ?? InternalCas.zero;
  final timeout = _mutationTimeout(options);
  final implSpecs = List.generate(
    specs.length,
    (index) => specs[index].toMessage(index),
  );

  final response = options.usesLegacyDurability
      ? await _connection.mutateInWithLegacyDurability(
          MutateInWithLegacyDurability(
            id: id,
            cas: cas,
            specs: implSpecs,
            storeSemantics: options.storeSemantics,
            timeout: timeout,
            expiry: expiry.inSeconds,
            preserveExpiry: options.preserveExpiry,
            persistTo: options.durabilityPersistTo,
            replicateTo: options.durabilityReplicateTo,
            partition: 0,
            opaque: 0,
            accessDeleted: false,
            createAsDeleted: false,
          ),
        )
      : await _connection.mutateIn(
          MutateInRequest(
            id: id,
            cas: cas,
            specs: implSpecs,
            storeSemantics: options.storeSemantics,
            timeout: timeout,
            expiry: expiry.inSeconds,
            preserveExpiry: options.preserveExpiry,
            durabilityLevel: options.durabilityLevel,
            partition: 0,
            opaque: 0,
            accessDeleted: false,
            createAsDeleted: false,
          ),
        );

  final results = response.fields.map((entry) {
    return MutateInResultEntry(
      value:
          entry.value.isEmpty ? null : _decodeSubDocumentValue(entry.value),
    );
  }).toList();

  return MutateInResult(
    content: results,
    cas: response.cas,
    token: response.token,
  );
}