// Copyright (c) Cratis. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. import 'reflect-metadata'; import { TypeIntrospector } from '../../types/index.js'; import { decorateModelBoundProperty } from '../../types/modelBoundProperty.js'; import { ChroniclePropertyDecorator, getPropertyMetadata } from '../../types/propertyDecoratorMetadata.js'; import { getFromAllEventsMetadata } from './fromAllEvents.js'; /** Metadata stored by the fromAll property decorator. */ export interface FromAllMetadata { /** The event property name to read the value from. */ readonly property?: string; /** The event context property name to read the value from. */ readonly contextProperty?: string; } const METADATA_KEY = 'chronicle:projection:fromAll'; /** * Property decorator that sets the decorated read model property from a property present on every * event type the projection is built from - the convention-based counterpart to declaring the same * mapping on every individual `fromEvent`. Equivalent to {@link fromEvery} for model-bound * projections. * @deprecated @fromAll remains a supported, deprecated alias for restricted mappings; prefer @fromEvery. * Contrary to the earlier 'next major' announcement, its behavior will not change; * use @fromAllEvents to opt into all-event subscriptions. * @param property - Optional event property name. If not specified, uses the model property name. * @param contextProperty - Optional event context property name. * @returns A property decorator. */ export function fromAll(property?: string, contextProperty?: string): ChroniclePropertyDecorator { return decorateModelBoundProperty((target: object, propertyKey: string | symbol) => { TypeIntrospector.trackProperty((target as { constructor: Function }).constructor, propertyKey.toString()); const metadata: FromAllMetadata = { property, contextProperty }; Reflect.defineMetadata(METADATA_KEY, metadata, target, propertyKey.toString()); }); } /** * Retrieves fromAll metadata stored on the given property, falling back to fromAllEvents metadata * for compatibility with existing model discovery. This reader does not imply an all-event subscription. * @param target - The class prototype. * @param propertyKey - The property name. * @returns The fromAll metadata, or undefined if not decorated. */ export function getFromAllMetadata(target: object, propertyKey: string): FromAllMetadata | undefined { return getPropertyMetadata(METADATA_KEY, target, propertyKey) ?? getFromAllEventsMetadata(target, propertyKey); }