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