Skip to content

Commit

Permalink
feat(core): add improved error message for conflicting attribute names
Browse files Browse the repository at this point in the history
  • Loading branch information
whimzyLive committed Jul 8, 2022
1 parent b562f3c commit 1031d40
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
Attribute,
AutoGenerateAttribute,
AUTO_GENERATE_ATTRIBUTE_STRATEGY,
ConflictingAttributeNameError,
} from '@typedorm/common';
import {UserAutoGenerateAttributes} from '../../../../__mocks__/user-auto-generate-attributes';
import {User} from '../../../../__mocks__/user';
Expand Down Expand Up @@ -269,6 +270,5 @@ test('throws an error when attribute name conflicts with a primary key or sort k
attributesMetadataBuilder
.build(mockTable, AdminTest, AdminTest)
.map(obj => Object.assign({}, obj));

expect(metadata).toThrow('dasdasds');
expect(metadata).toThrow(ConflictingAttributeNameError);
});
41 changes: 17 additions & 24 deletions packages/core/src/classes/connection/attribute-metadata-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,30 @@ export class AttributesMetadataBuilder {
decoratedEntityClass
);

// validate attribute names for primary key conflicts
if (
[
entityMetadata.table?.partitionKey,
entityMetadata.table?.usesCompositeKey() &&
entityMetadata.table?.sortKey,
]
.filter(Boolean)
.includes(attr.name)
) {
throw new ConflictingAttributeNameError({
attributeName: attr.name,
entity: entityMetadata.name,
tableName: entityMetadata.table?.name || '',
});
}

if (IsAutoGenerateAttributeRawMetadataOptions(attr)) {
return this.validateAndBuildAutoGeneratedAttributeMetadata(
attr,
entityMetadata
);
}

// if (
// [entityMetadata.table?.partitionKey, entityMetadata.table?.sortKey]
// .filter(Boolean)
// .includes(attr.name)
// ) {
// throw new ConflictingAttributeNameError({
// attributeName: attr.name,
// entity: entityMetadata.name,
// tableName: entityMetadata.table?.name || '',
// });
// }

return new AttributeMetadata({
table,
// when working with entity with multiple inheritance, use class with @Entity() if available
Expand All @@ -69,18 +74,6 @@ export class AttributesMetadataBuilder {
}
}

if (
[entityMetadata.table?.partitionKey, entityMetadata.table?.sortKey]
.filter(Boolean)
.includes(attr.name)
) {
throw new ConflictingAttributeNameError({
attributeName: attr.name,
entity: entityMetadata.name,
tableName: entityMetadata.table?.name || '',
});
}

return new AutoGeneratedAttributeMetadata({
...attr,
});
Expand Down

0 comments on commit 1031d40

Please sign in to comment.