Skip to content

Commit fe99f0c

Browse files
fix(mapper): use provided db name for property
when creating the key attributes use the db name for property, this is only relevant when a custom name was provided which is different from the js property name in model
1 parent fb45e71 commit fe99f0c

File tree

4 files changed

+84
-9
lines changed

4 files changed

+84
-9
lines changed

src/mapper/mapper.spec.ts

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,12 @@ import {
2828
SimpleModel,
2929
SimpleWithCompositePartitionKeyModel,
3030
SimpleWithPartitionKeyModel,
31+
SimpleWithRenamedCompositePartitionKeyModel,
32+
SimpleWithRenamedPartitionKeyModel,
3133
StringType,
3234
Type,
3335
} from '../../test/models'
36+
import { ModelWithCombinedDecoratorsModel } from '../../test/models/model-with-combined-decorators.model'
3437
import { IdMapper } from '../../test/models/model-with-custom-mapper.model'
3538
import { ModelWithEmptyValues } from '../../test/models/model-with-empty-values'
3639
import {
@@ -667,10 +670,7 @@ describe('Mapper', () => {
667670

668671
describe('model with autogenerated id', () => {
669672
it('should create an uuid', () => {
670-
const toDbVal: Attributes<ModelWithAutogeneratedId> = toDb(
671-
new ModelWithAutogeneratedId(),
672-
ModelWithAutogeneratedId,
673-
)
673+
const toDbVal = toDb(new ModelWithAutogeneratedId(), ModelWithAutogeneratedId)
674674
expect(toDbVal.id).toBeDefined()
675675
expect(keyOf(toDbVal.id)).toBe('S')
676676
// https://stackoverflow.com/questions/7905929/how-to-test-valid-uuid-guid
@@ -680,6 +680,12 @@ describe('Mapper', () => {
680680
})
681681
})
682682

683+
describe('model with combined decorators', () => {
684+
const toDbValue: ModelWithCombinedDecoratorsModel = { id: 'idValue' }
685+
const mapped = toDb(toDbValue, ModelWithCombinedDecoratorsModel)
686+
expect(mapped).toEqual({ custom_id: { S: 'idValue' } })
687+
})
688+
683689
describe('model with non string/number/binary keys', () => {
684690
it('should accept date as HASH or RANGE key', () => {
685691
const now = new Date()
@@ -978,6 +984,13 @@ describe('Mapper', () => {
978984
})
979985
})
980986

987+
it('PartitionKey only (custom db name)', () => {
988+
const attrs = createKeyAttributes(metadataForModel(SimpleWithRenamedPartitionKeyModel), 'myId')
989+
expect(attrs).toEqual({
990+
custom_id: { S: 'myId' },
991+
})
992+
})
993+
981994
it('PartitionKey + SortKey', () => {
982995
const now = new Date()
983996
const attrs = createKeyAttributes(metadataForModel(SimpleWithCompositePartitionKeyModel), 'myId', now)
@@ -987,6 +1000,15 @@ describe('Mapper', () => {
9871000
})
9881001
})
9891002

1003+
it('PartitionKey + SortKey (custom db name)', () => {
1004+
const now = new Date()
1005+
const attrs = createKeyAttributes(metadataForModel(SimpleWithRenamedCompositePartitionKeyModel), 'myId', now)
1006+
expect(attrs).toEqual({
1007+
custom_id: { S: 'myId' },
1008+
custom_date: { S: now.toISOString() },
1009+
})
1010+
})
1011+
9901012
it('should throw when required sortKey is missing', () => {
9911013
expect(() => createKeyAttributes(metadataForModel(SimpleWithCompositePartitionKeyModel), 'myId')).toThrow()
9921014
})
@@ -996,17 +1018,27 @@ describe('Mapper', () => {
9961018
it('should throw when model has no defined properties', () => {
9971019
expect(() => createToKeyFn(SimpleModel)).toThrow()
9981020
})
1021+
9991022
it('should throw when given partial has undefined key properties', () => {
10001023
expect(() => toKey({}, SimpleWithPartitionKeyModel)).toThrow()
10011024
expect(() => toKey({ id: 'myId' }, SimpleWithCompositePartitionKeyModel)).toThrow()
10021025
expect(() => toKey({ creationDate: new Date() }, SimpleWithCompositePartitionKeyModel)).toThrow()
10031026
})
1027+
10041028
it('should create key attributes of simple key', () => {
10051029
const key = toKey({ id: 'myId' }, SimpleWithPartitionKeyModel)
10061030
expect(key).toEqual({
10071031
id: { S: 'myId' },
10081032
})
10091033
})
1034+
1035+
it('should create key attributes of simple key (custom db name)', () => {
1036+
const key = toKey({ id: 'myId' }, SimpleWithRenamedPartitionKeyModel)
1037+
expect(key).toEqual({
1038+
custom_id: { S: 'myId' },
1039+
})
1040+
})
1041+
10101042
it('should create key attributes of composite key', () => {
10111043
const partial: Partial<SimpleWithCompositePartitionKeyModel> = { id: 'myId', creationDate: new Date() }
10121044
const key = toKey(partial, SimpleWithCompositePartitionKeyModel)
@@ -1015,6 +1047,16 @@ describe('Mapper', () => {
10151047
creationDate: { S: partial.creationDate!.toISOString() },
10161048
})
10171049
})
1050+
1051+
it('should create key attributes of composite key (custom db name)', () => {
1052+
const partial: Partial<SimpleWithRenamedCompositePartitionKeyModel> = { id: 'myId', creationDate: new Date() }
1053+
const key = toKey(partial, SimpleWithRenamedCompositePartitionKeyModel)
1054+
expect(key).toEqual({
1055+
custom_id: { S: partial.id! },
1056+
custom_date: { S: partial.creationDate!.toISOString() },
1057+
})
1058+
})
1059+
10181060
it('should create key with custom mapper', () => {
10191061
const partial: ModelWithCustomMapperModel = { id: new Id(7, 2018) }
10201062
const key = toKey(partial, ModelWithCustomMapperModel)

src/mapper/mapper.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ export function createToKeyFn<T>(modelConstructor: ModelConstructor<T>): (item:
167167
throw new Error(`there is no value for property ${propMeta.name.toString()} but is ${propMeta.key.type} key`)
168168
}
169169
const propertyValue = getPropertyValue(item, propMeta.name)
170-
key[propMeta.name] = <Attribute>toDbOne(propertyValue, propMeta)
170+
key[propMeta.nameDb] = <Attribute>toDbOne(propertyValue, propMeta)
171171
return key
172172
},
173173
<Attributes<T>>{},
@@ -192,16 +192,27 @@ export function createKeyAttributes<T>(
192192
): Attributes<Partial<T>> {
193193
const partitionKeyProp = metadata.getPartitionKey()
194194

195+
const partitionKeyMetadata = metadata.forProperty(partitionKeyProp)
196+
197+
if (!partitionKeyMetadata) {
198+
throw new Error('metadata for partition key must be defined')
199+
}
200+
195201
const keyAttributeMap = <Attributes<T>>{
196-
[partitionKeyProp]: toDbOne(partitionKey, metadata.forProperty(partitionKeyProp)),
202+
[partitionKeyMetadata.nameDb]: toDbOne(partitionKey, partitionKeyMetadata),
197203
}
198204

199205
if (hasSortKey(metadata)) {
200206
if (sortKey === null || sortKey === undefined) {
201207
throw new Error(`please provide the sort key for attribute ${metadata.getSortKey()}`)
202208
}
203209
const sortKeyProp = metadata.getSortKey()
204-
keyAttributeMap[sortKeyProp] = <Attribute>toDbOne(sortKey, metadata.forProperty(sortKeyProp))
210+
const sortKeyMetadata = metadata.forProperty(sortKeyProp)
211+
if (!sortKeyMetadata) {
212+
throw new Error('metadata for sort key must be defined')
213+
}
214+
215+
keyAttributeMap[sortKeyMetadata.nameDb] = <Attribute>toDbOne(sortKey,sortKeyMetadata)
205216
}
206217

207218
return keyAttributeMap

test/models/simple-with-composite-partition-key.model.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { DateProperty, Model, PartitionKey, SortKey } from '../../src/dynamo-easy'
1+
import { DateProperty, Model, PartitionKey, Property, SortKey } from '../../src/dynamo-easy'
22

33
@Model()
44
export class SimpleWithCompositePartitionKeyModel {
@@ -11,3 +11,16 @@ export class SimpleWithCompositePartitionKeyModel {
1111

1212
age: number
1313
}
14+
15+
@Model()
16+
export class SimpleWithRenamedCompositePartitionKeyModel {
17+
@PartitionKey()
18+
@Property({ name: 'custom_id' })
19+
id: string
20+
21+
@SortKey()
22+
@DateProperty({ name: 'custom_date' })
23+
creationDate: Date
24+
25+
age: number
26+
}
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Model, PartitionKey } from '../../src/dynamo-easy'
1+
import { Model, PartitionKey, Property } from '../../src/dynamo-easy'
22

33
@Model()
44
export class SimpleWithPartitionKeyModel {
@@ -7,3 +7,12 @@ export class SimpleWithPartitionKeyModel {
77

88
age: number
99
}
10+
11+
@Model()
12+
export class SimpleWithRenamedPartitionKeyModel {
13+
@PartitionKey()
14+
@Property({ name: 'custom_id' })
15+
id: string
16+
17+
age: number
18+
}

0 commit comments

Comments
 (0)