Skip to content

Commit 6198f46

Browse files
Merge pull request #79 from shiftcode/development
Development
2 parents aa8580b + a0a26d5 commit 6198f46

File tree

181 files changed

+7509
-8420
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

181 files changed

+7509
-8420
lines changed

.lintstagedrc.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
linters:
2+
"src/**/*(!spec).ts":
3+
- prettier --write --config ./.prettierrc.yml
4+
- tslint --project ./tsconfig.json -t codeFrame --fix
5+
- git add
6+
"**/package.json":
7+
- sort-package-json
8+
- git add

.prettierrc.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
printWidth: 120
33
singleQuote: true
44
semi: false
5-
trailingComma: es5
5+
trailingComma: all

.releaserc.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
# run semantic-release on master branch
12
branch: master

.travis.yml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ language: node_js
22
node_js:
33
- lts/*
44

5-
cache:
6-
directories:
7-
- $HOME/.npm
8-
- node_modules
5+
cache: npm
6+
# directories:
7+
# - $HOME/.npm
8+
# - node_modules
99

1010
#before_install:
1111
#- npm install npm@^6.0.0 -g
@@ -16,19 +16,19 @@ cache:
1616
# of updating the package lock.
1717
install:
1818
# decide if running on greenkeeper branch or not
19-
#- case $TRAVIS_BRANCH in greenkeeper*) npm i;; *) npm ci;; esac;
19+
- case $TRAVIS_BRANCH in greenkeeper*) npm i;; *) npm ci;; esac;
2020
# somehow the above command fails when we have a cache available on travis - needs some more investigation, stick with
2121
# npm i for now
22-
- npm i
22+
#- npm ci
2323

2424
notifications:
2525
email: false
2626

2727
before_script: npx greenkeeper-lockfile-update
2828

2929
script:
30-
- npm run lint
31-
- npm run test:prod
30+
- npm run lint:ci
31+
- npm run test:coverage
3232
- npm run build
3333
- npm run build:docs
3434

README.md

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Checkout the full technical api documentation [here](https://shiftcode.github.io
2222
Built with :heart: by [shiftcode](https://www.shiftcode.ch).
2323

2424
# Get Started
25-
```
25+
```typescript
2626
@Model()
2727
class Person{
2828
@PartitionKeyUUID()
@@ -109,7 +109,7 @@ Simple Type (no decorators required to work)
109109
- Boolean
110110
- Null
111111
- Array
112-
- Date (we only support MomentJS)
112+
- String/Number Enum
113113

114114
Complex Types (properties with these types need some decorators to work properly)
115115
- Set<simpleType | complexType>
@@ -121,7 +121,6 @@ Complex Types (properties with these types need some decorators to work properly
121121
| String | S |
122122
| Number | N |
123123
| Boolean | BOOL |
124-
| moment.Moment | S (ISO-8601 formatted) |
125124
| null | NULL |
126125
| Array | L, (S,N,B)S |
127126
| ES6 Set | L, (S,N,B)S |
@@ -132,7 +131,8 @@ Complex Types (properties with these types need some decorators to work properly
132131
| Date | Not Supported |
133132

134133
## Custom Attribute Mapping
135-
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.
136136

137137
## Collection Mapping (Array & Set)
138138

@@ -150,13 +150,42 @@ When one of the following decorators is present, the value is always mapped to a
150150
- @TypedArray()
151151

152152
## Date
153-
Right now we only support [MomentJS](http://momentjs.com/) Dates.
153+
We only support the native Date type and you need to explicitly mark a property to be a Date by using the @Date() decorator\
154+
(which is basically just syntactic sugar for @CustomMapper(TheDateMapper)).\
155+
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:\
156+
`updateDynamoEasyConfig({ dateMapper: MomentMapper })`\
157+
158+
159+
A mapper for moment dates could look like this:
160+
```typescript
161+
import * as moment from 'moment'
162+
import { MapperForType, StringAttribute } from '@shiftcoders/dynamo-easy'
163+
164+
export const MomentMapper: MapperForType<moment.Moment, StringAttribute> = {
165+
166+
fromDb: (value: StringAttribute) => {
167+
const parsed = moment(value.S, moment.ISO_8601)
168+
if (!parsed.isValid()) {
169+
throw new Error(`the value ${value} cannot be parsed into a valid moment date`)
170+
}
171+
return parsed
172+
},
173+
174+
toDb: (value: moment.Moment) => {
175+
if (!moment.isMoment(value)) {
176+
throw new Error(`the value ${value} is not of type moment`)
177+
}
178+
if (!value.isValid()) {
179+
throw new Error(`cannot map property value ${value}, because it is not a valid moment date`)
180+
}
181+
return { S: value.clone().utc().format() }
182+
},
183+
}
184+
```
154185

155-
If you want to explicitly mark a property to be a Date use the @Date() decorator. If we find a moment value we automatically map it to a String (using ISO-8601 format).
156-
When coming from db we do a regex test for ISO-8601 format and map it back to a moment object.
157186

158187
## Enum
159-
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.
160189

161190
# Request API
162191
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.
@@ -168,7 +197,8 @@ We support the following dynamodb operations with a fluent api:
168197
- Delete
169198
- Scan
170199
- Query
171-
- BatchGet
200+
- BatchGet (from a single table)
201+
- BatchWrite (to a single table)
172202
- MakeRequest (generic low level method for special scenarios)
173203

174204
There is always the possibility to access the Params object directly to add values which are not covered with our api.
@@ -189,7 +219,7 @@ function sessionValidityEnsurer(): Observable<boolean> {
189219
this.logger.debug('withValidSession :: cognitoService.isLoggedIn() -> we have a valid session -> proceed')
190220
return Observable.of(true)
191221
} else {
192-
this.logger.debug('withValidSession :: cognitoService.isLoggedIn() -> user is not logged in or token expired, try to get a new session')
222+
this.logger.debug(metadata)
193223
return this.getUser()
194224
.catch((err, caught): Observable<boolean> => {
195225
this.logger.error('withValidSession :: there was an error when refreshing the session', err)
@@ -209,7 +239,7 @@ By default we create a substitution placeholder for all the attributes, just to
209239

210240
attributename: age
211241

212-
```
242+
```typescript
213243
expression: '#age = :age'
214244
attributeExpressionNames: {'#age': 'age'}
215245
attributeExpressionValues: {':age': {N: '10'}}
@@ -218,8 +248,8 @@ attributeExpressionValues: {':age': {N: '10'}}
218248
this works seemlesly for top level attribtues, but if we wanna build an expression for where the attribute needs to be accessed with a document path, we need some special logic
219249
nested attribute: person.age
220250

221-
```
222-
attributeExpressionNames: {'#person':'person', '#age': 'age'}
251+
```typescript
252+
attributeExpressionNames: {'#person': 'person', '#age': 'age'}
223253
attributeExpressionValues: {':age': {N: '10'}}
224254
expression: '#person.#age = :age'
225255
```
@@ -240,7 +270,7 @@ There are two scenarios for a batch get item request. One is requesting multiple
240270
tables.
241271
The first scenario is support using DynamoStore.batchGet() the second one must be implemented using the BatchGetItem class.
242272

243-
```
273+
```typescript
244274
const request = new BatchRequest()
245275

246276
// table with simple primary key

jest.config.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module.exports = {
22
testEnvironment: "node",
33
globals: {
44
"ts-jest": {
5-
tsConfigFile: "./tsconfig.jest.json"
5+
tsConfig: "./tsconfig.jest.json"
66
}
77
},
88
transform: {
@@ -27,6 +27,7 @@ module.exports = {
2727
}
2828
},
2929
setupFiles: [
30-
"reflect-metadata"
30+
"reflect-metadata",
31+
'./test/jest-setup.ts'
3132
]
32-
}
33+
};

0 commit comments

Comments
 (0)