-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
ab7175c
commit 0a9b514
Showing
17 changed files
with
99 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
40 changes: 40 additions & 0 deletions
40
test/models/model-with-custom-mapper-and-default-value.model.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// tslint:disable:max-classes-per-file | ||
import { Model } from '../../src/decorator/impl/model/model.decorator' | ||
import { Property } from '../../src/decorator/impl/property/property.decorator' | ||
import { MapperForType } from '../../src/mapper/for-type/base.mapper' | ||
import { StringAttribute } from '../../src/mapper/type/attribute.type' | ||
|
||
export class MyProp { | ||
static default() { | ||
return new MyProp('default', 'none') | ||
} | ||
|
||
static parse(v: string) { | ||
const p = v.split('-') | ||
return new MyProp(p[0], p[1]) | ||
} | ||
|
||
static toString(v: MyProp) { | ||
return `${v.type}-${v.name}` | ||
} | ||
|
||
constructor(public type: string, public name: string) {} | ||
|
||
toString() { | ||
return MyProp.toString(this) | ||
} | ||
} | ||
|
||
const myPropMapper: MapperForType<MyProp, StringAttribute> = { | ||
fromDb: a => MyProp.parse(a.S), | ||
toDb: v => ({ S: MyProp.toString(v) }), | ||
} | ||
|
||
@Model() | ||
export class ModelWithCustomMapperAndDefaultValue { | ||
@Property({ | ||
mapper: myPropMapper, | ||
defaultValueProvider: () => MyProp.default(), | ||
}) | ||
myProp: MyProp | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { Model, PartitionKey, Property } from '../../src/dynamo-easy' | ||
|
||
@Model() | ||
export class ModelWithDefaultValue { | ||
@PartitionKey() | ||
@Property({ defaultValueProvider: () => `generated-id-${Math.floor(Math.random() * 1000)}` }) | ||
id: string | ||
} |