/** * @module Firestore Subcollections * * This module provides utilities for working with Firestore subcollections - collections that exist * within a specific document rather than at the root level. Subcollections enable hierarchical * data modeling in Firestore by allowing documents to contain their own collections. * * Collection kind: `'sub-collection'` (multi-item subcollection under a parent). * For the one-document-per-parent variant, see `'singleton-sub'` * ({@link SingleItemFirestoreCollection} in `./subcollection.single`). * * Key features of subcollections: * - They provide a natural way to model hierarchical relationships * - They establish clear ownership boundaries (parent document owns child documents) * - They enable more granular security rules (access to child documents can depend on parent) * - They support efficient querying within the context of a specific parent * * Example hierarchy in Firestore paths: * - users/{userId} (parent document) * - users/{userId}/posts/{postId} (document in subcollection) * - users/{userId}/posts/{postId}/comments/{commentId} (nested subcollection) */ import { type FirestoreDocument } from '../accessor/document'; import { type FirestoreCollection, type FirestoreCollectionConfig } from './collection'; /** * Configuration for a Firestore subcollection that maintains a reference to its parent document. * * This configuration extends the standard FirestoreCollectionConfig with a reference to the * parent document, establishing a parent-child relationship between documents in different * collections. This relationship is used for building proper collection paths and maintaining * the document hierarchy. * * @template T - The data type of documents in the subcollection * @template PT - The data type of the parent document * @template D - The document type for documents in the subcollection, defaults to FirestoreDocument * @template PD - The document type for the parent document, defaults to FirestoreDocument */ export interface FirestoreCollectionWithParentConfig = FirestoreDocument, PD extends FirestoreDocument = FirestoreDocument> extends FirestoreCollectionConfig { /** * The parent document that contains this subcollection. * This reference establishes the hierarchical relationship and is used for path construction * and maintaining the document hierarchy context. */ readonly parent: PD; } /** * A FirestoreCollection that represents a subcollection with a reference to its parent document. * * Backs the `'sub-collection'` collection kind: a multi-item subcollection * under a parent. Created at runtime via `firestoreContext.firestoreCollectionWithParent({...})` * and typed at the model layer as `FirestoreCollection = FirestoreCollectionWithParent`. * * For one-document-per-parent subcollections, use {@link SingleItemFirestoreCollection} * (kind `'singleton-sub'`) instead. * * This interface extends FirestoreCollection to maintain the parent-child relationship * between documents. It allows access to documents within the context of their parent, * enabling proper path construction and maintaining the document hierarchy. * * @template T - The data type of documents in the subcollection * @template PT - The data type of the parent document * @template D - The document type for documents in the subcollection, defaults to FirestoreDocument * @template PD - The document type for the parent document, defaults to FirestoreDocument */ export interface FirestoreCollectionWithParent = FirestoreDocument, PD extends FirestoreDocument = FirestoreDocument> extends FirestoreCollection { /** * Reference to the parent document that contains this subcollection. * This allows navigation up the document hierarchy and provides context * for the subcollection's position in the data model. */ readonly parent: PD; } /** * A factory function type for creating subcollection instances for a given parent document. * * This type defines a function that creates a subcollection instance when given a parent document. * It's useful for establishing reusable patterns for accessing subcollections across different * parent documents of the same type. * * @template T - The data type of documents in the subcollection * @template PT - The data type of the parent document * @template D - The document type for documents in the subcollection, defaults to FirestoreDocument * @template PD - The document type for the parent document, defaults to FirestoreDocument * @template C - The specific subcollection type to return, defaults to FirestoreCollectionWithParent */ export type FirestoreCollectionWithParentFactory = FirestoreDocument, PD extends FirestoreDocument = FirestoreDocument, C extends FirestoreCollectionWithParent = FirestoreCollectionWithParent> = (parent: PD) => C; /** * Creates a new subcollection instance with a reference to its parent document. * * This factory function creates a subcollection that maintains its relationship to a * parent document. The subcollection inherits all the capabilities of a standard * collection while also providing access to its parent document context. * * @template T - The data type of documents in the subcollection * @template PT - The data type of the parent document * @template D - The document type for documents in the subcollection, defaults to FirestoreDocument * @template PD - The document type for the parent document, defaults to FirestoreDocument * @param config - Configuration for the subcollection, including the parent document reference * @returns A subcollection instance linked to the specified parent document */ export declare function makeFirestoreCollectionWithParent = FirestoreDocument, PD extends FirestoreDocument = FirestoreDocument>(config: FirestoreCollectionWithParentConfig): FirestoreCollectionWithParent;