item()

in components/base/packages/api-testing-framework/support/aws/services/db-helpers/updater.js [164:223]


  item(item) {
    if (!item) return this;

    // we loop through all the properties that are defined and add them to the
    // update expression and to the expression values and that same time detect if they are marked as sets
    const keys = Object.keys(item);
    if (keys.length === 0) return this;

    const assignments = [];
    const values = {};
    const names = {};

    keys.forEach((key) => {
      const value = item[key];
      if (value === undefined) return;
      if (this.params.Key && this.params.Key.hasOwnProperty(key)) return; // eslint-disable-line no-prototype-builtins

      if (this.createdAtState.enabled && key === 'createdAt') return;
      if (this.updatedAtState.enabled && key === 'updatedAt') return;
      if (this.internals.revGiven && key === 'rev') return;

      names[`#${key}`] = key;
      assignments.push(`#${key} = :${key}`);

      if (this.marked[key] && _.isEmpty(value)) {
        values[`:${key}`] = null;
      } else if (this.marked[key]) {
        values[`:${key}`] = this.client.createSet(value, { validate: true });
      } else {
        values[`:${key}`] = value;
      }
    });

    if (assignments.length === 0) return this;

    this.internals.set.push(assignments.join(', '));

    let createdAt = this.createdAtState.value;
    if (this.createdAtState.enabled && !this.createdAtState.processed) {
      this.createdAtState.processed = true;
      createdAt = _.isEmpty(createdAt) ? new Date().toISOString() : createdAt;
      this.internals.set.push('#createdAt = if_not_exists(#createdAt, :createdAt)');
      names['#createdAt'] = 'createdAt';
      values[':createdAt'] = createdAt;
    }

    let updatedAt = this.updatedAtState.value;
    if (this.updatedAtState.enabled && !this.updatedAtState.processed) {
      this.updatedAtState.processed = true;
      updatedAt = _.isEmpty(updatedAt) ? new Date().toISOString() : updatedAt;
      this.internals.set.push('#updatedAt = :updatedAt');
      names['#updatedAt'] = 'updatedAt';
      values[':updatedAt'] = updatedAt;
    }

    this.names(names);
    this.values(values);

    return this;
  }