"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const DynamoDB = require("aws-sdk/clients/dynamodb");
const rxjs_1 = require("rxjs");
const operators_1 = require("rxjs/operators");
const dynamo_easy_config_1 = require("../config/dynamo-easy-config");
/**
 * Simply brings the standard dynamoDB operations into the rx world by wrapping the node callbacks into observables
 */
class DynamoRx {
    constructor() {
        // create the actual dynamo db client
        this.dynamoDB = new DynamoDB();
    }
    updateAwsConfigCredentials(newConfig) {
        this.dynamoDB.config.update({ credentials: newConfig.credentials });
    }
    /*
     * make all the dynamo requests return an observable
     */
    putItem(params) {
        return dynamo_easy_config_1.dynamoEasyConfig
            .sessionValidityEnsurer()
            .pipe(operators_1.switchMap(() => rxjs_1.from(this.dynamoDB.putItem(params).promise())));
    }
    getItem(params) {
        return dynamo_easy_config_1.dynamoEasyConfig
            .sessionValidityEnsurer()
            .pipe(operators_1.switchMap(() => rxjs_1.from(this.dynamoDB.getItem(params).promise())));
    }
    updateItem(params) {
        return dynamo_easy_config_1.dynamoEasyConfig
            .sessionValidityEnsurer()
            .pipe(operators_1.switchMap(() => rxjs_1.from(this.dynamoDB.updateItem(params).promise())));
    }
    deleteItem(params) {
        return dynamo_easy_config_1.dynamoEasyConfig
            .sessionValidityEnsurer()
            .pipe(operators_1.switchMap(() => rxjs_1.from(this.dynamoDB.deleteItem(params).promise())));
    }
    batchWriteItem(params) {
        return dynamo_easy_config_1.dynamoEasyConfig
            .sessionValidityEnsurer()
            .pipe(operators_1.switchMap(() => rxjs_1.from(this.dynamoDB.batchWriteItem(params).promise())));
    }
    batchGetItems(params) {
        return dynamo_easy_config_1.dynamoEasyConfig
            .sessionValidityEnsurer()
            .pipe(operators_1.switchMap(() => rxjs_1.from(this.dynamoDB.batchGetItem(params).promise())));
    }
    transactWriteItems(params) {
        return dynamo_easy_config_1.dynamoEasyConfig
            .sessionValidityEnsurer()
            .pipe(operators_1.switchMap(() => rxjs_1.from(this.dynamoDB.transactWriteItems(params).promise())));
    }
    transactGetItems(params) {
        return dynamo_easy_config_1.dynamoEasyConfig
            .sessionValidityEnsurer()
            .pipe(operators_1.switchMap(() => rxjs_1.from(this.dynamoDB.transactGetItems(params).promise())));
    }
    scan(params) {
        return dynamo_easy_config_1.dynamoEasyConfig.sessionValidityEnsurer().pipe(operators_1.switchMap(() => rxjs_1.from(this.dynamoDB.scan(params).promise())));
    }
    query(params) {
        if (!params.KeyConditionExpression) {
            throw new Error('key condition expression must be defined');
        }
        return dynamo_easy_config_1.dynamoEasyConfig.sessionValidityEnsurer().pipe(operators_1.switchMap(() => rxjs_1.from(this.dynamoDB.query(params).promise())));
    }
    makeRequest(operation, params) {
        return dynamo_easy_config_1.dynamoEasyConfig
            .sessionValidityEnsurer()
            .pipe(operators_1.switchMap(() => rxjs_1.from(this.dynamoDB.makeRequest(operation, params).promise())));
    }
}
exports.DynamoRx = DynamoRx;
//# sourceMappingURL=dynamo-rx.js.map