Skip to content

Commit dbe1844

Browse files
committed
Implement the copyProjectionToMongoDB decorator
1 parent 0339808 commit dbe1844

6 files changed

Lines changed: 93 additions & 2 deletions

File tree

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+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
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 { CopyProjectionToMongoDBDecoratedType } from './CopyProjectionToMongoDBDecoratedType';
5+
export { copyProjectionToMongoDB, isDecoratedCopyProjectionToMongoDB, getDecoratedCopyProjectionToMongoDB } from './copyProjectionToMongoDBDecorator';
46
export { CopyToMongoDBBuilder } from './CopyToMongoDBBuilder';
57
export { CopyToMongoDBCallback } from './CopyToMongoDBCallback';
68
export { ICopyToMongoDBBuilder } from './ICopyToMongoDBBuilder';
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>('copy-projection-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/ProjectionClassBuilder.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import { OnDecoratedProjectionMethod } from './OnDecoratedProjectionMethod';
1717
import { getOnDecoratedMethods } from './onDecorator';
1818
import { ProjectionDecoratedType } from './ProjectionDecoratedType';
1919
import { ProjectionCopies } from '../Copies/ProjectionCopies';
20+
import { MongoDBCopies } from '../Copies/MongoDB/MongoDBCopies';
21+
import { getDecoratedCopyProjectionToMongoDB, isDecoratedCopyProjectionToMongoDB } from './Copies/copyProjectionToMongoDBDecorator';
2022

2123
/**
2224
* Represents a builder for building a projection from a class.
@@ -55,8 +57,9 @@ export class ProjectionClassBuilder<T> implements IEquatable {
5557
results.addFailure(`Could not create projection ${this.type.type.name} because it contains invalid projection methods`, 'Maybe you have multiple @on methods handling the same event type?');
5658
return;
5759
}
58-
const copies = ProjectionCopies.default;
59-
//TODO: Create copies.
60+
61+
const copies = this.buildCopies(results);
62+
6063
return new Projection<T>(this.type.projectionId, this.type.type, this.type.scopeId, events, copies);
6164
}
6265

@@ -116,4 +119,22 @@ export class ProjectionClassBuilder<T> implements IEquatable {
116119
return instance;
117120
};
118121
}
122+
123+
private buildCopies(results: IClientBuildResults): ProjectionCopies {
124+
return new ProjectionCopies(
125+
this.buildMongoDBCopies(results),
126+
);
127+
}
128+
129+
private buildMongoDBCopies(results: IClientBuildResults): MongoDBCopies {
130+
if (!isDecoratedCopyProjectionToMongoDB(this.type.type)) {
131+
return MongoDBCopies.default;
132+
}
133+
134+
const decoratedType = getDecoratedCopyProjectionToMongoDB(this.type.type);
135+
136+
// TODO: Conversions
137+
138+
return new MongoDBCopies(true, decoratedType.collection, new Map());
139+
}
119140
}

Source/projections/Builders/_exports.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
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 * as Copies from './Copies/_exports';
5+
46
export { CouldNotCreateInstanceOfProjection } from './CouldNotCreateInstanceOfProjection';
57
export { IProjectionBuilder } from './IProjectionBuilder';
68
export { IProjectionBuilderForReadModel } from './IProjectionBuilderForReadModel';

Source/projections/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ export {
3232
} from './Builders/_exports';
3333

3434
export {
35+
copyProjectionToMongoDB,
36+
isDecoratedCopyProjectionToMongoDB,
37+
getDecoratedCopyProjectionToMongoDB,
38+
CopyProjectionToMongoDBDecoratedType,
3539
CopyToMongoDBBuilder,
3640
CopyToMongoDBCallback,
3741
ICopyToMongoDBBuilder,

0 commit comments

Comments
 (0)