import type { TypesaurusUtils as Utils } from "./utils.js"; import type { TypesaurusCore as Core } from "./core.js"; import type { TypesaurusUpdate as Update } from "./update.js"; export declare const transaction: TypesaurusTransaction.Function; export declare namespace TypesaurusTransaction { interface Function { (db: Core.DB, options?: Core.OperationOptions): ReadChain; } /** * The document reference type. */ interface ReadRef { type: "ref"; collection: ReadCollection; id: Def["Id"]; } /** * The document reference type. */ interface WriteRef extends DocAPI { type: "ref"; collection: WriteCollection; id: Def["Id"]; } interface DocAPI { set(data: Core.AssignArg, Props>): void; update(data: Update.Arg): void; upset(data: Core.AssignArg, Props>): void; remove(): void; } /** * The document type. It contains the reference in the DB and the model data. */ interface ReadDoc { type: "doc"; ref: ReadRef; data: Props["environment"] extends "server" ? Core.ServerData> : Core.Data, "present">; props: Props; } /** * The document type. It contains the reference in the DB and the model data. */ interface WriteDoc extends DocAPI { type: "doc"; ref: WriteRef; data: Props["environment"] extends "server" ? Core.ServerData> : Core.Data, "present">; props: Props; narrow(fn: Core.DocNarrowFunction, NarrowToModel>): WriteDoc<{ Model: NarrowToModel; Name: Def["Name"]; Id: Def["Id"]; WideModel: Def["WideModel"]; Flags: Def["Flags"] & { Reduced: true; }; }, Props> | undefined; } type AnyWriteCollection = WriteCollection | NestedWriteCollection>; interface WriteSchema { [CollectionPath: string]: AnyWriteCollection; } interface NestedWriteCollection> extends WriteCollection { (id: Def["Id"]): NestedSchema; } /** * */ interface WriteCollection { /** The Firestore path */ path: string; set(id: Def["Id"], data: Core.AssignArg, Props>): void; upset(id: Def["Id"], data: Core.AssignArg, Props>): void; update(id: Def["Id"], data: Update.Arg): void; remove(id: Def["Id"]): void; } type AnyReadCollection = ReadCollection | NestedReadCollection>; interface ReadSchema { [CollectionPath: string]: AnyReadCollection; } interface NestedReadCollection> extends ReadCollection { (id: Def["Id"]): NestedSchema; } interface ReadCollection extends Core.PlainCollection { /** The Firestore path */ path: string; get(id: Def["Id"]): Promise | null>; } /** * The transaction read API object. It contains {@link ReadHelpers.get|get} * the function that allows reading documents from the database. */ interface ReadHelpers { db: ReadDB; } /** * The transaction write API object. It unions a set of functions ({@link WriteHelpers.set|set}, * {@link WriteHelpers.update|update} and {@link WriteHelpers.remove|remove}) * that are similar to regular set, update and remove with the only * difference that the transaction counterparts will retry writes if * the state of data received with {@link ReadHelpers.get|get} would change. */ interface WriteHelpers { /** The result of the read function. */ result: ReadDocsToWriteDocs; db: WriteDB; } type ReadDocsToWriteDocs = Result extends ReadDoc ? WriteDoc : Result extends ReadRef ? WriteRef : Result extends object ? { [Key in keyof Result]: ReadDocsToWriteDocs; } : Result; interface ReadChain { read: (callback: ReadFunction) => WriteChain; } /** * The transaction body function type. */ type ReadFunction = ($: ReadHelpers) => Promise; interface WriteChain { write: (callback: WriteFunction) => WriteDocsToDocs; } /** * The transaction body function type. */ type WriteFunction = ($: WriteHelpers) => WriteResult; type ReadDB = { [Name in keyof Schema]: Name extends string ? Schema[Name] extends Core.NestedPlainCollection ? NestedReadCollection<{ Model: Model; Name: CustomName extends string ? CustomName : Name; Id: CustomId extends Core.Id | string ? CustomId : Core.Id>; WideModel: Model; Flags: Core.DocDefFlags; }, Props, ReadDB>> : Schema[Name] extends Core.PlainCollection ? ReadCollection<{ Model: Model; Name: CustomName extends string ? CustomName : Name; Id: CustomId extends Core.Id | string ? CustomId : Core.Id>; WideModel: Model; Flags: Core.DocDefFlags; }, Props> : never : never; }; type WriteDocsToDocs = Result extends WriteDoc ? Core.Doc : Result extends WriteRef ? Core.Ref : Result extends object ? { [Key in keyof Result]: WriteDocsToDocs; } : Result; type WriteDB = { [Name in keyof Schema]: Name extends string ? Schema[Name] extends Core.NestedPlainCollection ? NestedWriteCollection<{ Model: Model; Name: CustomName extends string ? CustomName : Name; Id: CustomId extends Core.Id | string ? CustomId : Core.Id>; WideModel: Model; Flags: Core.DocDefFlags; }, Props, WriteDB>> : Schema[Name] extends Core.PlainCollection ? WriteCollection<{ Model: Model; Name: CustomName extends string ? CustomName : Name; Id: CustomId extends Core.Id | string ? CustomId : Core.Id>; WideModel: Model; Flags: Core.DocDefFlags; }, Props> : never : never; }; }