import { type ArrayOrValue, type ModifierFunction } from '@dereekb/util'; import { type UserRelated } from '../../../model/user'; import { type DocumentReferenceRef } from '../reference'; import { type DocumentData, type SetOptions } from '../types'; import { type FirestoreDocumentDataAccessor, type FirestoreDocumentDataAccessorSetFunction } from './accessor'; import { AbstractFirestoreDocumentDataAccessorWrapper, type InterceptAccessorFactoryFunction } from './accessor.wrap'; /** * Controls when the modifier function is applied to document data. * * - `'always'`: Modifies data on every create, set, and update operation * - `'update'`: Only modifies when calling `set()` with merge options (updating existing documents) * - `'set'`: Only modifies when calling `create()` or `set()` without merge options (new documents) * - `'create'`: Only modifies on `create()` calls */ export type ModifyBeforeSetFistoreDataAccessorMode = 'always' | 'update' | 'set' | 'create'; /** * Input passed to the modifier function, containing the document data and reference context. */ export interface ModifyBeforeSetFistoreDataAccessorInput extends DocumentReferenceRef { /** * Data to pass to the modifyAndSet function. */ readonly data: Partial; /** * Set options passed to the set function, if available. */ readonly options?: SetOptions; } export type ModifyBeforeSetModifierFunction = ModifierFunction>; export interface ModifyBeforeSetConfig { /** * When to modify the input data. */ readonly when: ModifyBeforeSetFistoreDataAccessorMode; /** * Modifier or array of modifier functions to apply to input data. */ readonly modifier: ArrayOrValue>; } /** * Accessor wrapper that applies a modifier function to data before it is written to Firestore. * * The `when` mode in the config controls which write operations trigger the modifier. * Common use case: automatically copying the document ID into a `uid` field for {@link UserRelated} models * via {@link copyUserRelatedDataModifierConfig}. * * @template T - The document data type * @template D - The raw document data type in Firestore */ export declare class ModifyBeforeSetFirestoreDocumentDataAccessorWrapper extends AbstractFirestoreDocumentDataAccessorWrapper { readonly config: ModifyBeforeSetConfig; readonly modifier: ModifierFunction>; readonly set: FirestoreDocumentDataAccessorSetFunction; constructor(accessor: FirestoreDocumentDataAccessor, config: ModifyBeforeSetConfig); } /** * Creates a modifier function that copies the document reference's ID into the specified field on the data. * * Useful for models that need to store their own document ID as a field (e.g., `uid` on {@link UserRelated} models). * * @param fieldName - The field to copy the document ID into * @returns A modifier function that sets the field to the document's ID */ export declare function copyDocumentIdToFieldModifierFunction(fieldName: keyof T): ModifyBeforeSetModifierFunction; /** * Creates an {@link InterceptAccessorFactoryFunction} that wraps all created accessors with * {@link ModifyBeforeSetFirestoreDocumentDataAccessorWrapper} using the provided config. * * @param config - The ModifyBeforeSetConfig defining when and how to modify documents * @returns An InterceptAccessorFactoryFunction that wraps accessors with the modify-before-set behavior */ export declare function modifyBeforeSetInterceptAccessorFactoryFunction(config: ModifyBeforeSetConfig): InterceptAccessorFactoryFunction; /** * Creates a modifier that copies the document ID to the `uid` field for {@link UserRelated} models. * * @returns A ModifyBeforeSetModifierFunction that sets the `uid` field to the document's ID */ export declare function copyDocumentIdForUserRelatedModifierFunction(): ModifyBeforeSetModifierFunction; /** * Returns a pre-configured {@link ModifyBeforeSetConfig} for {@link UserRelated} models * that copies the document ID to the `uid` field on set operations (new document creation). * * @returns A ModifyBeforeSetConfig configured to copy the document ID to the `uid` field on set */ export declare function copyUserRelatedDataModifierConfig(): ModifyBeforeSetConfig; /** * Cached singleton factory for the UserRelated data modifier accessor interceptor. */ export declare const COPY_USER_RELATED_DATA_ACCESSOR_FACTORY_FUNCTION: import("@dereekb/util").CachedFactoryWithInput, unknown>; /** * Returns a typed {@link InterceptAccessorFactoryFunction} that applies the UserRelated * document ID copy modifier to all accessors created by the factory. * * @returns A typed InterceptAccessorFactoryFunction with the UserRelated document ID copy modifier applied */ export declare function copyUserRelatedDataAccessorFactoryFunction(): InterceptAccessorFactoryFunction;