Skip to content

Commit 0a9b514

Browse files
feat(decorator): defaultValueProvider for @Property()
With a defaultValueProvider you're able to have properties like createdAt without the necessity to set the date by hand. With this we also dropped the UUID as peerDependency. BREAKING CHANGE: `@PartitionKeyUUID()` does no longer exist. Use `@Property({ defaultValueProvider: uuidGeneratorFunction })` instead
1 parent ab7175c commit 0a9b514

17 files changed

+99
-60
lines changed

package.json

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@
6161
"@types/jest": "^24.0.18",
6262
"@types/lodash": "^4.14.137",
6363
"@types/node": "8.10.40",
64-
"@types/uuid": "^3.4.5",
6564
"aws-sdk": "^2.401.0",
6665
"colors": "^1.3.3",
6766
"coveralls": "^3.0.6",
@@ -82,14 +81,12 @@
8281
"tsutils": "^3.17.1",
8382
"typedoc": "0.14.0",
8483
"typedoc-plugin-external-module-name": "^2.1.0",
85-
"typescript": ">=2.9.1",
86-
"uuid": "^3.3.2"
84+
"typescript": ">=2.9.1"
8785
},
8886
"peerDependencies": {
8987
"aws-sdk": "^2.401.0",
9088
"lodash": "^4.17.11",
9189
"reflect-metadata": "^0.1.12",
92-
"tslib": "^1.10.0",
93-
"uuid": "^3.3.2"
90+
"tslib": "^1.10.0"
9491
}
9592
}

