Skip to content

Commit

Permalink
fix(core): allow updating non-key attributes with primary key attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
whimzyLive committed Jan 26, 2022
1 parent 0e72772 commit 4b6a7ec
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 47 deletions.
1 change: 0 additions & 1 deletion packages/common/src/error/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,5 @@ export * from './no-such-entity-exists-error';
export * from './missing-required-entity-physical-name-error';
export * from './duplicate-entity-physical-name-error';
export * from './invalid-unique-attribute-update-error';
export * from './invalid-primary-key-attributes-update-error';
export * from './invalid-parallel-scan-limit-option-error';
export * from './invalid-dynamic-update-attribute-value';

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {LazyTransactionWriteItemListLoader} from './../is-lazy-transaction-write-item-list-loader';
import {
Attribute,
Entity,
InvalidPrimaryKeyAttributesUpdateError,
InvalidUniqueAttributeUpdateError,
QUERY_ORDER,
} from '@typedorm/common';
Expand Down Expand Up @@ -935,21 +935,63 @@ test('throws when trying to update primary key attribute and unique attribute in
expect(updateItem).toThrow(InvalidUniqueAttributeUpdateError);
});

test('throws when trying to update primary key attribute and non key attribute in the same request', () => {
const updateItem = () =>
transformer.toDynamoUpdateItem<Photo, PhotoPrimaryKey>(
Photo,
test('allows updating primary key attribute and non key attribute in the same request', () => {
const updateItem = transformer.toDynamoUpdateItem<Photo, PhotoPrimaryKey>(
Photo,
{
category: CATEGORY.PETS,
id: 1,
},
{
id: 2,
createdAt: moment(),
name: 'new name',
}
);

expect(updateItem).toEqual({
entityClass: Photo,
lazyLoadTransactionWriteItems: expect.any(Function),
primaryKeyAttributes: {
category: 'PETS',
id: 1,
},
});

expect(
(updateItem as LazyTransactionWriteItemListLoader).lazyLoadTransactionWriteItems(
{
category: CATEGORY.PETS,
id: 1,
},
{
id: 2,
createdAt: moment(),
category: CATEGORY.KIDS,
name: 'old name',
}
);

expect(updateItem).toThrow(InvalidPrimaryKeyAttributesUpdateError);
)
).toEqual([
{
Put: {
Item: {
GSI1PK: 'PHOTO#2',
SK: 'PHOTO#2',
category: 'KIDS',
createdAt: '2021-06-01',
id: 2,
name: 'new name',
updatedAt: '1622530750',
},
ReturnValues: 'ALL_NEW',
TableName: 'test-table',
},
},
{
Delete: {
Key: {
PK: 'PHOTO#KIDS',
SK: 'PHOTO#1',
},
TableName: 'test-table',
},
},
]);
});

test('transforms update item request with complex condition input', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
InvalidFilterInputError,
InvalidSelectInputError,
InvalidUniqueAttributeUpdateError,
InvalidPrimaryKeyAttributesUpdateError,
InvalidDynamicUpdateAttributeValueError,
} from '@typedorm/common';
import {DynamoDB} from 'aws-sdk';
Expand Down Expand Up @@ -505,28 +504,13 @@ export class DocumentClientRequestTransformer extends BaseTransformer {

// validate primary key attributes
if (!isEmptyObject(affectedPrimaryKeyAttributes)) {
const primaryKeyReferencedAttributes = this.connection.getPrimaryKeyAttributeInterpolationsForEntity(
entityClass
);
const nonKeyAttributesToUpdate = Object.keys(body).filter(
attr => !primaryKeyReferencedAttributes.includes(attr)
);

// updates are not allowed for attributes that unique and also references primary key.
if (uniqueAttributesToUpdate.length) {
throw new InvalidUniqueAttributeUpdateError(
affectedPrimaryKeyAttributes!,
uniqueAttributesToUpdate.map(attr => attr.name)
);
}

// primary key and non key attributes can not be updated together
if (nonKeyAttributesToUpdate.length) {
throw new InvalidPrimaryKeyAttributesUpdateError(
affectedPrimaryKeyAttributes!,
nonKeyAttributesToUpdate
);
}
}

/**
Expand Down

0 comments on commit 4b6a7ec

Please sign in to comment.