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
 */
export 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 dynamoEasyConfig.sessionValidityEnsurer()
            .pipe(switchMap(() => from(this.dynamoDb.putItem(params).promise())));
    }
    getItem(params) {
        return dynamoEasyConfig.sessionValidityEnsurer()
            .pipe(switchMap(() => from(this.dynamoDb.getItem(params).promise())));
    }
    updateItem(params) {
        return dynamoEasyConfig.sessionValidityEnsurer()
            .pipe(switchMap(() => from(this.dynamoDb.updateItem(params).promise())));
    }
    deleteItem(params) {
        return dynamoEasyConfig.sessionValidityEnsurer()
            .pipe(switchMap(() => from(this.dynamoDb.deleteItem(params).promise())));
    }
    batchWriteItem(params) {
        return dynamoEasyConfig.sessionValidityEnsurer()
            .pipe(switchMap(() => from(this.dynamoDb.batchWriteItem(params).promise())));
    }
    batchGetItems(params) {
        return dynamoEasyConfig.sessionValidityEnsurer()
            .pipe(switchMap(() => from(this.dynamoDb.batchGetItem(params).promise())));
    }
    transactWriteItems(params) {
        return dynamoEasyConfig.sessionValidityEnsurer()
            .pipe(switchMap(() => from(this.dynamoDb.transactWriteItems(params).promise())));
    }
    transactGetItems(params) {
        return dynamoEasyConfig.sessionValidityEnsurer()
            .pipe(switchMap(() => from(this.dynamoDb.transactGetItems(params).promise())));
    }
    scan(params) {
        return dynamoEasyConfig.sessionValidityEnsurer()
            .pipe(switchMap(() => from(this.dynamoDb.scan(params).promise())));
    }
    query(params) {
        if (!params.KeyConditionExpression) {
            throw new Error('key condition expression must be defined');
        }
        return dynamoEasyConfig.sessionValidityEnsurer()
            .pipe(switchMap(() => from(this.dynamoDb.query(params).promise())));
    }
    makeRequest(operation, params) {
        return dynamoEasyConfig.sessionValidityEnsurer()
            .pipe(switchMap(() => from(this.dynamoDb.makeRequest(operation, params).promise())));
    }
}
//# sourceMappingURL=dynamo-rx.js.map