Skip to content

Commit

Permalink
Merge pull request #217 from shiftcode/#215-createkeyfn-use-namedb
Browse files Browse the repository at this point in the history
#215 createkeyfn use namedb
  • Loading branch information
simonmumenthaler authored May 20, 2019
2 parents fb45e71 + ae086d7 commit 7a36394
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 9 deletions.
49 changes: 45 additions & 4 deletions src/mapper/mapper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import {
SimpleModel,
SimpleWithCompositePartitionKeyModel,
SimpleWithPartitionKeyModel,
SimpleWithRenamedCompositePartitionKeyModel,
SimpleWithRenamedPartitionKeyModel,
StringType,
Type,
} from '../../test/models'
Expand Down Expand Up @@ -667,10 +669,7 @@ describe('Mapper', () => {

describe('model with autogenerated id', () => {
it('should create an uuid', () => {
const toDbVal: Attributes<ModelWithAutogeneratedId> = toDb(
new ModelWithAutogeneratedId(),
ModelWithAutogeneratedId,
)
const toDbVal = toDb(new ModelWithAutogeneratedId(), ModelWithAutogeneratedId)
expect(toDbVal.id).toBeDefined()
expect(keyOf(toDbVal.id)).toBe('S')
// https://stackoverflow.com/questions/7905929/how-to-test-valid-uuid-guid
Expand All @@ -680,6 +679,12 @@ describe('Mapper', () => {
})
})

describe('model with combined decorators', () => {
const toDbValue: SimpleWithRenamedPartitionKeyModel = { id: 'idValue', age: 30 }
const mapped = toDb(toDbValue, SimpleWithRenamedPartitionKeyModel)
expect(mapped).toEqual({ custom_id: { S: 'idValue' }, age: { N: '30' } })
})

describe('model with non string/number/binary keys', () => {
it('should accept date as HASH or RANGE key', () => {
const now = new Date()
Expand Down Expand Up @@ -978,6 +983,13 @@ describe('Mapper', () => {
})
})

it('PartitionKey only (custom db name)', () => {
const attrs = createKeyAttributes(metadataForModel(SimpleWithRenamedPartitionKeyModel), 'myId')
expect(attrs).toEqual({
custom_id: { S: 'myId' },
})
})

it('PartitionKey + SortKey', () => {
const now = new Date()
const attrs = createKeyAttributes(metadataForModel(SimpleWithCompositePartitionKeyModel), 'myId', now)
Expand All @@ -987,6 +999,15 @@ describe('Mapper', () => {
})
})

it('PartitionKey + SortKey (custom db name)', () => {
const now = new Date()
const attrs = createKeyAttributes(metadataForModel(SimpleWithRenamedCompositePartitionKeyModel), 'myId', now)
expect(attrs).toEqual({
custom_id: { S: 'myId' },
custom_date: { S: now.toISOString() },
})
})

it('should throw when required sortKey is missing', () => {
expect(() => createKeyAttributes(metadataForModel(SimpleWithCompositePartitionKeyModel), 'myId')).toThrow()
})
Expand All @@ -996,17 +1017,27 @@ describe('Mapper', () => {
it('should throw when model has no defined properties', () => {
expect(() => createToKeyFn(SimpleModel)).toThrow()
})

it('should throw when given partial has undefined key properties', () => {
expect(() => toKey({}, SimpleWithPartitionKeyModel)).toThrow()
expect(() => toKey({ id: 'myId' }, SimpleWithCompositePartitionKeyModel)).toThrow()
expect(() => toKey({ creationDate: new Date() }, SimpleWithCompositePartitionKeyModel)).toThrow()
})

it('should create key attributes of simple key', () => {
const key = toKey({ id: 'myId' }, SimpleWithPartitionKeyModel)
expect(key).toEqual({
id: { S: 'myId' },
})
})

it('should create key attributes of simple key (custom db name)', () => {
const key = toKey({ id: 'myId' }, SimpleWithRenamedPartitionKeyModel)
expect(key).toEqual({
custom_id: { S: 'myId' },
})
})

it('should create key attributes of composite key', () => {
const partial: Partial<SimpleWithCompositePartitionKeyModel> = { id: 'myId', creationDate: new Date() }
const key = toKey(partial, SimpleWithCompositePartitionKeyModel)
Expand All @@ -1015,6 +1046,16 @@ describe('Mapper', () => {
creationDate: { S: partial.creationDate!.toISOString() },
})
})

it('should create key attributes of composite key (custom db name)', () => {
const partial: Partial<SimpleWithRenamedCompositePartitionKeyModel> = { id: 'myId', creationDate: new Date() }
const key = toKey(partial, SimpleWithRenamedCompositePartitionKeyModel)
expect(key).toEqual({
custom_id: { S: partial.id! },
custom_date: { S: partial.creationDate!.toISOString() },
})
})

it('should create key with custom mapper', () => {
const partial: ModelWithCustomMapperModel = { id: new Id(7, 2018) }
const key = toKey(partial, ModelWithCustomMapperModel)
Expand Down
17 changes: 14 additions & 3 deletions src/mapper/mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export function createToKeyFn<T>(modelConstructor: ModelConstructor<T>): (item:
throw new Error(`there is no value for property ${propMeta.name.toString()} but is ${propMeta.key.type} key`)
}
const propertyValue = getPropertyValue(item, propMeta.name)
key[propMeta.name] = <Attribute>toDbOne(propertyValue, propMeta)
key[propMeta.nameDb] = <Attribute>toDbOne(propertyValue, propMeta)
return key
},
<Attributes<T>>{},
Expand All @@ -192,16 +192,27 @@ export function createKeyAttributes<T>(
): Attributes<Partial<T>> {
const partitionKeyProp = metadata.getPartitionKey()

const partitionKeyMetadata = metadata.forProperty(partitionKeyProp)

if (!partitionKeyMetadata) {
throw new Error('metadata for partition key must be defined')
}

const keyAttributeMap = <Attributes<T>>{
[partitionKeyProp]: toDbOne(partitionKey, metadata.forProperty(partitionKeyProp)),
[partitionKeyMetadata.nameDb]: toDbOne(partitionKey, partitionKeyMetadata),
}

if (hasSortKey(metadata)) {
if (sortKey === null || sortKey === undefined) {
throw new Error(`please provide the sort key for attribute ${metadata.getSortKey()}`)
}
const sortKeyProp = metadata.getSortKey()
keyAttributeMap[sortKeyProp] = <Attribute>toDbOne(sortKey, metadata.forProperty(sortKeyProp))
const sortKeyMetadata = metadata.forProperty(sortKeyProp)
if (!sortKeyMetadata) {
throw new Error('metadata for sort key must be defined')
}

keyAttributeMap[sortKeyMetadata.nameDb] = <Attribute>toDbOne(sortKey,sortKeyMetadata)
}

return keyAttributeMap
Expand Down
15 changes: 14 additions & 1 deletion test/models/simple-with-composite-partition-key.model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DateProperty, Model, PartitionKey, SortKey } from '../../src/dynamo-easy'
import { DateProperty, Model, PartitionKey, Property, SortKey } from '../../src/dynamo-easy'

@Model()
export class SimpleWithCompositePartitionKeyModel {
Expand All @@ -11,3 +11,16 @@ export class SimpleWithCompositePartitionKeyModel {

age: number
}

@Model()
export class SimpleWithRenamedCompositePartitionKeyModel {
@PartitionKey()
@Property({ name: 'custom_id' })
id: string

@SortKey()
@DateProperty({ name: 'custom_date' })
creationDate: Date

age: number
}
11 changes: 10 additions & 1 deletion test/models/simple-with-partition-key.model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Model, PartitionKey } from '../../src/dynamo-easy'
import { Model, PartitionKey, Property } from '../../src/dynamo-easy'

@Model()
export class SimpleWithPartitionKeyModel {
Expand All @@ -7,3 +7,12 @@ export class SimpleWithPartitionKeyModel {

age: number
}

@Model()
export class SimpleWithRenamedPartitionKeyModel {
@PartitionKey()
@Property({ name: 'custom_id' })
id: string

age: number
}

0 comments on commit 7a36394

Please sign in to comment.