"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const operators_1 = require("rxjs/operators");
const metadata_1 = require("../decorator/metadata");
const logger_1 = require("../logger/logger");
const default_session_validity_ensurer_const_1 = require("./default-session-validity-ensurer.const");
const default_table_name_resolver_const_1 = require("./default-table-name-resolver.const");
const dynamo_rx_1 = require("./dynamo-rx");
const request_1 = require("./request");
const batch_write_single_table_request_1 = require("./request/batchwritesingletable/batch-write-single-table.request");
const regex_1 = require("./request/regex");
class DynamoStore {
    constructor(modelClazz, tableNameResolver = default_table_name_resolver_const_1.DEFAULT_TABLE_NAME_RESOLVER, sessionValidityEnsurer = default_session_validity_ensurer_const_1.DEFAULT_SESSION_VALIDITY_ENSURER) {
        this.modelClazz = modelClazz;
        this.logger = logger_1.createLogger('dynamo.DynamoStore', modelClazz);
        this.dynamoRx = new dynamo_rx_1.DynamoRx(sessionValidityEnsurer);
        const tableName = tableNameResolver(metadata_1.metadataForClass(this.modelClazz).modelOptions.tableName);
        if (!regex_1.REGEX_TABLE_NAME.test(tableName)) {
            throw new Error('make sure the table name only contains these characters «a-z A-Z 0-9 - _ .» and is between 3 and 255 characters long');
        }
        this.tableName = tableName;
        this.logger.debug('instance created');
    }
    get dynamoDb() {
        return this.dynamoRx.dynamoDb;
    }
    put(item) {
        return new request_1.PutRequest(this.dynamoRx, this.modelClazz, this.tableName, item);
    }
    get(partitionKey, sortKey) {
        return new request_1.GetRequest(this.dynamoRx, this.modelClazz, this.tableName, partitionKey, sortKey);
    }
    update(partitionKey, sortKey) {
        return new request_1.UpdateRequest(this.dynamoRx, this.modelClazz, this.tableName, partitionKey, sortKey);
    }
    delete(partitionKey, sortKey) {
        return new request_1.DeleteRequest(this.dynamoRx, this.modelClazz, this.tableName, partitionKey, sortKey);
    }
    /**
     * This is a special implementation of batchWriteItem request, because it only supports one table,
     * if you wish to write items to multiple tables
     * create an instance of BatchWriteItemInput and use store.makeRequest with it.
     */
    batchWrite() {
        return new batch_write_single_table_request_1.BatchWriteSingleTableRequest(this.dynamoRx, this.modelClazz, this.tableName);
    }
    scan() {
        return new request_1.ScanRequest(this.dynamoRx, this.modelClazz, this.tableName);
    }
    query() {
        return new request_1.QueryRequest(this.dynamoRx, this.modelClazz, this.tableName);
    }
    /**
     * This is a special implementation of BatchGetItem request, because it only supports one table,
     * if you wish to retrieve items from multiple tables
     * create an instance of BatchGetItemInput and use store.makeRequest with it.
     */
    batchGetItem(keys) {
        return new request_1.BatchGetSingleTableRequest(this.dynamoRx, this.modelClazz, this.tableName, keys);
    }
    makeRequest(operation, params) {
        this.logger.debug('request', params);
        return this.dynamoRx.makeRequest(operation, params).pipe(operators_1.tap(response => this.logger.debug('response', response)));
    }
}
exports.DynamoStore = DynamoStore;
//# sourceMappingURL=dynamo-store.js.map