"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(awsRegion) {
        // 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