import { type CollectionReferenceRef, type DocumentReferenceRef, type FirestoreContextReference, type QueryLikeReferenceRef } from '../reference'; import { type FirestoreDocument, type FirestoreDocumentAccessorFactory, type FirestoreDocumentAccessorFactoryConfig, type FirestoreDocumentAccessorForTransactionFactory, type FirestoreDocumentAccessorForWriteBatchFactory, type LimitedFirestoreDocumentAccessorFactory, type LimitedFirestoreDocumentAccessorForTransactionFactory, type LimitedFirestoreDocumentAccessorForWriteBatchFactory, type LimitedFirestoreDocumentAccessor, type FirestoreDocumentAccessor } from '../accessor/document'; import { type FirestoreItemPageIterationBaseConfig, type FirestoreItemPageIterationFactory } from '../query/iterator'; import { type FirestoreQueryFactory } from '../query/query'; import { type FirestoreDrivers } from '../driver/driver'; import { type FirestoreCollectionCacheRef, type FirestoreCollectionCacheConfig } from '../cache/cache'; import { type FirestoreCollectionQueryFactory } from './collection.query'; import { type ArrayOrValue, type Maybe, type ModelKey, type ModelTypeString } from '@dereekb/util'; /** * The camelCase model name/type. * * This represents the entity type in the domain model and is used for type identification. * Model types should follow standard TypeScript naming conventions (typically PascalCase * or camelCase) and represent the singular form of the entity (e.g., 'user', 'post'). * * @example 'user', 'post', 'order' */ export type FirestoreModelType = ModelTypeString; /** * An all lowercase name that references a collection. Is usually the lowercase version of the FirestoreModelType. * * This is the part of the path that identifies what the collection is. Collection names are used in * Firestore paths and should follow Firestore naming conventions (lowercase, no spaces, no special characters, since "_" is reserved/used for TwoWayFlatFirestoreModelKey). * * Each collection name in the app should be unique, as usage of CollectionGroups would cause collections * with the same name to be returned regardless of their location in the document hierarchy. * * @example 'u', 'ps', 'or' * * @semanticType * @semanticTopic identifier * @semanticTopic string * @semanticTopic dereekb-firebase:firestore */ export type FirestoreCollectionName = string; /** * Separator used in Firestore paths to separate collection and document IDs. * This matches Firestore's internal path separator convention. */ export declare const FIRESTORE_COLLECTION_NAME_SEPARATOR = "/"; /** * Unique identifier for a nested collection type. Is the combination of all FirestoreCollectionNames of all parents. * * This type represents the full path of a collection in the document hierarchy, making it unique across * the entire Firestore database. It's used for collection group queries and for distinguishing between * collections with the same name but at different hierarchy levels. * * @example 'u', 'u/ps', 'u/or' */ export type FirestoreCollectionType = ModelTypeString; /** * Reference to a FirestoreCollectionType. * * This interface provides a way to reference a collection type without necessarily * having a concrete collection instance. It's used in type hierarchies and for * creating collection group queries. */ export interface FirestoreCollectionTypeRef { /** * The unique collection type identifier that represents this collection's position * in the document hierarchy. */ readonly collectionType: FirestoreCollectionType; } /** * Identifies whether a model is at the root level or nested within another collection. * * - 'root': The model exists in a top-level collection * - 'nested': The model exists in a subcollection of another document */ export type FirestoreModelIdentityType = 'root' | 'nested'; /** * A firestore model's identity. * * This composite type combines information about a model's type, collection name, * and position in the document hierarchy. It provides a complete picture of a model's * identity within the Firestore database structure. * * The identity is used for creating collections, documents, and queries with the * correct types and paths. * * @template M - The model type (e.g., 'profile', 'post') * @template C - The collection name (e.g., 'pr', 'po') */ export type FirestoreModelIdentity = FirestoreModelTypeRef & FirestoreCollectionNameRef & FirestoreCollectionTypeRef & { /** * Indicates whether this model exists at the root level or is nested within another collection. */ readonly type: FirestoreModelIdentityType; }; /** * Utility type to extract the model type from a FirestoreModelIdentity. * * @template I - The FirestoreModelIdentity to extract from */ export type FirestoreModelIdentityModelType = I extends FirestoreModelIdentity ? M : never; /** * Utility type to extract the collection name from a FirestoreModelIdentity. * * @template I - The FirestoreModelIdentity to extract from */ export type FirestoreModelIdentityCollectionName = I extends FirestoreModelIdentity ? C : never; /** * A root-level FirestoreModelIdentity. * * This represents a model that exists in a top-level collection, not nested within * any other documents. Root collections are directly accessible from the Firestore * database root. * * @template M - The model type (e.g., 'user', 'post') * @template C - The collection name (e.g., 'users', 'posts') */ export type RootFirestoreModelIdentity = FirestoreModelIdentity & { readonly type: 'root'; }; /** * A nested FirestoreModelIdentity with a parent. * * This represents a model that exists in a subcollection of another document. * The parent property provides access to the parent model's identity, enabling * navigation up the document hierarchy. * * @template P - The parent model's identity type * @template M - The model type (e.g., 'comment', 'attachment') * @template C - The collection name (e.g., 'comments', 'attachments') */ export type FirestoreModelIdentityWithParent

