public async insertEntity()

in src/table/handlers/TableHandler.ts [178:296]


  public async insertEntity(
    _tableName: string,
    options: Models.TableInsertEntityOptionalParams,
    context: Context
  ): Promise<Models.TableInsertEntityResponse> {
    const tableContext = new TableStorageContext(context);
    const account = this.getAndCheckAccountName(tableContext);
    const table = this.getAndCheckTableName(tableContext);
    const accept = this.getAndCheckPayloadFormat(tableContext);
    const prefer = this.getAndCheckPreferHeader(tableContext);
    this.checkBodyLimit(context, context.request?.getBody());

    // currently unable to use checking functions as the partitionKey
    // and rowKey are not coming through the context.
    // const partitionKey = this.getAndCheckPartitionKey(tableContext);
    // const rowKey = this.getAndCheckRowKey(tableContext);
    if (
      options.tableEntityProperties == undefined ||
      !options.tableEntityProperties ||
      // rowKey and partitionKey may be empty string
      options.tableEntityProperties.PartitionKey === null ||
      options.tableEntityProperties.PartitionKey == undefined ||
      options.tableEntityProperties.RowKey === null ||
      options.tableEntityProperties.RowKey === undefined
    ) {
      throw StorageErrorFactory.getPropertiesNeedValue(context);
    }

    // check that key properties are valid
    this.validateKey(context, options.tableEntityProperties.PartitionKey);
    this.validateKey(context, options.tableEntityProperties.RowKey);

    this.checkProperties(context, options.tableEntityProperties);

    // need to remove the etags from the properties to avoid errors
    // https://docs.microsoft.com/en-us/rest/api/storageservices/insert-entity
    options.tableEntityProperties = this.removeEtagProperty(
      options.tableEntityProperties
    );

    const entity: Entity = this.createPersistedEntity(
      context,
      options,
      options.tableEntityProperties?.PartitionKey,
      options.tableEntityProperties?.RowKey
    );
    let normalizedEntity;
    try {
      normalizedEntity = new NormalizedEntity(entity);
      normalizedEntity.normalize();
    } catch (e: any) {
      this.logger.error(
        `TableHandler:insertEntity() ${e.name} ${JSON.stringify(e.stack)}`,
        context.contextID
      );
      throw StorageErrorFactory.getInvalidInput(context);
    }

    await this.metadataStore.insertTableEntity(
      context,
      table,
      account,
      entity,
      tableContext.batchId
    );

    const response: Models.TableInsertEntityResponse = {
      clientRequestId: options.requestId,
      requestId: tableContext.contextID,
      version: TABLE_API_VERSION,
      date: context.startTime,
      statusCode: 201,
      eTag: entity.eTag
    };

    if (prefer === RETURN_CONTENT || prefer === undefined) {
      const body = {} as any;
      const annotation = getEntityOdataAnnotationsForResponse(
        account,
        table,
        this.getOdataAnnotationUrlPrefix(tableContext, account),
        options.tableEntityProperties?.PartitionKey,
        options.tableEntityProperties?.RowKey,
        accept
      );

      if (accept === MINIMAL_METADATA_ACCEPT) {
        body["odata.metadata"] = annotation.odatametadata;
        body["odata.etag"] = entity.eTag;
      }

      if (accept === FULL_METADATA_ACCEPT) {
        body["odata.metadata"] = annotation.odatametadata;
        body["odata.type"] = annotation.odatatype;
        body["odata.id"] = annotation.odataid;
        body["odata.etag"] = entity.eTag;
        body["odata.editLink"] = annotation.odataeditLink;
      }

      // for (const key of Object.keys(entity.properties)) {
      //   body[key] = entity.properties[key];
      // }

      // response.body = new BufferStream(Buffer.from(JSON.stringify(body)));
      const rawResponse = normalizedEntity.toResponseString(accept, body);
      this.logger.debug(
        `TableHandler:insertEntity() Raw response string is ${JSON.stringify(
          rawResponse
        )}`,
        context.contextID
      );
      response.body = new BufferStream(Buffer.from(rawResponse));
    }

    this.updateResponseAccept(tableContext, accept);
    this.updateResponsePrefer(response, tableContext);

    return response;
  }