|
| 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 | +} |
0 commit comments