Skip to content

Commit

Permalink
46
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielSchaffer committed Mar 17, 2019
1 parent 91f84aa commit 6165450
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 48 deletions.
3 changes: 1 addition & 2 deletions packages/dandi/core/src/injector.ts
@@ -1,7 +1,6 @@
import { AppInjectorContext } from '@dandi/core/src/app-injector-context'

import { Constructor } from '@dandi/common'

import { AppInjectorContext } from './app-injector-context'
import { InjectionContext } from './injection-context'
import { InjectionToken } from './injection-token'
import { InjectionResult } from './injection-result'
Expand Down
45 changes: 1 addition & 44 deletions packages/dandi/model-builder/README.md
Expand Up @@ -4,7 +4,7 @@ The `@dandi/model-builder` package contains utilities for dynamically
constructing and validating models defined using decorators from
`@dandi/model`.

While `ModelBuilder` can be used on its own, it is used by several other
`ModelBuilder` can be used on its own, and it is used by several other
Dandi packages to provide automatic construction and validation of
data models:

Expand All @@ -15,46 +15,3 @@ data models:
to their defined models when using `queryModel` and `queryModelSingle`
- `@dandi-contrib/aws-lambda` - converts incoming AWS event data to Dandi
models

## ModelBuilder

The `ModelBuilder` interface exposes two methods for working with models.
`constructModel` is used for converting a POJO ("Plain Old JavaScript
Object") into an instance of a desired model class. `constructMember` is
used to convert individual values to a specified type.

## ModelBuilderOptions

Providing options to `constructModel` or `constructMember` calls will
allow you to configure validation, transform the keys of your source
object, or for `constructModel`, transform the source object itself
before construction.

- **`validators`:** An array of `ModelValidator` instances that will be
used to validate the constructed model.
- **`keyTransform`:** A `KeyTransformFn` that will be used to modify the
keys of the source object when mapping properties to the constructed
model. This is useful when dealing with external data sources that
provide data is in one case (e.g. `snake_case`), and your models are
written in another (hopefully, `camelCase`).
- **`dataTransformers`:** An array of `DataTransformer` instances that
will modify the source object before starting construction of the model.
This can be used to do just about anything, but as an example, it can
be used with `@dandi-contrib/data-pg` and `NestedKeyTransformer` to convert a
flat POJO with dot notated keys to a complex object with nested
properties.

## MetadataModelBuilder

`MetadataModelBuilder` is the default implementation of `ModelBuilder`
and is included in the `@dandi/model-builder` package.

`MetadataModelBuilder` uses the metadata generated by using decorators
from `@dandi/model` to define a model. Some things to note about using
`MetadataModelBuilder`:

- Nested models are supported - it will recurse into each complex type
and convert any properties defined by the `@dandi/model` decorators.
- When using the `keyTransform` option, it will _not_ transform the keys
of `Map` properties (defined with `MapOf`), or JSON properties
(specified by adding `Json`).
43 changes: 43 additions & 0 deletions packages/dandi/model-builder/docs/DOCS.md
@@ -0,0 +1,43 @@
## ModelBuilder

The [[ModelBuilder]] interface exposes two methods for working with models.
[[ModelBuilder.constructModel]] is used for converting a POJO ("Plain
Old JavaScript Object") into an instance of a desired model class.
[[ModelBuilder.constructMember]] is used to convert individual values to
a specified type.

## ModelBuilderOptions

Providing options to `constructModel` or `constructMember` calls will
allow you to configure validation, transform the keys of your source
object, or for `constructModel`, transform the source object itself
before construction.

- **`validators`:** An array of [[ModelValidator]] instances that will be
used to validate the constructed model.
- **`keyTransform`:** A `KeyTransformFn` that will be used to modify the
keys of the source object when mapping properties to the constructed
model. This is useful when dealing with external data sources that
provide data is in one case (e.g. `snake_case`), and your models are
written in another (hopefully, `camelCase`).
- **`dataTransformers`:** An array of [[DataTransformer]] instances that
will modify the source object before starting construction of the model.
This can be used to do just about anything, but as an example, it can
be used with [[@dandi-contrib/data-pg]] and [[NestedKeyTransformer]]
to convert a flat POJO with dot notated keys to a complex object with
nested properties.

## MetadataModelBuilder

[[MetadataModelBuilder]] is the default implementation of [[ModelBuilder]]
and is included in the [[@dandi/model-builder]] package.

[[MetadataModelBuilder]] uses the metadata generated by using decorators
from [[@dandi/model]] to define a model. Some things to note about using
[[MetadataModelBuilder]]:

- Nested models are supported - it will recurse into each complex type
and convert any properties defined by the [[@dandi/model]] decorators.
- When using the `keyTransform` option, it will _not_ transform the keys
of `Map` properties (defined with [[@MapOf()]]), or JSON properties
(specified by adding [[@Json()]]).
10 changes: 9 additions & 1 deletion packages/dandi/model-builder/index.ts
@@ -1,7 +1,15 @@
/**
* @module
* [[include:README.md]]
* [[include:docs/DOCS.md]]
*/

/**
* @ignore - this placeholder required so that typedoc uses the above module comment
*/
export * from './src/currency.type.converter'
export * from './src/data.transformer'
export * from './src/date.time.type.converter'
// export * from './src/key.mapper.factory';
export * from './src/key.transformer'
export * from './src/metadata.model.builder'
export * from './src/metadata.model.validator'
Expand Down
3 changes: 3 additions & 0 deletions packages/dandi/model-builder/src/currency.type.converter.ts
Expand Up @@ -4,6 +4,9 @@ import { MemberMetadata } from '@dandi/model'

import { TypeConverter } from './type.converter'

/**
* An implementation of [[TypeConverter]] that converts [[Currency]] values.
*/
@Injectable(TypeConverter)
export class CurrencyTypeConverter implements TypeConverter<Currency> {
public readonly type = Currency;
Expand Down
6 changes: 5 additions & 1 deletion packages/dandi/model-builder/src/data.transformer.ts
@@ -1,3 +1,7 @@
/**
* A service that transforms source objects before they are used for constructing and
* validating models with [[ModelBuilder]]
*/
export interface DataTransformer {
transform(source: any): any;
transform(source: any): any
}

0 comments on commit 6165450

Please sign in to comment.