Skip to content

Commit 977eb38

Browse files
committed
refactor(mapper): use object instead of class
BREAKING CHANGE: CustomMapper decorator no longer accepts classes but an object with the two methods fromDb/toDb
1 parent a9b6b38 commit 977eb38

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

README.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ Simple Type (no decorators required to work)
109109
- Boolean
110110
- Null
111111
- Array
112+
- String/Number Enum
112113

113114
Complex Types (properties with these types need some decorators to work properly)
114115
- Set<simpleType | complexType>
@@ -130,7 +131,8 @@ Complex Types (properties with these types need some decorators to work properly
130131
| Date | Not Supported |
131132

132133
## Custom Attribute Mapping
133-
It is always possible to define a custom mapping strategy, just implement the [MapperForType](https://shiftcode.github.io/dynamo-easy/interfaces/_mapper_for_type_base_mapper_.mapperfortype.html) class.
134+
It is always possible to define a custom mapping strategy,
135+
just implement the [MapperForType](https://shiftcode.github.io/dynamo-easy/interfaces/_mapper_for_type_base_mapper_.mapperfortype.html) and provide with the CustomMapper directive.
134136

135137
## Collection Mapping (Array & Set)
136138

@@ -152,39 +154,38 @@ We only support the native Date type and you need to explicitly mark a property
152154
(which is basically just syntactic sugar for @CustomMapper(TheDateMapper)).\
153155
If you want to use a different type for the @Date decorator (eg. Moment) you need to define a custom mapper and provide it to the dynamo easy config like this:\
154156
`updateDynamoEasyConfig({ dateMapper: MomentMapper })`\
155-
-> Update the config before importing any models!
156157

157158

158159
A mapper for moment dates could look like this:
159160
```typescript
160161
import * as moment from 'moment'
161162
import { MapperForType, StringAttribute } from '@shiftcoders/dynamo-easy'
162163

163-
export class MomentMapper implements MapperForType<moment.Moment, StringAttribute> {
164+
export const MomentMapper: MapperForType<moment.Moment, StringAttribute> = {
164165

165-
fromDb(value: StringAttribute): moment.Moment {
166-
const parsed: moment.Moment = moment(value.S, moment.ISO_8601)
166+
fromDb: (value: StringAttribute) => {
167+
const parsed = moment(value.S, moment.ISO_8601)
167168
if (!parsed.isValid()) {
168169
throw new Error(`the value ${value} cannot be parsed into a valid moment date`)
169170
}
170171
return parsed
171-
}
172+
},
172173

173-
toDb(value: moment.Moment): StringAttribute {
174+
toDb: (value: moment.Moment) => {
174175
if (!moment.isMoment(value)) {
175176
throw new Error(`the value ${value} is not of type moment`)
176177
}
177178
if (!value.isValid()) {
178179
throw new Error(`cannot map property value ${value}, because it is not a valid moment date`)
179180
}
180181
return { S: value.clone().utc().format() }
181-
}
182+
},
182183
}
183184
```
184185

185186

186187
## Enum
187-
Enum values are persisted as Numbers (index of enum).
188+
Enum values are persisted as Numbers (index of enum) or string if string value was given.
188189

189190
# Request API
190191
To start making requests create an instance of [DynamoStore](https://shiftcode.github.io/dynamo-easy/classes/_dynamo_dynamo_store_.dynamostore.html) and execute the desired operation using the provided api.
@@ -196,7 +197,8 @@ We support the following dynamodb operations with a fluent api:
196197
- Delete
197198
- Scan
198199
- Query
199-
- BatchGet
200+
- BatchGet (from a single table)
201+
- BatchWrite (to a single table)
200202
- MakeRequest (generic low level method for special scenarios)
201203

202204
There is always the possibility to access the Params object directly to add values which are not covered with our api.

0 commit comments

Comments
 (0)