, M extends FirestoreModelType = FirestoreModelType, C extends FirestoreCollectionName = FirestoreCollectionName> = FirestoreModelIdentity & { readonly type: 'nested'; /** * Reference to the parent model's identity. */ readonly parent: P; }; /** * A default collection name derived from the model name. * * This utility type automatically converts a model type to a lowercase collection name, * following the convention that collection names are typically lowercase plurals of * the model name. It's used as a convenience when creating collections without * explicitly specifying a collection name. * * @template M - The model type * @example For model type 'User', this generates 'user' */ export type FirestoreModelDefaultCollectionName = `${Lowercase}`; /** * Utility type to extract the model types from a FirestoreModelIdentity. * This is similar to FirestoreModelIdentityModelType but preserves the template parameter * structure for more complex type operations. * * @template I - The FirestoreModelIdentity to extract from */ export type FirestoreModelTypes = I extends FirestoreModelIdentity ? M : never; /** * Creates a FirestoreModelIdentity value. * * @param modelName * @returns */ export declare function firestoreModelIdentity(modelName: M): RootFirestoreModelIdentity>; export declare function firestoreModelIdentity

, M extends FirestoreModelType>(parent: P, modelName: M): FirestoreModelIdentityWithParent>; export declare function firestoreModelIdentity(modelName: M, collectionName: C): RootFirestoreModelIdentity; export declare function firestoreModelIdentity

