Skip to content

Commit

Permalink
docs(gitbook): initial doc from v1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelwittwer committed Feb 6, 2019
1 parent d0b461f commit 6f32508
Show file tree
Hide file tree
Showing 13 changed files with 730 additions and 12 deletions.
22 changes: 18 additions & 4 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
---
description: Some description
---
# Introduction

{% code-tabs %}
{% code-tabs-item title="looks-easy-right.ts" %}
```typescript
dynamoStore
.scan()
.where(
attribute('name').equals('shiftcode'),
attribute('age').gt(20)
)
.exec()
```
{% endcode-tabs-item %}
{% endcode-tabs %}

## Developed with ❤ by [www.shiftcode.ch](www.shiftcode.ch)


# Dynamo-Easy Gitbook

22 changes: 20 additions & 2 deletions docs/SUMMARY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
# Table of contents

* [Dynamo-Easy Gitbook](README.md)
* [Second Page](second-page.md)
* [Introduction](README.md)

## Get Started

* [Installation](get-started/installation/README.md)
* [Browser vs. Node usage](get-started/installation/browser-vs.-node-usage.md)
* [Jump into code](get-started/jump-into-code.md)

## API Doc

* [Config](api/dynamo-easy-config/README.md)
* [Dynamo-Easy Config](api/dynamo-easy-config/configuration.md)
* [AWS SDK Config](api/dynamo-easy-config/aws-sdk-config.md)
* [Model](api/model/README.md)
* [Decorators](api/model/decorators.md)
* [Mapping Concept](api/model/mapping-concept.md)
* [CustomMapper](api/model/custommapper.md)
* [Dynamo Store](api/untitled/README.md)
* [Model Requests](api/untitled/model-requests.md)
* [Multi-Model Requests](api/multi-model-requests.md)

2 changes: 2 additions & 0 deletions docs/api/dynamo-easy-config/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Config

2 changes: 2 additions & 0 deletions docs/api/dynamo-easy-config/aws-sdk-config.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# AWS SDK Config

66 changes: 66 additions & 0 deletions docs/api/dynamo-easy-config/configuration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Dynamo-Easy Config

## Authorization

```typescript
import { updateDynamoEasyConfig } from '@shiftcoders/dynamo-easy'

updateDynamoEasyConfig({
sessionValidityEnsurer: ():Observable<void> => {
// do whatever you need to do to make sure the session is valid
// and return an Observable<void> when done
return of(true).pipe(map(() => { return }))
}
})
```

## Logging

To receive log statements from dynamo-easy you need to provide a function

```typescript
import { LogInfo, updateDynamoEasyConfig } from '@shiftcoders/dynamo-easy'

updateDynamoEasyConfig({
logReceiver: (logInfo: LogInfo) => {
const msg = `[${logInfo.level}] ${logInfo.timestamp} ${logInfo.className} (${
logInfo.modelClass
}): ${logInfo.message}`
console.debug(msg, logInfo.data)
}
})
```

## TableNameResolver

To use different table names per stage you can provide the tableNameResolver function.

This function will receive the default table name \(either resolved using the model name or custom value when a tableName was provided in the @Model decorator\) and needs to return the extended table name as string.

```typescript
import { TableNameResolver, updateDynamoEasyConfig } from '@shiftcoders/dynamo-easy'

const myTableNameResolver: TableNameResolver = (tableName: string) => {
return `myPrefix-${tableName}`
}

updateDynamoEasyConfig({
tableNameResolver: myTableNameResolver
})
```

## DateMapper

If you want to use a different type for the @DateProperty decorator \(eg. Moment or DayJS\) you need to define a custom mapper and provide it to the easy config.

```typescript
import { updateDynamoEasyConfig } from '@shiftcoders/dynamo-easy'
import { momentDateMapper } from './moment-date.mapper'

updateDynamoEasyConfig({
dateMapper: momentDateMapper
})
```

{% page-ref page="../model/custommapper.md" %}

2 changes: 2 additions & 0 deletions docs/api/model/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Model

29 changes: 29 additions & 0 deletions docs/api/model/custommapper.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
description: >-
With custom mappers you're able to define how the JS values are mapped (toDb)
and how the dynamoDB attributes are parsed (fromDb).
---

# CustomMapper

Scenarios you need to use a custom mapper:

* Using complex objects as partition key or sort key \(since dynamoDB only supports \[N\]umber \| \[S\]tring \| \[B\]inary for such\)
* working with class instances instead of plain javascript objects \(e.g. Dates\)
* Storing complex objects in dynamoDB set \(only N\|S\|B sets are possible\)

A mapper for date objects which would be stored as \[N\]umbers could look like this:

{% code-tabs %}
{% code-tabs-item title="date-to-number.mapper.ts" %}
```typescript
import { MapperForType, NumberAttribute } from '@shiftcoders/dynamo-easy'

export const dateToNumberMapper: MapperForType<Date, NumberAttribute> = {
fromDb: attributeValue => new Date(parseInt(attributeValue.N, 10)),
toDb: propertyValue => ({ N: `${propertyValue.getTime()}` }),
}
```
{% endcode-tabs-item %}
{% endcode-tabs %}

