import { map, tap } from 'rxjs/operators';
import { createLogger } from '../../../logger/logger';
import { toDb } from '../../../mapper';
import { attribute } from '../../expression/logical-operator';
import { WriteRequest } from '../write.request';
export class PutRequest extends WriteRequest {
    constructor(dynamoRx, modelClazz, tableName, item) {
        super(dynamoRx, modelClazz, tableName);
        this.logger = createLogger('dynamo.request.PutRequest', modelClazz);
        this.params.Item = toDb(item, this.modelClazz);
    }
    /**
     * Adds a condition expression to the request, which makes sure the item will only be saved if the id does not exist
     * @returns {PutRequest<T>}
     */
    ifNotExists(predicate) {
        // FIXME should we check for sort key too?
        const conditionDefFns = [];
        if (predicate === undefined || (predicate !== undefined && predicate === true)) {
            conditionDefFns.push(attribute(this.metadata.getPartitionKey()).attributeNotExists());
            const sortKey = this.metadata.getSortKey();
            if (sortKey !== null) {
                conditionDefFns.push(attribute(sortKey).attributeNotExists());
            }
            this.onlyIf(...conditionDefFns);
        }
        return this;
    }
    execFullResponse() {
        this.logger.debug('request', this.params);
        return this.dynamoRx.putItem(this.params).pipe(tap(response => this.logger.debug('response', response)));
    }
    exec() {
        return this.execFullResponse().pipe(map(response => {
            return;
        }));
    }
}
//# sourceMappingURL=put.request.js.map