Skip to content

Commit 1950ab8

Browse files
committed
Implement property conversion
1 parent dbe1844 commit 1950ab8

6 files changed

Lines changed: 82 additions & 8 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (c) Dolittle. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
import { Constructor } from '@dolittle/types';
5+
import { ProjectionField } from '../../Copies/ProjectionField';
6+
import { Conversion } from '../../Copies/MongoDB/Conversion';
7+
8+
/**
9+
* Represents a projection property decorated with a 'covertToMongoDB' decorator.
10+
*/
11+
export class MongoDBConversionDecoratedProperty {
12+
/**
13+
* Initialises a new instance of the {@link MongoDBConversionDecoratedProperty} class.
14+
* @param {ProjectionField} field - The projection field to be converted.
15+
* @param {Conversion} conversion - The conversion to apply.
16+
* @param {Constructor<any>} type - The decorated type.
17+
*/
18+
constructor(
19+
readonly field: ProjectionField,
20+
readonly conversion: Conversion,
21+
readonly type: Constructor<any>
22+
) { }
23+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
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+
export { convertToMongoDB, getConvertToMongoDBDecoratedProperties } from './convertToMongoDBDecorator';
45
export { CopyProjectionToMongoDBDecoratedType } from './CopyProjectionToMongoDBDecoratedType';
56
export { copyProjectionToMongoDB, isDecoratedCopyProjectionToMongoDB, getDecoratedCopyProjectionToMongoDB } from './copyProjectionToMongoDBDecorator';
67
export { CopyToMongoDBBuilder } from './CopyToMongoDBBuilder';
78
export { CopyToMongoDBCallback } from './CopyToMongoDBCallback';
89
export { ICopyToMongoDBBuilder } from './ICopyToMongoDBBuilder';
10+
export { MongoDBConversionDecoratedProperty } from './MongoDBConversionDecoratedProperty';
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) Dolittle. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
import { Decorators } from '@dolittle/sdk.common';
5+
import { Constructor } from '@dolittle/types';
6+
7+
import { ProjectionField } from '../../Copies/ProjectionField';
8+
import { Conversion } from '../../Copies/MongoDB/Conversion';
9+
import { MongoDBConversionDecoratedProperty } from './MongoDBConversionDecoratedProperty';
10+
11+
const [decorator, getMetadata] = Decorators.createMetadataDecorator<MongoDBConversionDecoratedProperty[]>('projection-copy-to-mongodb-conversions', 'convertToMongoDB', Decorators.DecoratorTarget.Property);
12+
13+
/**
14+
* Decorator for specifying conversions to be applied when producing read model copies to MongoDB.
15+
* @param {Conversion} conversion - The conversion to apply for the decorated property.
16+
* @returns {Decorators.Decorator} The decorator.
17+
*/
18+
export function convertToMongoDB(conversion: Conversion): Decorators.Decorator {
19+
return decorator((target, type, propertyKey, _ , mongoDBConversionDecoratedProperties) => {
20+
const properties = mongoDBConversionDecoratedProperties || [];
21+
22+
properties.push(new MongoDBConversionDecoratedProperty(
23+
ProjectionField.from(propertyKey as string),
24+
conversion,
25+
type));
26+
27+
return properties;
28+
});
29+
}
30+
31+
/**
32+
* Gets the MongoDB conversion decorated projection properties of the specified class.
33+
* @param {Constructor<any>} type - The class to get the MongoDB conversion decorated projection properties for.
34+
* @returns {MongoDBConversionDecoratedProperty[]} The MongoDB conversion decorated projection properties.
35+
*/
36+
export function getConvertToMongoDBDecoratedProperties(type: Constructor<any>): MongoDBConversionDecoratedProperty[] {
37+
return getMetadata(type) || [];
38+
}

Source/projections/Builders/Copies/copyProjectionToMongoDBDecorator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { CollectionName, CollectionNameLike } from '../../Copies/MongoDB/Collect
77

88
import { CopyProjectionToMongoDBDecoratedType } from './CopyProjectionToMongoDBDecoratedType';
99

