public async updateDocument()

in src/Explorer/Tables/TableDataClient.ts [184:259]


  public async updateDocument(
    collection: ViewModels.Collection,
    originalDocument: any,
    newEntity: Entities.ITableEntity,
  ): Promise<Entities.ITableEntity> {
    const clearMessage = NotificationConsoleUtils.logConsoleProgress(`Updating row ${originalDocument.RowKey._}`);

    try {
      let whereSegment = " WHERE";
      let keys: CassandraTableKey[] = collection.cassandraKeys.partitionKeys.concat(
        collection.cassandraKeys.clusteringKeys,
      );
      for (let keyIndex in keys) {
        const key = keys[keyIndex].property;
        const keyType = keys[keyIndex].type;
        whereSegment += this.isStringType(keyType)
          ? ` ${key} = '${newEntity[key]._}' AND`
          : ` ${key} = ${newEntity[key]._} AND`;
      }
      whereSegment = whereSegment.slice(0, whereSegment.length - 4);

      let updateQuery = `UPDATE ${collection.databaseId}.${collection.id()}`;
      let isPropertyUpdated = false;
      let isFirstPropertyToUpdate = true;
      for (let property in newEntity) {
        if (
          !originalDocument[property] ||
          newEntity[property]._.toString() !== originalDocument[property]._.toString()
        ) {
          if (newEntity[property]._.toString() === "" || newEntity[property]._ === undefined) {
            continue;
          }
          let propertyQuerySegment = this.isStringType(newEntity[property].$)
            ? `${property} = '${newEntity[property]._}',`
            : `${property} = ${newEntity[property]._},`;
          // Only add the "SET" keyword once
          if (isFirstPropertyToUpdate) {
            propertyQuerySegment = " SET " + propertyQuerySegment;
            isFirstPropertyToUpdate = false;
          }
          updateQuery += propertyQuerySegment;
          isPropertyUpdated = true;
        }
      }

      if (isPropertyUpdated) {
        updateQuery = updateQuery.slice(0, updateQuery.length - 1);
        updateQuery += whereSegment;
        await this.queryDocuments(collection, updateQuery);
      }

      let deleteQuery = `DELETE `;
      let isPropertyDeleted = false;
      for (let property in originalDocument) {
        if (property !== TableConstants.EntityKeyNames.RowKey && !newEntity[property] && !!originalDocument[property]) {
          deleteQuery += ` ${property},`;
          isPropertyDeleted = true;
        }
      }

      if (isPropertyDeleted) {
        deleteQuery = deleteQuery.slice(0, deleteQuery.length - 1);
        deleteQuery += ` FROM ${collection.databaseId}.${collection.id()}${whereSegment}`;
        await this.queryDocuments(collection, deleteQuery);
      }

      newEntity[TableConstants.EntityKeyNames.RowKey] = originalDocument[TableConstants.EntityKeyNames.RowKey];
      NotificationConsoleUtils.logConsoleInfo(`Successfully updated row ${newEntity.RowKey._}`);
      return newEntity;
    } catch (error) {
      handleError(error, "UpdateRowCassandra", "Failed to update row ${newEntity.RowKey._}");
      throw error;
    } finally {
      clearMessage();
    }
  }