Skip to content

Commit 75f712b

Browse files
Merge branch 'master' into #216-condition-builder
2 parents d1ae1bb + 7a36394 commit 75f712b

File tree

4 files changed

+83
-9
lines changed

4 files changed

+83
-9
lines changed

src/mapper/mapper.spec.ts

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import {
2828
SimpleModel,
2929
SimpleWithCompositePartitionKeyModel,
3030
SimpleWithPartitionKeyModel,
31+
SimpleWithRenamedCompositePartitionKeyModel,
32+
SimpleWithRenamedPartitionKeyModel,
3133
StringType,
3234
Type,
3335
} from '../../test/models'
@@ -667,10 +669,7 @@ describe('Mapper', () => {
667669

668670
describe('model with autogenerated id', () => {
669671
it('should create an uuid', () => {
670-
const toDbVal: Attributes<ModelWithAutogeneratedId> = toDb(
671-
new ModelWithAutogeneratedId(),
672-
ModelWithAutogeneratedId,
673-
)
672+
const toDbVal = toDb(new ModelWithAutogeneratedId(), ModelWithAutogeneratedId)
674673
expect(toDbVal.id).toBeDefined()
675674
expect(keyOf(toDbVal.id)).toBe('S')
676675
// https://stackoverflow.com/questions/7905929/how-to-test-valid-uuid-guid
@@ -680,6 +679,12 @@ describe('Mapper', () => {
680679
})
681680
})
682681

682+
describe('model with combined decorators', () => {
683+
const toDbValue: SimpleWithRenamedPartitionKeyModel = { id: 'idValue', age: 30 }
684+
const mapped = toDb(toDbValue, SimpleWithRenamedPartitionKeyModel)
685+
expect(mapped).toEqual({ custom_id: { S: 'idValue' }, age: { N: '30' } })
686+
})
687+
683688
describe('model with non string/number/binary keys', () => {
684689
it('should accept date as HASH or RANGE key', () => {
685690
const now = new Date()
@@ -978,6 +983,13 @@ describe('Mapper', () => {
978983
})
979984
})
980985

986+
it('PartitionKey only (custom db name)', () => {
987+
const attrs = createKeyAttributes(metadataForModel(SimpleWithRenamedPartitionKeyModel), 'myId')
988+
expect(attrs).toEqual({
989+
custom_id: { S: 'myId' },
990+
})
991+
})
992+
981993
it('PartitionKey + SortKey', () => {
982994
const now = new Date()
983995
const attrs = createKeyAttributes(metadataForModel(SimpleWithCompositePartitionKeyModel), 'myId', now)
@@ -987,6 +999,15 @@ describe('Mapper', () => {
987999
})
9881000
})
9891001

1002+
it('PartitionKey + SortKey (custom db name)', () => {
1003+
const now = new Date()
1004+
const attrs = createKeyAttributes(metadataForModel(SimpleWithRenamedCompositePartitionKeyModel), 'myId', now)
1005+
expect(attrs).toEqual({
1006+
custom_id: { S: 'myId' },
1007+
custom_date: { S: now.toISOString() },
1008+
})
1009+
})
1010+
9901011
it('should throw when required sortKey is missing', () => {
9911012
expect(() => createKeyAttributes(metadataForModel(SimpleWithCompositePartitionKeyModel), 'myId')).toThrow()
9921013
})
@@ -996,17 +1017,27 @@ describe('Mapper', () => {
9961017
it('should throw when model has no defined properties', () => {
9971018
expect(() => createToKeyFn(SimpleModel)).toThrow()
9981019
})
1020+
9991021
it('should throw when given partial has undefined key properties', () => {
10001022
expect(() => toKey({}, SimpleWithPartitionKeyModel)).toThrow()
10011023
expect(() => toKey({ id: 'myId' }, SimpleWithCompositePartitionKeyModel)).toThrow()
10021024
expect(() => toKey({ creationDate: new Date() }, SimpleWithCompositePartitionKeyModel)).toThrow()
10031025
})
1026+
10041027
it('should create key attributes of simple key', () => {
10051028
const key = toKey({ id: 'myId' }, SimpleWithPartitionKeyModel)
10061029
expect(key).toEqual({
10071030
id: { S: 'myId' },
10081031
})
10091032
})
1033+
1034+
it('should create key attributes of simple key (custom db name)', () => {
1035+
const key = toKey({ id: 'myId' }, SimpleWithRenamedPartitionKeyModel)
1036+
expect(key).toEqual({
1037+
custom_id: { S: 'myId' },
1038+
})
1039+
})
1040+
10101041
it('should create key attributes of composite key', () => {
10111042
const partial: Partial<SimpleWithCompositePartitionKeyModel> = { id: 'myId', creationDate: new Date() }
10121043
const key = toKey(partial, SimpleWithCompositePartitionKeyModel)
@@ -1015,6 +1046,16 @@ describe('Mapper', () => {
10151046
creationDate: { S: partial.creationDate!.toISOString() },
10161047
})
10171048
})
1049+
1050+
it('should create key attributes of composite key (custom db name)', () => {
1051+
const partial: Partial<SimpleWithRenamedCompositePartitionKeyModel> = { id: 'myId', creationDate: new Date() }
1052+
const key = toKey(partial, SimpleWithRenamedCompositePartitionKeyModel)
1053+
expect(key).toEqual({
1054+
custom_id: { S: partial.id! },
1055+
custom_date: { S: partial.creationDate!.toISOString() },
1056+
})
1057+
})
1058+
10181059
it('should create key with custom mapper', () => {
10191060
const partial: ModelWithCustomMapperModel = { id: new Id(7, 2018) }
10201061
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)