dynamodb-toolbox
Version:
Lightweight and type-safe query builder for DynamoDB and TypeScript.
47 lines (46 loc) • 1.94 kB
JavaScript
import { EntityParser } from '../../../entity/actions/parse/index.js';
import { DynamoDBToolboxError } from '../../../errors/index.js';
import { WriteTransaction } from '../transactWrite/transaction.js';
import { $condition, $key, $options } from './constants.js';
import { parseOptions } from './options.js';
export class ConditionCheck extends WriteTransaction {
constructor(entity, key, condition, options = {}) {
super(entity);
this[$key] = key;
this[$condition] = condition;
this[$options] = options;
}
key(nextKey) {
return new ConditionCheck(this.entity, nextKey, this[$condition], this[$options]);
}
condition(nextCondition) {
return new ConditionCheck(this.entity, this[$key], nextCondition, this[$options]);
}
options(nextOptions) {
return new ConditionCheck(this.entity, this[$key], this[$condition], typeof nextOptions === 'function' ? nextOptions(this[$options]) : nextOptions);
}
params() {
var _a;
if (!this[$key]) {
throw new DynamoDBToolboxError('actions.incompleteAction', {
message: 'ConditionCheck incomplete: Missing "key" property'
});
}
if (!this[$condition]) {
throw new DynamoDBToolboxError('actions.incompleteAction', {
message: 'ConditionCheck incomplete: Missing "condition" property'
});
}
const options = this[$options];
const { key } = this.entity.build(EntityParser).parse(this[$key], { mode: 'key' });
const awsOptions = parseOptions(this.entity, this[$condition], options);
return {
ConditionCheck: {
TableName: (_a = options.tableName) !== null && _a !== void 0 ? _a : this.entity.table.getName(),
Key: key,
...awsOptions
}
};
}
}
ConditionCheck.actionName = 'conditionCheck';