import * as DynamoDB from 'aws-sdk/clients/dynamodb';
import { from } from 'rxjs';
import { switchMap } from 'rxjs/operators';
import { dynamoEasyConfig } from '../config/dynamo-easy-config';
/**
 * Simply brings the standard dynamodb operations into the rx world by wrapping the node callbacks into observables
 */
var DynamoRx = /** @class */ (function () {
    function DynamoRx(awsRegion) {
        // create the actual dynamo db client
        this.dynamoDb = new DynamoDB();
    }
    DynamoRx.prototype.updateAwsConfigCredentials = function (newConfig) {
        this.dynamoDb.config.update({ credentials: newConfig.credentials });
    };
    /*
     * make all the dynamo requests return an observable
     */
    DynamoRx.prototype.putItem = function (params) {
        var _this = this;
        return dynamoEasyConfig.sessionValidityEnsurer()
            .pipe(switchMap(function () { return from(_this.dynamoDb.putItem(params).promise()); }));
    };
    DynamoRx.prototype.getItem = function (params) {
        var _this = this;
        return dynamoEasyConfig.sessionValidityEnsurer()
            .pipe(switchMap(function () { return from(_this.dynamoDb.getItem(params).promise()); }));
    };
    DynamoRx.prototype.updateItem = function (params) {
        var _this = this;
        return dynamoEasyConfig.sessionValidityEnsurer()
            .pipe(switchMap(function () { return from(_this.dynamoDb.updateItem(params).promise()); }));
    };
    DynamoRx.prototype.deleteItem = function (params) {
        var _this = this;
        return dynamoEasyConfig.sessionValidityEnsurer()
            .pipe(switchMap(function () { return from(_this.dynamoDb.deleteItem(params).promise()); }));
    };
    DynamoRx.prototype.batchWriteItem = function (params) {
        var _this = this;
        return dynamoEasyConfig.sessionValidityEnsurer()
            .pipe(switchMap(function () { return from(_this.dynamoDb.batchWriteItem(params).promise()); }));
    };
    DynamoRx.prototype.batchGetItems = function (params) {
        var _this = this;
        return dynamoEasyConfig.sessionValidityEnsurer()
            .pipe(switchMap(function () { return from(_this.dynamoDb.batchGetItem(params).promise()); }));
    };
    DynamoRx.prototype.transactWriteItems = function (params) {
        var _this = this;
        return dynamoEasyConfig.sessionValidityEnsurer()
            .pipe(switchMap(function () { return from(_this.dynamoDb.transactWriteItems(params).promise()); }));
    };
    DynamoRx.prototype.transactGetItems = function (params) {
        var _this = this;
        return dynamoEasyConfig.sessionValidityEnsurer()
            .pipe(switchMap(function () { return from(_this.dynamoDb.transactGetItems(params).promise()); }));
    };
    DynamoRx.prototype.scan = function (params) {
        var _this = this;
        return dynamoEasyConfig.sessionValidityEnsurer()
            .pipe(switchMap(function () { return from(_this.dynamoDb.scan(params).promise()); }));
    };
    DynamoRx.prototype.query = function (params) {
        var _this = this;
        if (!params.KeyConditionExpression) {
            throw new Error('key condition expression must be defined');
        }
        return dynamoEasyConfig.sessionValidityEnsurer()
            .pipe(switchMap(function () { return from(_this.dynamoDb.query(params).promise()); }));
    };
    DynamoRx.prototype.makeRequest = function (operation, params) {
        var _this = this;
        return dynamoEasyConfig.sessionValidityEnsurer()
            .pipe(switchMap(function () { return from(_this.dynamoDb.makeRequest(operation, params).promise()); }));
    };
    return DynamoRx;
}());
export { DynamoRx };
//# sourceMappingURL=dynamo-rx.js.map