dynamodb-toolbox
Version:
Lightweight and type-safe query builder for DynamoDB and TypeScript.
103 lines (102 loc) • 4.45 kB
JavaScript
import { BatchDeleteRequest } from '../../../entity/actions/batchDelete/batchDeleteRequest.js';
import { BatchGetRequest } from '../../../entity/actions/batchGet/batchGetRequest.js';
import { BatchPutRequest } from '../../../entity/actions/batchPut/batchPutRequest.js';
import { DynamoDBToolboxError } from '../../../errors/index.js';
import { AccessPattern } from '../../../table/actions/accessPattern/index.js';
import { BatchGetCommand, execute as executeBatchGet } from '../../../table/actions/batchGet/index.js';
import { BatchWriteCommand, execute as executeBatchWrite } from '../../../table/actions/batchWrite/index.js';
import { DeletePartitionCommand } from '../../../table/actions/deletePartition/index.js';
import { PrimaryKeyParser } from '../../../table/actions/parsePrimaryKey/index.js';
import { QueryCommand } from '../../../table/actions/query/index.js';
import { ScanCommand } from '../../../table/actions/scan/index.js';
import { $entities, TableAction } from '../../../table/index.js';
export class TableRepository extends TableAction {
constructor(table, entities = []) {
super(table, entities);
}
entities(...nextEntities) {
return new TableRepository(this.table, nextEntities);
}
parsePrimaryKey(keyInput) {
return new PrimaryKeyParser(this.table).parse(keyInput);
}
async scan(options = {}) {
return new ScanCommand(this.table, this[$entities], options).send();
}
async query(query, options = {}) {
return new QueryCommand(this.table, this[$entities], query, options).send();
}
async deletePartition(query, options = {}) {
return new DeletePartitionCommand(this.table, this[$entities], query, options).send();
}
static executeBatchGet(...commands) {
return executeBatchGet(...commands);
}
batchGet(..._requests) {
const [headRequestOrOptions = {}, ...tailRequests] = _requests;
const requests = tailRequests;
let options = {};
if (isBatchGetRequest(headRequestOrOptions)) {
requests.unshift(headRequestOrOptions);
}
else {
options = headRequestOrOptions;
}
const firstRequest = requests[0];
if (firstRequest === undefined) {
throw new DynamoDBToolboxError('actions.incompleteAction', {
message: 'batchGet incomplete: No BatchGetRequest supplied'
});
}
const entities = [];
const entityNames = new Set();
for (const request of requests) {
if (entityNames.has(request.entity.entityName)) {
continue;
}
entities.push(request.entity);
entityNames.add(request.entity.entityName);
}
return new BatchGetCommand(this.table, entities, requests, options);
}
static executeBatchWrite(...commands) {
return executeBatchWrite(...commands);
}
batchWrite(..._requests) {
const [headRequestOrOptions = {}, ...tailRequests] = _requests;
const requests = tailRequests;
let options = {};
if (isBatchWriteRequest(headRequestOrOptions)) {
requests.unshift(headRequestOrOptions);
}
else {
options = headRequestOrOptions;
}
const firstRequest = requests[0];
if (firstRequest === undefined) {
throw new DynamoDBToolboxError('actions.incompleteAction', {
message: 'batchGet incomplete: No BatchWriteRequest supplied'
});
}
const entities = [];
const entityNames = new Set();
for (const request of requests) {
if (entityNames.has(request.entity.entityName)) {
continue;
}
entities.push(request.entity);
entityNames.add(request.entity.entityName);
}
return new BatchWriteCommand(this.table, entities, requests, options);
}
accessPattern(schema, pattern, options = {}) {
return new AccessPattern(this.table, this[$entities], schema,
/**
* @debt v3 "put query in a 'query' key so it's not polluted by the options"
*/
pattern, options);
}
}
TableRepository.actionName = 'repository';
const isBatchGetRequest = (input) => input instanceof BatchGetRequest;
const isBatchWriteRequest = (input) => input instanceof BatchPutRequest || input instanceof BatchDeleteRequest;