"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");
/**
 * Simply brings the standard dynamodb operations into the rx world by wrapping the node callbacks into observables
 */
class DynamoRx {
    constructor(sessionValidityEnsurer, awsRegion) {
        // this.logger = new Logger(() => LogLevel.DEBUG, 'DynamoDbService');
        // create the actual dynamo db client
        this.dynamoDb = new DynamoDB();
        this.sessionValidityEnsurer = sessionValidityEnsurer;
    }
    updateAwsConfigCredentials(newConfig) {
        this.dynamoDb.config.update({ credentials: newConfig.credentials });
    }
    /*
     * make all the dynamo requests return an observable
     */
    putItem(params) {
        return this.sessionValidityEnsurer().pipe(operators_1.switchMap(() => rxjs_1.from(this.dynamoDb.putItem(params).promise())));
    }
    getItem(params) {
        return this.sessionValidityEnsurer().pipe(operators_1.switchMap(() => rxjs_1.from(this.dynamoDb.getItem(params).promise())));
    }
    updateItem(params) {
        return this.sessionValidityEnsurer().pipe(operators_1.switchMap(() => rxjs_1.from(this.dynamoDb.updateItem(params).promise())));
    }
    deleteItem(params) {
        return this.sessionValidityEnsurer().pipe(operators_1.switchMap(() => rxjs_1.from(this.dynamoDb.deleteItem(params).promise())));
    }
    batchWriteItem(params) {
        return this.sessionValidityEnsurer().pipe(operators_1.switchMap(() => rxjs_1.from(this.dynamoDb.batchWriteItem(params).promise())));
    }
    batchGetItems(params) {
        return this.sessionValidityEnsurer().pipe(operators_1.switchMap(() => rxjs_1.from(this.dynamoDb.batchGetItem(params).promise())));
    }
    scan(params) {
        return this.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 this.sessionValidityEnsurer().pipe(operators_1.switchMap(() => rxjs_1.from(this.dynamoDb.query(params).promise())));
    }
    makeRequest(operation, params) {
        return this.sessionValidityEnsurer().pipe(operators_1.switchMap(() => rxjs_1.from(this.dynamoDb.makeRequest(operation, params).promise())));
    }
}
exports.DynamoRx = DynamoRx;
//# sourceMappingURL=dynamo-rx.js.map