Skip to content

Commit e3ef286

Browse files
authored
Merge pull request #116 from dolittle/copy-decorators
Copy decorators
2 parents 579a7e7 + 295e692 commit e3ef286

14 files changed

Lines changed: 199 additions & 24 deletions

Samples/Tutorials/Projections/DishCounter.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33

44
// Sample code for the tutorial at https://dolittle.io/tutorials/projections/typescript/
55

6-
import { ProjectionContext, projection, on } from '@dolittle/sdk.projections';
6+
import { ProjectionContext, projection, on, copyProjectionToMongoDB, convertToMongoDB, MongoDBConversion } from '@dolittle/sdk.projections';
77

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

1010
@projection('98f9db66-b6ca-4e5f-9fc3-638626c9ecfa')
11+
@copyProjectionToMongoDB()
1112
export class DishCounter {
13+
1214
name: string = 'Unknown';
15+
@convertToMongoDB(MongoDBConversion.Binary)
1316
numberOfTimesPrepared: number = 0;
1417

1518
@on(DishPrepared, _ => _.keyFromProperty('Dish'))
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
6+
import { CollectionName } from '../../Copies/MongoDB/CollectionName';
7+
8+
/**
9+
* Represents a projection decorated with the a 'copyToMongoDB' decorator.
10+
*/
11+
export class CopyProjectionToMongoDBDecoratedType {
12+
/**
13+
* Initialises a new instance of the {@link CopyProjectionToMongoDBDecoratedType} class.
14+
* @param {CollectionName} collection - The collection name to use.
15+
* @param {Constructor<any>} type - The decorated type.
16+
*/
17+
constructor(
18+
readonly collection: CollectionName,
19+
readonly type: Constructor<any>,
20+
) {}
21+
}

Source/projections/Builders/CopyToMongoDBBuilder.ts renamed to Source/projections/Builders/Copies/CopyToMongoDBBuilder.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ import { ComplexValueMap } from '@dolittle/sdk.artifacts';
55
import { IClientBuildResults } from '@dolittle/sdk.common';
66
import { Constructor } from '@dolittle/types';
77

8-
import { ProjectionField } from '../Copies/ProjectionField';
9-
import { CollectionName, CollectionNameLike } from '../Copies/MongoDB/CollectionName';
10-
import { Conversion } from '../Copies/MongoDB/Conversion';
11-
import { MongoDBCopies } from '../Copies/MongoDB/MongoDBCopies';
12-
import { ProjectionId } from '../ProjectionId';
8+
import { ProjectionField } from '../../Copies/ProjectionField';
9+
import { CollectionName, CollectionNameLike } from '../../Copies/MongoDB/CollectionName';
10+
import { Conversion } from '../../Copies/MongoDB/Conversion';
11+
import { MongoDBCopies } from '../../Copies/MongoDB/MongoDBCopies';
12+
import { ProjectionId } from '../../ProjectionId';
13+
import { ReadModelField } from './../ReadModelField';
1314
import { ICopyToMongoDBBuilder } from './ICopyToMongoDBBuilder';
14-
import { ReadModelField } from './ReadModelField';
1515

1616
/**
1717
* Represents an implementation of {@link ICopyToMongoDBBuilder}.

Source/projections/Builders/CopyToMongoDBCallback.ts renamed to Source/projections/Builders/Copies/CopyToMongoDBCallback.ts

File renamed without changes.

Source/projections/Builders/ICopyToMongoDBBuilder.ts renamed to Source/projections/Builders/Copies/ICopyToMongoDBBuilder.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
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 { CollectionNameLike } from '../Copies/MongoDB/CollectionName';
5-
import { Conversion } from '../Copies/MongoDB/Conversion';
6-
import { ReadModelField } from './ReadModelField';
4+
import { CollectionNameLike } from '../../Copies/MongoDB/CollectionName';
5+
import { Conversion } from '../../Copies/MongoDB/Conversion';
6+
import { ReadModelField } from './../ReadModelField';
77

88
/**
99
* Defines a builder for configuring read model copies to a MongoDB collection.
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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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+
export { convertToMongoDB, getConvertToMongoDBDecoratedProperties } from './convertToMongoDBDecorator';
5+
export { CopyProjectionToMongoDBDecoratedType } from './CopyProjectionToMongoDBDecoratedType';
6+
export { copyProjectionToMongoDB, isDecoratedCopyProjectionToMongoDB, getDecoratedCopyProjectionToMongoDB } from './copyProjectionToMongoDBDecorator';
7+
export { CopyToMongoDBBuilder } from './CopyToMongoDBBuilder';
8+
export { CopyToMongoDBCallback } from './CopyToMongoDBCallback';
9+
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+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
import { CollectionName, CollectionNameLike } from '../../Copies/MongoDB/CollectionName';
7+
8+
import { CopyProjectionToMongoDBDecoratedType } from './CopyProjectionToMongoDBDecoratedType';
9+
10+
const [decorator, getMetadata] = Decorators.createMetadataDecorator<CopyProjectionToMongoDBDecoratedType>('projection-copy-to-mongodb', 'copyProjectionToMongoDB', Decorators.DecoratorTarget.Class);
11+
12+
/**
13+
* Decorator to mark a Projection class to copy the read models to a MongoDB collection.
14+
* @param {CollectionNameLike} [collection] - An optional collection name to use, defaults to the name of the class.
15+
* @returns {Decorators.Decorator} The decorator.
16+
*/
17+
export function copyProjectionToMongoDB(collection?: CollectionNameLike): Decorators.Decorator {
18+
return decorator((target, type) => {
19+
return new CopyProjectionToMongoDBDecoratedType(
20+
CollectionName.from(collection ?? type.name),
21+
type);
22+
});
23+
}
24+
25+
/**
26+
* Checks whether the specified class is decorated with a copy to MongoDB decorator.
27+
* @param {Constructor<any>} type - The class to get the decorated copy to MongoDB for.
28+
* @returns {boolean} True if the decorator is applied, false if not.
29+
*/
30+
export function isDecoratedCopyProjectionToMongoDB(type: Constructor<any>): boolean {
31+
return getMetadata(type, false, false) !== undefined;
32+
}
33+
34+
/**
35+
* Gets the {@link CopyProjectionToMongoDBDecoratedType} of the specified class.
36+
* @param {Constructor<any>} type - The class to get the decorated copy to MongoDB for.
37+
* @returns {CopyProjectionToMongoDBDecoratedType} The decorated projection type.
38+
*/
39+
export function getDecoratedCopyProjectionToMongoDB(type: Constructor<any>): CopyProjectionToMongoDBDecoratedType {
40+
return getMetadata(type, true, 'Projection classes to be copied to MongoDB must be decorated');
41+
}

Source/projections/Builders/IProjectionBuilderForReadModel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import { Generation } from '@dolittle/sdk.artifacts';
88
import { EventType, EventTypeIdLike, ScopeId } from '@dolittle/sdk.events';
99

1010
import { ProjectionCallback } from '../ProjectionCallback';
11+
import { CopyToMongoDBCallback } from './Copies/CopyToMongoDBCallback';
1112
import { KeySelectorBuilderCallback } from './KeySelectorBuilderCallback';
12-
import { CopyToMongoDBCallback } from './CopyToMongoDBCallback';
1313

1414
/**
1515
* Defines a builder for building a projection for a read model from method callbacks.

0 commit comments

Comments
 (0)