-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(transactions): add UpdateItemTransaction
- Loading branch information
Showing
12 changed files
with
2,121 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { PutItemTransaction } from './putItem' | ||
export { DeleteItemTransaction } from './deleteItem' | ||
export { UpdateItemTransaction } from './updateItem' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { UpdateItemTransaction } from './operation' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import type { EntityV2 } from 'v1/entity' | ||
|
||
import { DynamoDBToolboxError } from 'v1/errors' | ||
|
||
import { $entity, EntityOperation } from '../../class' | ||
import type { UpdateItemInput } from '../../updateItem/types' | ||
import { WriteItemTransaction } from '../types' | ||
import { transactUpdateItemParams, TransactUpdateItemParams } from './transactUpdateItemParams' | ||
import { DynamoDBDocumentClient } from '@aws-sdk/lib-dynamodb' | ||
import type { UpdateItemTransactionOptions } from './options' | ||
|
||
export const $item = Symbol('$item') | ||
export type $item = typeof $item | ||
|
||
export const $options = Symbol('$options') | ||
export type $options = typeof $options | ||
|
||
export class UpdateItemTransaction< | ||
ENTITY extends EntityV2 = EntityV2, | ||
OPTIONS extends UpdateItemTransactionOptions<ENTITY> = UpdateItemTransactionOptions<ENTITY> | ||
> | ||
extends EntityOperation<ENTITY> | ||
implements WriteItemTransaction<ENTITY, 'Update'> { | ||
static operationName = 'transactUpdate' as const | ||
|
||
private [$item]?: UpdateItemInput<ENTITY> | ||
public item: (nextItem: UpdateItemInput<ENTITY>) => UpdateItemTransaction<ENTITY> | ||
public [$options]: OPTIONS | ||
public options: <NEXT_OPTIONS extends UpdateItemTransactionOptions<ENTITY>>( | ||
nextOptions: NEXT_OPTIONS | ||
) => UpdateItemTransaction<ENTITY, NEXT_OPTIONS> | ||
|
||
constructor(entity: ENTITY, item?: UpdateItemInput<ENTITY>, options: OPTIONS = {} as OPTIONS) { | ||
super(entity) | ||
this[$item] = item | ||
this[$options] = options | ||
|
||
this.item = nextItem => new UpdateItemTransaction(this[$entity], nextItem, this[$options]) | ||
this.options = nextOptions => new UpdateItemTransaction(this[$entity], this[$item], nextOptions) | ||
} | ||
|
||
params = (): TransactUpdateItemParams => { | ||
if (!this[$item]) { | ||
throw new DynamoDBToolboxError('operations.incompleteCommand', { | ||
message: 'UpdateItemTransaction incomplete: Missing "item" property' | ||
}) | ||
} | ||
|
||
return transactUpdateItemParams(this[$entity], this[$item], this[$options]) | ||
} | ||
|
||
get = (): { | ||
documentClient: DynamoDBDocumentClient | ||
type: 'Update' | ||
params: TransactUpdateItemParams | ||
} => ({ | ||
documentClient: this[$entity].table.documentClient, | ||
type: 'Update', | ||
params: this.params() | ||
}) | ||
} | ||
|
||
export type UpdateItemTransactionClass = typeof UpdateItemTransaction |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import type { EntityV2 } from 'v1/entity' | ||
import type { ConditionOptions } from 'v1/operations/types/condition' | ||
|
||
export type UpdateItemTransactionOptions< | ||
ENTITY extends EntityV2 = EntityV2 | ||
> = ConditionOptions<ENTITY> |
2 changes: 2 additions & 0 deletions
2
src/v1/operations/transactions/updateItem/transactUpdateItemParams/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { transactUpdateItemParams } from './transactUpdateItemParams' | ||
export type { TransactUpdateItemParams } from './transactUpdateItemParams' |
33 changes: 33 additions & 0 deletions
33
src/v1/operations/transactions/updateItem/transactUpdateItemParams/parseUpdateItemOptions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import type { EntityV2 } from 'v1/entity' | ||
import { parseCondition } from 'v1/operations/expression/condition/parse' | ||
|
||
import { rejectExtraOptions } from 'v1/operations/utils/parseOptions/rejectExtraOptions' | ||
|
||
import { UpdateItemTransactionOptions } from '../options' | ||
import type { TransactUpdateItemParams } from './transactUpdateItemParams' | ||
|
||
type TransactionOptions = Omit<TransactUpdateItemParams, 'TableName' | 'Key' | 'UpdateExpression'> | ||
|
||
export const parseUpdateItemTransactionOptions = <ENTITY extends EntityV2>( | ||
entity: ENTITY, | ||
putItemTransactionOptions: UpdateItemTransactionOptions<ENTITY> | ||
): TransactionOptions => { | ||
const transactionOptions: TransactionOptions = {} | ||
|
||
const { condition, ...extraOptions } = putItemTransactionOptions | ||
rejectExtraOptions(extraOptions) | ||
|
||
if (condition !== undefined) { | ||
const { | ||
ExpressionAttributeNames, | ||
ExpressionAttributeValues, | ||
ConditionExpression | ||
} = parseCondition(entity, condition) | ||
|
||
transactionOptions.ExpressionAttributeNames = ExpressionAttributeNames | ||
transactionOptions.ExpressionAttributeValues = ExpressionAttributeValues | ||
transactionOptions.ConditionExpression = ConditionExpression | ||
} | ||
|
||
return transactionOptions | ||
} |
27 changes: 27 additions & 0 deletions
27
...perations/transactions/updateItem/transactUpdateItemParams/parseUpdateTransactionInput.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import type { EntityV2 } from 'v1/entity' | ||
import type { Item, RequiredOption } from 'v1/schema' | ||
import { parseSchemaClonedInput } from 'v1/validation/parseClonedInput' | ||
import { UpdateItemInputExtension } from 'v1/operations/types' | ||
import { parseUpdateExtension } from 'v1/operations/updateItem/updateItemParams/extension/parseExtension' | ||
|
||
type EntityUpdateCommandInputParser = ( | ||
entity: EntityV2, | ||
input: Item<UpdateItemInputExtension> | ||
) => Generator<Item<UpdateItemInputExtension>, Item<UpdateItemInputExtension>> | ||
|
||
const requiringOptions = new Set<RequiredOption>(['always']) | ||
|
||
export const parseEntityUpdateTransactionInput: EntityUpdateCommandInputParser = ( | ||
entity, | ||
input | ||
) => { | ||
const parser = parseSchemaClonedInput(entity.schema, input, { | ||
operationName: 'update', | ||
requiringOptions, | ||
parseExtension: parseUpdateExtension | ||
}) | ||
|
||
parser.next() // cloned | ||
|
||
return parser | ||
} |
64 changes: 64 additions & 0 deletions
64
...1/operations/transactions/updateItem/transactUpdateItemParams/transactUpdateItemParams.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import type { TransactWriteCommandInput } from '@aws-sdk/lib-dynamodb' | ||
import isEmpty from 'lodash.isempty' | ||
import omit from 'lodash.omit' | ||
|
||
import type { EntityV2 } from 'v1/entity' | ||
import { parsePrimaryKey } from 'v1/operations/utils/parsePrimaryKey' | ||
|
||
import type { UpdateItemInput } from '../../../updateItem' | ||
import { parseEntityUpdateTransactionInput } from './parseUpdateTransactionInput' | ||
import type { UpdateItemTransactionOptions } from '../options' | ||
|
||
import { parseUpdateItemTransactionOptions } from './parseUpdateItemOptions' | ||
import { parseUpdate } from 'v1/operations/updateItem/updateExpression/parse' | ||
|
||
export type TransactUpdateItemParams = NonNullable< | ||
NonNullable<TransactWriteCommandInput['TransactItems']>[number]['Update'] | ||
> | ||
|
||
export const transactUpdateItemParams = < | ||
ENTITY extends EntityV2, | ||
OPTIONS extends UpdateItemTransactionOptions<ENTITY> | ||
>( | ||
entity: ENTITY, | ||
input: UpdateItemInput<ENTITY>, | ||
updateItemTransactionOptions: OPTIONS = {} as OPTIONS | ||
): TransactUpdateItemParams => { | ||
const validInputParser = parseEntityUpdateTransactionInput(entity, input) | ||
const validInput = validInputParser.next().value | ||
const collapsedInput = validInputParser.next().value | ||
|
||
const keyInput = entity.computeKey ? entity.computeKey(validInput) : collapsedInput | ||
const primaryKey = parsePrimaryKey(entity, keyInput) | ||
|
||
const { | ||
ExpressionAttributeNames: updateExpressionAttributeNames, | ||
ExpressionAttributeValues: updateExpressionAttributeValues, | ||
...update | ||
} = parseUpdate(entity, omit(collapsedInput, Object.keys(primaryKey))) | ||
|
||
const { | ||
ExpressionAttributeNames: optionsExpressionAttributeNames, | ||
ExpressionAttributeValues: optionsExpressionAttributeValues, | ||
...options | ||
} = parseUpdateItemTransactionOptions(entity, updateItemTransactionOptions) | ||
|
||
const ExpressionAttributeNames = { | ||
...optionsExpressionAttributeNames, | ||
...updateExpressionAttributeNames | ||
} | ||
|
||
const ExpressionAttributeValues = { | ||
...optionsExpressionAttributeValues, | ||
...updateExpressionAttributeValues | ||
} | ||
|
||
return { | ||
TableName: entity.table.getName(), | ||
Key: primaryKey, | ||
UpdateExpression: update.UpdateExpression, | ||
...options, | ||
...(!isEmpty(ExpressionAttributeNames) ? { ExpressionAttributeNames } : {}), | ||
...(!isEmpty(ExpressionAttributeValues) ? { ExpressionAttributeValues } : {}) | ||
} | ||
} |
Oops, something went wrong.