/** * Effect-based upsert operations for entities. * * Uses Ref for atomic state mutation, Effect Schema for validation, * and typed errors (ValidationError, ForeignKeyError). * * Upsert = find by `where` clause → update if exists, create if not. */ import { Effect, PubSub, Ref, type Schema } from "effect"; import type { ForeignKeyError, HookError, UniqueConstraintError, ValidationError } from "../../errors/crud-errors.js"; import type { UpsertInternalInput, UpsertManyResult, UpsertResult } from "../../types/crud-types.js"; import type { DerivedIdConfig } from "../../types/database-config-types.js"; import type { HooksConfig } from "../../types/hook-types.js"; import type { CollectionIndexes } from "../../types/index-types.js"; import type { ChangeEvent } from "../../types/reactive-types.js"; import type { SearchIndexMap } from "../../types/search-types.js"; import { type NormalizedConstraints } from "./unique-check.js"; type HasId = { readonly id: string; }; type RelationshipConfig = { readonly type: "ref" | "inverse"; readonly target: string; readonly foreignKey?: string; }; /** * Upsert a single entity: find by `where`, update if exists, create if not. * * Steps: * 1. Look up entity by where clause in Ref state * 2a. If found: run beforeUpdate hooks, apply update operators, validate, update in state, run afterUpdate/onChange * 2b. If not found: merge where + create data, generate ID/timestamps, validate, run beforeCreate hooks, add to state, run afterCreate/onChange * 3. Validate foreign key constraints * 4. Return entity with __action metadata */ export declare const upsert: (collectionName: string, schema: Schema.Codec, relationships: Record, ref: Ref.Ref>, stateRefs: Record>>, indexes?: CollectionIndexes, hooks?: HooksConfig, uniqueFields?: NormalizedConstraints, searchIndexRef?: Ref.Ref, searchIndexFields?: ReadonlyArray, changePubSub?: PubSub.PubSub, derivedId?: DerivedIdConfig) => (input: UpsertInternalInput) => Effect.Effect, ValidationError | ForeignKeyError | HookError | UniqueConstraintError>; /** * Upsert multiple entities efficiently. * * For each input: * - If entity matches where clause: apply updates (or skip if unchanged) * - If no match: create new entity * * All changes validated and applied atomically. * Returns categorized results: created, updated, unchanged. * * Runs hooks per entity: * - Create path: beforeCreate → mutation → afterCreate → onChange("create") * - Update path: beforeUpdate → mutation → afterUpdate → onChange("update") */ export declare const upsertMany: (collectionName: string, schema: Schema.Codec, relationships: Record, ref: Ref.Ref>, stateRefs: Record>>, indexes?: CollectionIndexes, hooks?: HooksConfig, uniqueFields?: NormalizedConstraints, searchIndexRef?: Ref.Ref, searchIndexFields?: ReadonlyArray, changePubSub?: PubSub.PubSub, derivedId?: DerivedIdConfig) => (inputs: ReadonlyArray>) => Effect.Effect, ValidationError | ForeignKeyError | HookError | UniqueConstraintError>; export {}; //# sourceMappingURL=upsert.d.ts.map