proposit-core
    Preparing search index...

    Function orderChangeset

    • Converts a changeset into a flat, ordered array of persistence operations that is safe to execute sequentially against a relational store with foreign-key constraints.

      The FK dependency chain is:

      • expression.premiseIdpremise.id
      • expression.variableIdvariable.id (for variable-type expressions)
      • expression.parentIdexpression.id (self-FK for tree structure)
      • variable.argumentIdargument.id
      • premise.argumentIdargument.id

      The resulting order guarantees that every referenced row exists before any row that depends on it is inserted, and that every dependent row is removed before the row it references is deleted.

      Ordering phases:

      1. Update premises — ensure premise rows have correct metadata before dependent deletes run.
      2. Reparent expressions — update expressions whose IDs are NOT in the removed set. This detaches reparented children from doomed parents before ON DELETE CASCADE runs. Expressions that appear in both modified and removed are skipped (the row is about to be deleted).
      3. Delete expressions — expression rows hold FKs to variables and premises, so they must be removed first.
      4. Delete variables — safe after expression deletes (no remaining FK references from expressions).
      5. Delete premises — safe after all child rows are removed.
      6. Insert premises — new premises must exist before their expressions and variables can be inserted.
      7. Insert variables — new variables must exist before variable-type expressions can reference them.
      8. Insert expressions — topologically sorted so parent expressions are inserted before their children (satisfies the parentId self-FK).
      9. Update variables — grouped after inserts for clarity.
      10. (No-op — expression updates are now emitted in Phase 2.)
      11. Update argument metadata — if present.
      12. Update role state — if present.

      Type Parameters

      • TExpr extends TCorePropositionalExpression = TCorePropositionalExpression
      • TVar extends
            | {
                argumentId: string;
                argumentVersion: number;
                checksum: string;
                claimId: string;
                claimVersion: number;
                id: string;
                symbol: string;
            }
            | {
                argumentId: string;
                argumentVersion: number;
                boundArgumentId: string;
                boundArgumentVersion: number;
                boundPremiseId: string;
                checksum: string;
                id: string;
                symbol: string;
            } =
            | {
                argumentId: string;
                argumentVersion: number;
                checksum: string;
                claimId: string;
                claimVersion: number;
                id: string;
                symbol: string;
            }
            | {
                argumentId: string;
                argumentVersion: number;
                boundArgumentId: string;
                boundArgumentVersion: number;
                boundPremiseId: string;
                checksum: string;
                id: string;
                symbol: string;
            }
      • TPremise extends
            | {
                argumentId: string;
                argumentVersion: number;
                checksum: string;
                combinedChecksum: string;
                descendantChecksum: string
                | null;
                id: string;
                type: "freeform";
            }
            | {
                argumentId: string;
                argumentVersion: number;
                checksum: string;
                combinedChecksum: string;
                derivedClaimId: string;
                descendantChecksum: string
                | null;
                id: string;
                type: "derivation";
            } =
            | {
                argumentId: string;
                argumentVersion: number;
                checksum: string;
                combinedChecksum: string;
                descendantChecksum: string
                | null;
                id: string;
                type: "freeform";
            }
            | {
                argumentId: string;
                argumentVersion: number;
                checksum: string;
                combinedChecksum: string;
                derivedClaimId: string;
                descendantChecksum: string
                | null;
                id: string;
                type: "derivation";
            }
      • TArg extends {
            checksum: string;
            combinedChecksum: string;
            descendantChecksum: string | null;
            id: string;
            version: number;
        } = {
            checksum: string;
            combinedChecksum: string;
            descendantChecksum: string | null;
            id: string;
            version: number;
        }

      Parameters

      Returns TOrderedOperation<TExpr, TVar, TPremise, TArg>[]

      A flat array of TOrderedOperation entries in FK-safe execution order. Returns an empty array if the changeset is empty.