164 changes: 164 additions & 0 deletions docs/api/model/decorators.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
---
description: >-
Decorators are used to add some metadata to our model classes, relevant to our
javascript-to-dynamo mapper.
---

# Decorators

We rely on the reflect-metadata \([https://www.npmjs.com/package/reflect-metadata](https://www.npmjs.com/package/reflect-metadata)\) library for reflection api.

To get started with decorators just add a [@Model\(\)](https://shiftcode.github.io/dynamo-easy/modules/_decorator_impl_model_model_decorator_.html) Decorator to any typescript class.

If you need to read the metadata by hand for some purpose, use the [MetadataHelper](https://shiftcode.github.io/dynamo-easy/classes/_decorator_metadata_metadata_helper_.metadatahelper.html) to read the informations.

We make heavy usage of compile time informations about our models and the property types. Here is a list of the types that can be retrieved from compile time information for the key `design:type`. \(The metadata will only be added if at least one decorator is present on a property\)

* String
* Number
* Boolean
* Array \(no generics\)
* Custom Types
* ES6 types like Set, Map will be mapped to Object when calling for the type via Reflect.get\(design:type\), so we need some extra info.

Generic information is never available due to some serialization limitations at the time of writing.

## Model Decorators

### @Model

Use the `@Model` decorator to make it 'mappable' for the dynamo-easy mapper.
You can optionally pass an object containing the table name if you don't want the default table name.
The default table name is built with `${kebabCase(modelName)}s`

```typescript
import { Model } from '@shiftcoders/dynamo-easy'

@Model({tableName: 'my-model-table'})
class MyModel {
}
```

{% hint style="info" %}
To use different table names on different stages the [tableNameResolver](../dynamo-easy-config/configuration.md#tablenameresolver) is the right choice.
{% endhint %}

## Key Decorators

### Primary Key

* `PartitionKey`
* `SortKey`

```typescript
import { Model, PartitionKey, SortKey } from '@shiftcoders/dynamo-easy'

@Model()
class MyModel {
@PartitionKey()
myPartitionKey: string

@SortKey()
mySortKey: number
}
```

### Global Secondary Index

We provide two decorators to work with global secondary indexes:

* `GSIPartitionKey`
* `GSISortKey`

{% hint style="info" %}
You can use multiple GSI on the same model and also use the same property for different Indexes
{% endhint %}

```typescript
import { Model, GSIPartitionKey, GSISortKey } from '@shiftcoders/dynamo-easy'

@Model()
class MyModel {
@GSIPartitionKey('NameOfIndex')
myGsiPartitionKey: string

@GSISortKey('NameOfIndex')
myGsiSortKey: number
}
```

### Local Secondary Index

```typescript
import { Model, LSISortKey, PartitionKey, SortKey } from '@shiftcoders/dynamo-easy'

@Model()
class MyModel {
@PartitionKey()
myPartitionKey: string

@SortKey()
mySortKey: number

@LSISortKey()
myLsiSortKey: number
}
```

## Type Decorators

### @CollectionProperty\(options\)

The CollectionProperty decorator is used for arrays and sets. It defines if the values should be mapped to \[L\]ist or \[\(N\|S\|B\)S\]et and stores the information how the Attributes should be parsed.

#### options

`itemType: ModelConstructor` provide the class of the items inside the collection if they have decorators \(this ItemClass also needs the `@Model` decorator\)

`itemMapper: MapperForType` provide a custom mapper to map the complex items of your collection to \[S\]tring, \[N\]umber or \[B\]inary. This is mainly useful if you want to store them in a \[S\]et.

`sorted: boolean` the collection will be stored as \[L\]ist \(\[S\]et does not preserve the order\) no matter if the javascript type is a `Set` or an `Array` .

`name: string` define a different name \(than the property name\) for DynamoDB.

{% hint style="info" %}
it does no make sense to provide both, the itemType and an itemMapper. An Error would be thrown.
{% endhint %}

> further information how arrays and sets are mapped:
{% page-ref page="mapping-concept.md" %}

### @Property\(options\)

`name: string` define a different name \(than the property name\) for DynamoDB.

`mapper: MapperForType` define a custom mapper \(e.g if you want to use a complex object as PartitionKey or SortKey\)

### @DateProperty\(\)

The DateProperty decorator is just syntactic sugar for `@Property({mapper: dateMapper})`
all properties decorated with it will be mapped with the default dateMapper or the one you define with `updateDynamoEasyConfig({dateMapper:})`.

## Other

### @Transient

The `@Transient` decorator can be used to ignore a property when the object is mapped.

```typescript
import { Model, Transient } from '@shiftcoders/dynamo-easy'

@Model()
class MyModel {
@Transient()
myPropertyToIgnore: any
}
```







Loading

0 comments on commit 6f32508

Please sign in to comment.