import { tap } from 'rxjs/operators';
import { metadataForClass } from '../decorator/metadata';
import { createLogger } from '../logger/logger';
import { DEFAULT_SESSION_VALIDITY_ENSURER } from './default-session-validity-ensurer.const';
import { DEFAULT_TABLE_NAME_RESOLVER } from './default-table-name-resolver.const';
import { DynamoRx } from './dynamo-rx';
import { BatchGetSingleTableRequest, DeleteRequest, GetRequest, PutRequest, QueryRequest, ScanRequest, UpdateRequest, } from './request';
import { BatchWriteSingleTableRequest } from './request/batchwritesingletable/batch-write-single-table.request';
import { REGEX_TABLE_NAME } from './request/regex';
export class DynamoStore {
    constructor(modelClazz, tableNameResolver = DEFAULT_TABLE_NAME_RESOLVER, sessionValidityEnsurer = DEFAULT_SESSION_VALIDITY_ENSURER) {
        this.modelClazz = modelClazz;
        this.logger = createLogger('dynamo.DynamoStore', modelClazz);
        this.dynamoRx = new DynamoRx(sessionValidityEnsurer);
        const tableName = tableNameResolver(metadataForClass(this.modelClazz).modelOptions.tableName);
        if (!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 PutRequest(this.dynamoRx, this.modelClazz, this.tableName, item);
    }
    get(partitionKey, sortKey) {
        return new GetRequest(this.dynamoRx, this.modelClazz, this.tableName, partitionKey, sortKey);
    }
    update(partitionKey, sortKey) {
        return new UpdateRequest(this.dynamoRx, this.modelClazz, this.tableName, partitionKey, sortKey);
    }
    delete(partitionKey, sortKey) {
        return new 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 BatchWriteSingleTableRequest(this.dynamoRx, this.modelClazz, this.tableName);
    }
    scan() {
        return new ScanRequest(this.dynamoRx, this.modelClazz, this.tableName);
    }
    query() {
        return new 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 BatchGetSingleTableRequest(this.dynamoRx, this.modelClazz, this.tableName, keys);
    }
    makeRequest(operation, params) {
        this.logger.debug('request', params);
        return this.dynamoRx.makeRequest(operation, params).pipe(tap(response => this.logger.debug('response', response)));
    }
}
//# sourceMappingURL=dynamo-store.js.map