insert method

Future<void> insert(
  1. DocumentData data, [
  2. TransactionId? transactionId
])

Inserts the document with the given data. If the document already exists, the operation will be treated as upsert. The insert will be reflected optimistically locally and will be applied to the server later. If a transactionId is provided, the insert will be applied to the server as an atomic operation together with the rest of the operations in the transaction and the insert will not reflect locally until the transaction is completed locally.

The returned Future will complete once the insert has been applied to the server or immediately if the insert is part of a transaction.

Implementation

Future<void> insert(DocumentData data, [TransactionId? transactionId]) async {
  final squidDocIdObj = parseSquidDocId(squidDocId);
  final integrationId = squidDocIdObj.integrationId;

  /// Exclude the generated client id from the mutation properties.
  Map<String, dynamic> docIdProps = json.decode(squidDocIdObj.docId);
  if (docIdProps['__squidId'] != null) docIdProps = {};

  /// Destructure composite __ids for the built_in_db.
  if (integrationId == defaultBuiltInDb && docIdProps['__id'] != null) {
    try {
      final idProps = json.decode(docIdProps['__id']);
      docIdProps = {...docIdProps, ...idProps};
    } catch (e) {
      /// ignore
    }
  }

  return _dataManager.applyOutgoingMutation(
      Mutation.insert(squidDocIdObj: squidDocIdObj, properties: {
        ...data,
        '__docId__': squidDocIdObj.docId,
        ...docIdProps
      }),
      transactionId);
}