, M extends FirestoreModelType, C extends FirestoreCollectionName = FirestoreCollectionName>(parent: P, modelName: M, collectionName: C): FirestoreModelIdentityWithParent; /** * Map that maps FirestoreModelIdentity value's FirestoreModelType, FirestoreCollectionName, and FirestoreCollectionType value to the FirestoreModelType. */ export type FirestoreModelIdentityTypeMap = Map; /** * Creates a FirestoreModelIdentityTypeMap from the input identities. * * @param identities * @returns */ export declare function firestoreModelIdentityTypeMap(identities: Iterable): FirestoreModelIdentityTypeMap; /** * Reference to a FirestoreModelType */ export interface FirestoreModelTypeRef { /** * Returns the FirestoreModelType for this context. */ readonly modelType: M; } /** * Reads the FirestoreModelType from a FirestoreModelType or FirestoreModelTypeRef. * * @param modelTypeInput * @returns */ export declare function firestoreModelType(modelTypeInput: FirestoreModelType | FirestoreModelTypeRef): FirestoreModelType; /** * Reference to a FirestoreCollectionName */ export interface FirestoreCollectionNameRef { /** * Returns the FirestoreCollectionName for this context. */ readonly collectionName: C; } /** * Reference to a FirestoreModelIdentity via the FirestoreModelType and FirestoreCollectionName */ export interface FirestoreModelTypeModelIdentityRef { /** * Returns the FirestoreModelIdentity for this context. */ readonly modelIdentity: FirestoreModelIdentity; } /** * Reference to a FirestoreModelIdentity */ export interface FirestoreModelIdentityRef { /** * Returns the FirestoreModelIdentity for this context. */ readonly modelIdentity: I; } /** * The model's id within a collection. * * Different from the FirestoreModelKey, which is the full path in the databse. * * Example: * * 12345 * * @semanticType * @semanticTopic identifier * @semanticTopic string * @semanticTopic dereekb-firebase:firestore */ export type FirestoreModelId = string; /** * Input for firestoreModelId() */ export type FirestoreModelIdInput = FirestoreModelId | FirestoreModelKey | DocumentReferenceRef | FirestoreModelKeyRef | FirestoreModelIdRef; /** * Reads a firestoreModelId from the input. * * @param input * @returns */ export declare function firestoreModelId(input: FirestoreModelIdInput): FirestoreModelId; /** * Returns the array of ids within a FirestoreModelKey. * * @param input * @returns */ export declare function firestoreModelIdsFromKey(input: FirestoreModelKey | DocumentReferenceRef | FirestoreModelKeyRef): FirestoreModelId[]; /** * A FirestoreModelKey that has been "flat" to a FirestoreModelId. * * This is useful in cases where another object's is is derivative from a FirestoreModelKey. * * All slashes are removed from the object in order to make it a valid FirestoreModelId. */ export type FlatFirestoreModelKey = FirestoreModelId; /** * A FlatFirestoreModelKey that is encoded in a "one-way" manner by removing all slashes. * * This is useful for cases where the original ModelKey does not need to be inferred from the flat key. */ export type OneWayFlatFirestoreModelKey = FlatFirestoreModelKey; /** * Creates a OneWayFlatFirestoreModelKey from the input FirestoreModelKey. * * @param key * @returns */ export declare function flatFirestoreModelKey(key: FirestoreModelKey): OneWayFlatFirestoreModelKey; /** * A FlatFirestoreModelKey that is encoded in a "two-way" manner by replacing the slashes with underscores. * * This is useful for cases where the original ModelKey needs to be inferred from the flat key. */ export type TwoWayFlatFirestoreModelKey = FlatFirestoreModelKey; /** * Creates a TwoWayFlatFirestoreModelKey from the input FirestoreModelKey. * * @param key * @returns */ export declare function twoWayFlatFirestoreModelKey(key: FirestoreModelKey): TwoWayFlatFirestoreModelKey; /** * Creates a TwoWayFlatFirestoreModelKey from the input FirestoreModelKey. * * @param key * @returns */ export declare function inferKeyFromTwoWayFlatFirestoreModelKey(key: TwoWayFlatFirestoreModelKey): FirestoreModelKey; /** * Firestore Model Id Regex * * https://stackoverflow.com/questions/52850099/what-is-the-reg-expression-for-firestore-constraints-on-document-ids */ export declare const FIRESTORE_MODEL_ID_REGEX: RegExp; /** * Returns true if the input string is a FirestoreModelId. * * @param input - The string to test * @returns True if the input is a valid FirestoreModelId */ export declare function isFirestoreModelId(input: string | FirestoreModelId): input is FirestoreModelId; /** * Reference to a FirestoreModelId */ export interface FirestoreModelIdRef { /** * Returns the FirestoreModelId for this context. */ readonly id: FirestoreModelId; } /** * The full path for a model in the Firestore database. * * Example: * * collection/12345/subcollection/67890 */ export type FirestoreModelKey = ModelKey; /** * FirestoreModelKey and FirestoreCollectionType ref */ export type FirestoreModelKeyCollectionTypePair = FirestoreModelKeyRef & FirestoreCollectionTypeRef; /** * Creates a FirestoreModelKeyCollectionTypePair from the input FirestoreModelKey. * * @param key * @returns */ export declare function firestoreModelKeyCollectionTypePair(key: FirestoreModelKey): FirestoreModelKeyCollectionTypePair; /** * Firestore Model Key Regex that checks for pairs. */ export declare const FIRESTORE_MODEL_KEY_REGEX: RegExp; /** * Firestore Model Key Regex that is more strict */ export declare const FIRESTORE_MODEL_KEY_REGEX_STRICT: RegExp; /** * Returns true if the input string is a FirestoreModelKey. * * @param input - The string to test * @returns True if the input is a valid FirestoreModelKey */ export declare function isFirestoreModelKey(input: string | FirestoreModelKey): input is FirestoreModelKey; /** * Returns true if the input string is a FirestoreModelId or a FirestoreModelKey. * * @param input - The string to test * @returns True if the input is a valid FirestoreModelId or FirestoreModelKey */ export declare function isFirestoreModelIdOrKey(input: string | FirestoreModelId | FirestoreModelKey): input is FirestoreModelId | FirestoreModelKey; /** * A part of a FirestoreModelKey. */ export type FirestoreModelKeyPart = `${C}/${K}`; /** * One part of a FirestoreModelKe */ export type FirestoreCollectionModelKeyPart = N extends FirestoreCollectionNameRef ? FirestoreModelKeyPart : never; export type FirestoreCollectionModelKey = FirestoreCollectionModelKeyPart; /** * Creates a firestoreModelKeyPart * * @param identity * @param id * @returns */ export declare function firestoreModelKeyPart(identity: N, id: K): FirestoreCollectionModelKeyPart; /** * Creates a firestoreModelKey for RootFirestoreModelIdentity values. * * @param identity * @param id * @returns */ export declare const firestoreModelKey: (identity: I, id: K) => FirestoreCollectionModelKey; /** * Creates a FirestoreModelKey for root FirestoreModelIdentity values. */ export type FirestoreModelKeyFactory = (id: K) => FirestoreCollectionModelKey; /** * Creates a FirestoreModelKeyFactory for the input root identity. * * @param identity - The root FirestoreModelIdentity to bind the factory to * @returns A factory function that creates FirestoreModelKey values for the given identity and a provided id */ export declare function firestoreModelKeyFactory(identity: I): (id: K) => FirestoreCollectionModelKeyPart; /** * Creates an array of FirestoreCollectionModelKey values from the input ids. * * @param identity * @param ids * @returns */ export declare function firestoreModelKeys(identity: I, ids: K[]): FirestoreCollectionModelKey[]; /** * Creates a FirestoreModelKey from a parent key and a child identity and id. * * @param parent * @param identity * @param id * @returns */ export declare function childFirestoreModelKey(parent: ReadFirestoreModelKeyInput, identity: FirestoreModelIdentity, id: FirestoreModelId): FirestoreModelKey; /** * Creates a FirestoreModelKey array from a parent key, a child identity and an array of ids. * * @param parent * @param identity * @param ids * @returns */ export declare function childFirestoreModelKeys(parent: ReadFirestoreModelKeyInput, identity: FirestoreModelIdentity, ids: FirestoreModelId[]): FirestoreModelKey[]; /** * Joins together a number of FirestoreModelKeyPart values. * * @param parts * @returns */ export declare function firestoreModelKeyPath(...parts: FirestoreModelKeyPart[]): FirestoreModelKey; /** * Creates a number of child paths from the parent path. * * @param parent * @param children * @returns */ export declare function childFirestoreModelKeyPath(parent: FirestoreModelKeyPart, children: ArrayOrValue): FirestoreModelKey[]; export type FirestoreModelCollectionAndIdPairObject = Record; /** * Converts a FirestoreModelKey or reference into a record mapping each collection name to its document id. * * @param input - The FirestoreModelKey, DocumentReferenceRef, or FirestoreModelKeyRef to convert * @returns An object mapping collection names to document ids, or undefined if the key is unavailable */ export declare function firestoreModelKeyPairObject(input: FirestoreModelKey | DocumentReferenceRef | FirestoreModelKeyRef): Maybe; /** * String that is composed of the FirestoreCollectionNames derived from an input FirestoreModelKey and joined together via a separator. * * Is equivalent to a FirestoreCollectionType if the FIRESTORE_COLLECTION_NAME_SEPARATOR is used as the separator. * * @semanticType * @semanticTopic identifier * @semanticTopic string * @semanticTopic dereekb-firebase:firestore */ export type FirestoreModelCollectionTypeArrayName = string; /** * Returns the FirestoreCollectionType derived from the input FirestoreModelKey. * * @param input - The key or reference to extract the collection type from * @returns The FirestoreCollectionType string, or undefined if the key is unavailable */ export declare function firestoreModelKeyCollectionType(input: ReadFirestoreModelKeyInput): Maybe; /** * Returns the collection type array name string derived from the input key, joined by the given separator. * * @param input - The key or reference to extract collection names from * @param separator - The separator to join collection names with; defaults to the Firestore collection name separator * @returns The joined collection type array name string, or undefined if the key is unavailable */ export declare function firestoreModelKeyCollectionTypeArrayName(input: ReadFirestoreModelKeyInput, separator?: string): Maybe; /** * Returns the collection type array name string derived from a FirestoreModelIdentity, joined by the given separator. * * @param input - The FirestoreModelIdentity to derive collection names from * @param separator - The separator to join collection names with; defaults to the Firestore collection name separator * @returns The joined collection type array name string */ export declare function firestoreIdentityTypeArrayName(input: FirestoreModelIdentity, separator?: string): FirestoreModelCollectionTypeArrayName; export type FirestoreModelCollectionTypeArray = FirestoreCollectionName[]; /** * Returns an ordered array of collection names derived from a FirestoreModelIdentity, from root to leaf. * * @param input - The FirestoreModelIdentity to traverse * @returns An array of FirestoreCollectionName values ordered from root to leaf */ export declare function firestoreIdentityTypeArray(input: FirestoreModelIdentity): FirestoreModelCollectionTypeArray; /** * Returns an ordered array of collection names derived from the input FirestoreModelKey. * * @param input - The key or reference to extract collection names from * @returns An array of FirestoreCollectionName values, or undefined if the key is unavailable */ export declare function firestoreModelKeyCollectionTypeArray(input: ReadFirestoreModelKeyInput): Maybe; export interface FirestoreModelCollectionAndIdPair extends FirestoreModelIdRef, FirestoreCollectionNameRef { } /** * Returns the collection name of the input key. * * @param input - The key or reference to extract the collection name from * @returns The FirestoreCollectionName from the deepest key pair, or undefined if unavailable */ export declare function firestoreModelKeyCollectionName(input: ReadFirestoreModelKeyInput): Maybe; /** * Returns the parent model key from up the specified amount of levels. * * @param input - The key or reference to extract the parent key from * @param maxLevelsUp - The number of levels to traverse up the key hierarchy; defaults to 1 * @returns The parent FirestoreModelKey, or undefined if no parent exists */ export declare function firestoreModelKeyParentKey(input: ReadFirestoreModelKeyInput, maxLevelsUp?: number): Maybe; /** * Returns the collection/id pair array truncated by the specified number of levels from the end. * * @param input - The key or reference to extract pairs from * @param maxLevelsUp - The number of levels to remove from the end; defaults to 1 * @returns An array of FirestoreModelCollectionAndIdPair values up to the parent level, or undefined if unavailable */ export declare function firestoreModelKeyParentKeyPartPairs(input: ReadFirestoreModelKeyInput, maxLevelsUp?: number): Maybe; /** * Returns the last pair type from all generated pairs from the input. * * @param input * @returns */ export declare function firestoreModelKeyTypePair(input: ReadFirestoreModelKeyInput): Maybe; /** * Parses a FirestoreModelKey into an ordered array of collection/id pair objects. * * @param input - The key or reference to parse * @returns An array of FirestoreModelCollectionAndIdPair values, or undefined if the key is unavailable */ export declare function firestoreModelKeyPartPairs(input: ReadFirestoreModelKeyInput): Maybe; /** * Creates a FirestoreModelKey from the input pairs. * * @param input * @returns */ export declare function firestoreModelKeyPartPairsKeyPath(input: FirestoreModelCollectionAndIdPair[]): FirestoreModelKey; /** * Maps the input FirestoreModelCollectionAndIdPair[] values to FirestoreModelKeyPart[] values. * * @param input * @returns */ export declare function firestoreModelKeyPartPairsPaths(input: FirestoreModelCollectionAndIdPair[]): FirestoreModelKeyPart[]; export type ReadFirestoreModelKeyInput = FirestoreModelKey | FirestoreModelKeyRef | DocumentReferenceRef; /** * Reads a FirestoreModelKey from the input, which may be a raw key string, a key ref, or a document reference ref. * * @param input - The key, key ref, or document reference ref to read * @param required - Whether to throw if the key is not available; defaults to false * @returns The FirestoreModelKey string, or undefined if unavailable and not required */ export declare function readFirestoreModelKey(input: ReadFirestoreModelKeyInput, required: true): FirestoreModelKey; export declare function readFirestoreModelKey(input: ReadFirestoreModelKeyInput, required: false): Maybe; export declare function readFirestoreModelKey(input: ReadFirestoreModelKeyInput, required?: boolean): Maybe; export declare const FIRESTORE_DUMMY_MODEL_KEY = "dummymodel/dummykey"; /** * Dummy value used to pass validations or other cases where a key is required but ultimately not used. Is not meant to reference a real model. */ export type FirestoreDummyModelKey = typeof FIRESTORE_DUMMY_MODEL_KEY; /** * Returns the FirestoreDummyModelKey value. * * This function provides access to a special sentinel key that can be used * as a placeholder when a valid model key is required but not available. * It's useful in testing scenarios or when initializing structures that * require a key before one is available. * * @returns The dummy model key constant */ export declare function firestoreDummyKey(): FirestoreDummyModelKey; /** * Reference to a FirestoreModelKey */ export interface FirestoreModelKeyRef { /** * Returns the FirestoreModelKey for this context. */ readonly key: FirestoreModelKey; } /** * Base interface for accessing documents in a Firestore collection. * * Combines document accessor factories, query capabilities, and iteration support into a single * interface. This is the foundation for both {@link FirestoreCollection} (full access) and * collection group queries (limited access without collection reference). * * @template T - The document data type * @template D - The concrete FirestoreDocument subclass * @template A - The accessor type (limited or full) */ export interface FirestoreCollectionLike = FirestoreDocument, A extends LimitedFirestoreDocumentAccessor = LimitedFirestoreDocumentAccessor> extends FirestoreContextReference, FirestoreModelIdentityRef, QueryLikeReferenceRef, FirestoreItemPageIterationFactory, FirestoreQueryFactory, LimitedFirestoreDocumentAccessorFactory, LimitedFirestoreDocumentAccessorForTransactionFactory, LimitedFirestoreDocumentAccessorForWriteBatchFactory, FirestoreCollectionQueryFactory, FirestoreCollectionCacheRef { } /** * FirestoreCollection configuration */ export interface FirestoreCollectionConfig = FirestoreDocument> extends FirestoreContextReference, FirestoreDrivers, Omit, 'queryLike'>, Partial>, FirestoreDocumentAccessorFactoryConfig, Partial { } /** * Full Firestore collection interface with document CRUD, querying, iteration, and context support. * * Backs the `'root'` collection kind: a standard Firestore root collection. * Created at runtime via `firestoreContext.firestoreCollection({...})` and * typed at the model layer as * `FirestoreCollection = FirestoreCollection`. * * For other collection kinds, see: * - `'root-singleton'` ({@link RootSingleItemFirestoreCollection} in `./collection.single`) — single doc in a root collection. * - `'sub-collection'` ({@link FirestoreCollectionWithParent} in `./subcollection`) — multi-item subcollection under a parent. * - `'singleton-sub'` ({@link SingleItemFirestoreCollection} in `./subcollection.single`) — one document per parent. * * This is the primary interface used to interact with a Firestore collection. It provides: * - **Document access**: Create, load, and manage documents via {@link FirestoreDocumentAccessor} * - **Querying**: Build typed queries via the query factory * - **Iteration**: Paginate through results via firestore iteration factories * - **Context support**: Create accessors for transactions and write batches * * Created via {@link makeFirestoreCollection}. * * @template T - The document data type * @template D - The concrete FirestoreDocument subclass */ export interface FirestoreCollection = FirestoreDocument> extends FirestoreCollectionLike>, CollectionReferenceRef, FirestoreDocumentAccessorFactory, FirestoreDocumentAccessorForTransactionFactory, FirestoreDocumentAccessorForWriteBatchFactory { readonly config: FirestoreCollectionConfig; } /** * Ref to a FirestoreCollection */ export interface FirestoreCollectionRef = FirestoreDocument> { readonly firestoreCollection: FirestoreCollection; } /** * Creates a new FirestoreCollection instance from the provided configuration. * * This factory function initializes a Firestore collection with all the necessary * components for document access, querying, and data conversion. It sets up: * * 1. The collection reference with the proper converter * 2. Document accessor for CRUD operations * 3. Query factory for building typed queries * 4. Iteration utilities for paginated access * * @template T - The data type of documents in the collection * @template D - The FirestoreDocument type that wraps the data * @param inputConfig - Configuration for the collection * @returns A fully configured FirestoreCollection instance */ export declare function makeFirestoreCollection>(inputConfig: FirestoreCollectionConfig): FirestoreCollection;