11// Copyright (c) Dolittle. All rights reserved.
22// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33
4- import { ComplexValueMap } from '@dolittle/sdk.artifacts' ;
54import { IClientBuildResults } from '@dolittle/sdk.common' ;
65import { Constructor } from '@dolittle/types' ;
76
8- import { ProjectionField } from '../../Copies/ProjectionField ' ;
7+ import { ProjectionProperty } from '../../Copies/ProjectionProperty ' ;
98import { CollectionName , CollectionNameLike } from '../../Copies/MongoDB/CollectionName' ;
109import { Conversion } from '../../Copies/MongoDB/Conversion' ;
1110import { MongoDBCopies } from '../../Copies/MongoDB/MongoDBCopies' ;
11+ import { PropertyConversion } from '../../Copies/MongoDB/PropertyConversion' ;
1212import { ProjectionId } from '../../ProjectionId' ;
1313import { ReadModelField } from './../ReadModelField' ;
1414import { ICopyToMongoDBBuilder } from './ICopyToMongoDBBuilder' ;
1515
16+ type BuilderConversion = { property : string , convertTo : Conversion , children : BuilderConversion [ ] } ;
17+
1618/**
1719 * Represents an implementation of {@link ICopyToMongoDBBuilder}.
1820 * @template T The type of the projection read model.
1921 */
2022export class CopyToMongoDBBuilder < T > extends ICopyToMongoDBBuilder < T > {
2123 private _collectionName ?: CollectionName ;
22- private readonly _conversions : Map < ProjectionField , Conversion > = new ComplexValueMap ( ProjectionField , field => [ field . value ] , 1 ) ;
24+ private readonly _conversions : Map < string , Conversion > = new Map ( ) ;
2325
2426 /**
2527 * Initialises a new instance of the {@link CopyToMongoDBBuilder} class.
@@ -42,7 +44,7 @@ export class CopyToMongoDBBuilder<T> extends ICopyToMongoDBBuilder<T> {
4244
4345 /** @inheritdoc */
4446 withConversion ( field : ReadModelField < T > , to : Conversion ) : ICopyToMongoDBBuilder < T > {
45- this . _conversions . set ( ProjectionField . from ( field ) , to ) ;
47+ this . _conversions . set ( field , to ) ;
4648 return this ;
4749 }
4850
@@ -57,12 +59,59 @@ export class CopyToMongoDBBuilder<T> extends ICopyToMongoDBBuilder<T> {
5759 return undefined ;
5860 }
5961
60- return new MongoDBCopies ( true , this . _collectionName , this . _conversions ) ;
62+ return new MongoDBCopies ( true , this . _collectionName , this . buildPropertyConversions ( ) ) ;
6163 }
6264
6365 private inferCollectionNameFromType ( ) {
6466 if ( this . _readModelTypeOrInstance instanceof Function ) {
6567 this . _collectionName = CollectionName . from ( this . _readModelTypeOrInstance . name ) ;
6668 }
6769 }
70+
71+ private buildPropertyConversions ( ) : PropertyConversion [ ] {
72+ const conversions : BuilderConversion [ ] = [ ] ;
73+
74+ for ( const [ field , conversionType ] of this . _conversions ) {
75+ const properties = field . split ( '.' ) ;
76+ const conversion = this . makeConversionWithParents ( conversions , properties ) ;
77+ conversion . convertTo = conversionType ;
78+ }
79+
80+ return this . convertPropertyConversions ( conversions ) ;
81+ }
82+
83+ private makeConversionWithParents ( conversions : BuilderConversion [ ] , properties : string [ ] ) : BuilderConversion {
84+ const current = properties [ 0 ] ;
85+ const remainder = properties . slice ( 1 ) ;
86+
87+ for ( const conversion of conversions ) {
88+ if ( conversion . property === current ) {
89+ if ( remainder . length > 0 ) {
90+ return this . makeConversionWithParents ( conversion . children , remainder ) ;
91+ }
92+
93+ return conversion ;
94+ }
95+ }
96+
97+ const conversion = { property : current , convertTo : Conversion . None , children : [ ] } ;
98+ conversions . push ( conversion ) ;
99+ if ( remainder . length > 0 ) {
100+ return this . makeConversionWithParents ( conversion . children , remainder ) ;
101+ }
102+
103+ return conversion ;
104+ }
105+
106+ private convertPropertyConversions ( conversions : BuilderConversion [ ] ) : PropertyConversion [ ] {
107+ return conversions . map ( conversion =>
108+ new PropertyConversion (
109+ ProjectionProperty . from ( conversion . property ) ,
110+ conversion . convertTo ,
111+ false ,
112+ ProjectionProperty . from ( '' ) ,
113+ this . convertPropertyConversions ( conversion . children )
114+ )
115+ ) ;
116+ }
68117}
0 commit comments