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