10-
const [decorator, getMetadata] = Decorators.createMetadataDecorator<CopyProjectionToMongoDBDecoratedType>('copy-projection-to-mongodb', 'copyProjectionToMongoDB', Decorators.DecoratorTarget.Class);
10+
const [decorator, getMetadata] = Decorators.createMetadataDecorator<CopyProjectionToMongoDBDecoratedType>('projection-copy-to-mongodb', 'copyProjectionToMongoDB', Decorators.DecoratorTarget.Class);
1111

1212
/**
1313
* Decorator to mark a Projection class to copy the read models to a MongoDB collection.

Source/projections/Builders/ProjectionClassBuilder.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import { Guid, IEquatable } from '@dolittle/rudiments';
55
import { Constructor } from '@dolittle/types';
66

7-
import { Generation } from '@dolittle/sdk.artifacts';
7+
import { ComplexValueMap, Generation } from '@dolittle/sdk.artifacts';
88
import { IClientBuildResults } from '@dolittle/sdk.common';
99
import { EventType, EventTypeId, EventTypeIdLike, EventTypeMap, IEventTypes } from '@dolittle/sdk.events';
1010

@@ -13,12 +13,15 @@ import { IProjection } from '../IProjection';
1313
import { KeySelector } from '../KeySelector';
1414
import { Projection } from '../Projection';
1515
import { ProjectionCallback } from '../ProjectionCallback';
16-
import { OnDecoratedProjectionMethod } from './OnDecoratedProjectionMethod';
17-
import { getOnDecoratedMethods } from './onDecorator';
18-
import { ProjectionDecoratedType } from './ProjectionDecoratedType';
1916
import { ProjectionCopies } from '../Copies/ProjectionCopies';
17+
import { ProjectionField } from '../Copies/ProjectionField';
18+
import { Conversion } from '../Copies/MongoDB/Conversion';
2019
import { MongoDBCopies } from '../Copies/MongoDB/MongoDBCopies';
20+
import { getConvertToMongoDBDecoratedProperties } from './Copies/convertToMongoDBDecorator';
2121
import { getDecoratedCopyProjectionToMongoDB, isDecoratedCopyProjectionToMongoDB } from './Copies/copyProjectionToMongoDBDecorator';
22+
import { OnDecoratedProjectionMethod } from './OnDecoratedProjectionMethod';
23+
import { getOnDecoratedMethods } from './onDecorator';
24+
import { ProjectionDecoratedType } from './ProjectionDecoratedType';
2225

2326
/**
2427
* Represents a builder for building a projection from a class.
@@ -132,9 +135,14 @@ export class ProjectionClassBuilder<T> implements IEquatable {
132135
}
133136

134137
const decoratedType = getDecoratedCopyProjectionToMongoDB(this.type.type);
138+
const collection = decoratedType.collection;
135139

136-
// TODO: Conversions
140+
const decoratedProperties = getConvertToMongoDBDecoratedProperties(this.type.type);
141+
const conversions: Map<ProjectionField, Conversion> = new ComplexValueMap(ProjectionField, field => [field.value], 1);
142+
for (const decoratedProperty of decoratedProperties) {
143+
conversions.set(decoratedProperty.field, decoratedProperty.conversion);
144+
}
137145

138-
return new MongoDBCopies(true, decoratedType.collection, new Map());
146+
return new MongoDBCopies(true, collection, conversions);
139147
}
140148
}

Source/projections/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,16 @@ export {
3232
} from './Builders/_exports';
3333

3434
export {
35+
convertToMongoDB,
36+
getConvertToMongoDBDecoratedProperties,
37+
CopyProjectionToMongoDBDecoratedType,
3538
copyProjectionToMongoDB,
3639
isDecoratedCopyProjectionToMongoDB,
3740
getDecoratedCopyProjectionToMongoDB,
38-
CopyProjectionToMongoDBDecoratedType,
3941
CopyToMongoDBBuilder,
4042
CopyToMongoDBCallback,
4143
ICopyToMongoDBBuilder,
44+
MongoDBConversionDecoratedProperty,
4245
} from './Builders/Copies/_exports';
4346

4447
export {

0 commit comments

Comments
 (0)