src/decorator/decorators.spec.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ describe('Decorators should add correct metadata', () => {
117117
expect(prop!.nameDb).toBe('id')
118118
expect(prop!.key).toBeDefined()
119119
expect(prop!.key!.type).toBe('HASH')
120-
expect(prop!.key!.uuid).toBeFalsy()
121120
expect(prop!.transient).toBeFalsy()
122121
expect(prop!.typeInfo).toBeDefined()
123122
expect(prop!.typeInfo!.type).toBe(String)
@@ -130,7 +129,6 @@ describe('Decorators should add correct metadata', () => {
130129
expect(prop!.nameDb).toBe('creationDate')
131130
expect(prop!.key).toBeDefined()
132131
expect(prop!.key!.type).toBe('RANGE')
133-
expect(prop!.key!.uuid).toBeFalsy()
134132
expect(prop!.transient).toBeFalsy()
135133
expect(prop!.typeInfo).toBeDefined()
136134
})

src/decorator/impl/key/partition-key-uuid.decorator.ts

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/decorator/impl/property/property-data.model.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ import { MapperForType } from '../../../mapper/for-type/base.mapper'
66
/**
77
* Option interface for @Property decorator
88
*/
9-
export interface PropertyData {
10-
// the name of property how it is named in dynamoDB
9+
export interface PropertyData<T> {
10+
/**
11+
* the name of property how it is named in dynamoDB
12+
*/
1113
name: string
12-
mapper: MapperForType<any, any>
14+
mapper: MapperForType<T, any>
15+
defaultValueProvider: () => T
1316
}

src/decorator/impl/property/property.decorator.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ import { PropertyMetadata } from '../../metadata/property-metadata.model'
55
import { initOrUpdateProperty } from './init-or-update-property.function'
66
import { PropertyData } from './property-data.model'
77

8-
export function Property(opts: Partial<PropertyData> = {}): PropertyDecorator {
8+
export function Property<T>(opts: Partial<PropertyData<T>> = {}): PropertyDecorator {
99
return (target: object, propertyKey: string | symbol) => {
1010
if (typeof propertyKey === 'string') {
1111
const propertyOptions: Partial<PropertyMetadata<any>> = {
1212
name: propertyKey,
1313
nameDb: opts.name || propertyKey,
14+
defaultValueProvider: opts.defaultValueProvider,
1415
}
1516

1617
if ('mapper' in opts && !!opts.mapper) {

src/decorator/impl/public-api.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ export * from './index/lsi-sort-key.decorator'
1414
export * from './index/index-type.enum'
1515
// key
1616
export * from './key/partition-key.decorator'
17-
export * from './key/partition-key-uuid.decorator'
1817
export * from './key/sort-key.decorator'
1918

2019
// model

src/decorator/metadata/metadata.spec.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
INDEX_ACTIVE_CREATED_AT,
77
INDEX_COUNT,
88
ModelWithABunchOfIndexes,
9-
ModelWithAutogeneratedId,
9+
ModelWithDefaultValue,
1010
ModelWithGSI,
1111
ModelWithLSI,
1212
ModelWithoutPartitionKeyModel,
@@ -22,7 +22,7 @@ describe('metadata', () => {
2222
let metaDataLsi: Metadata<ModelWithLSI>
2323
let metaDataGsi: Metadata<ModelWithGSI>
2424
let metaDataIndexes: Metadata<ModelWithABunchOfIndexes>
25-
let metaDataUuid: Metadata<ModelWithAutogeneratedId>
25+
let metaDataDefaultValue: Metadata<ModelWithDefaultValue>
2626
let metaDataComplex: Metadata<ComplexModel>
2727

2828
beforeEach(() => {
@@ -32,7 +32,7 @@ describe('metadata', () => {
3232
metaDataLsi = new Metadata(ModelWithLSI)
3333
metaDataGsi = new Metadata(ModelWithGSI)
3434
metaDataIndexes = new Metadata(ModelWithABunchOfIndexes)
35-
metaDataUuid = new Metadata(ModelWithAutogeneratedId)
35+
metaDataDefaultValue = new Metadata(ModelWithDefaultValue)
3636
metaDataComplex = new Metadata(ComplexModel)
3737
})
3838

@@ -61,12 +61,13 @@ describe('metadata', () => {
6161
expect(nestedObjDateMeta).toBeUndefined()
6262
})
6363

64-
it('getKeysWithUUID', () => {
65-
const uuid = metaDataUuid.getKeysWithUUID()
66-
expect(uuid.length).toBe(1)
67-
expect(uuid[0].key).toBeDefined()
68-
expect(uuid[0].key!.uuid).toBeTruthy()
69-
expect(uuid[0].name).toBe('id')
64+
it('getPropertiesWithDefaultValueProvider', () => {
65+
const props = metaDataDefaultValue.getPropertiesWithDefaultValueProvider()
66+
expect(props.length).toBe(1)
67+
expect(props[0].key).toBeDefined()
68+
expect(props[0].name).toBe('id')
69+
expect(props[0].defaultValueProvider).toBeDefined()
70+
expect(props[0].defaultValueProvider!()).toBeDefined()
7071
})
7172

7273
it('getPartitionKey', () => {

src/decorator/metadata/metadata.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ export class Metadata<T> {
7070

7171
/**
7272
*
73-
* @returns {Array<PropertyMetadata<any>>} Returns all the properties property the @PartitionKeyUUID decorator is present, returns an empty array by default
73+
* @returns {Array<PropertyMetadata<any>>} Returns all the properties a defaultValueProvider, returns an empty array by default
7474
*/
75-
getKeysWithUUID(): Array<PropertyMetadata<any>> {
76-
return filterBy(this.modelOptions, p => !!(p.key && p.key.uuid), [])
75+
getPropertiesWithDefaultValueProvider(): Array<PropertyMetadata<any>> {
76+
return filterBy(this.modelOptions, p => !!p.defaultValueProvider, [])
7777
}
7878

7979
/**

src/decorator/metadata/property-metadata.model.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ export interface TypeInfo {
1313

1414
export interface Key {
1515
type: DynamoDB.KeyType
16-
uuid?: boolean
1716
}
1817

1918
export interface PropertyMetadata<T, R extends Attribute = Attribute> {
@@ -49,6 +48,8 @@ export interface PropertyMetadata<T, R extends Attribute = Attribute> {
4948

5049
// index?: IModelAttributeIndex
5150
transient?: boolean
51+
52+
defaultValueProvider?: () => any
5253
}
5354

5455
/**

src/dynamo/expression/logical-operator/attribute.function.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
* @Model()
1717
* class Person{
1818
*
19-
* @PartitionKeyUUID()
19+
* @PartitionKey()
2020
* id: string
2121
* age: number
2222
* }

0 commit comments

Comments
 (0)