Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

1.3 KB
Raw Blame
import type { UnknownAction } from "@reduxjs/toolkit";

import type { State } from "metabase/redux/store";
import type { Collection, CollectionId } from "metabase-types/api";

/**
 * Invalidation strategies for remote sync model mutations.
 */
export enum InvalidationType {
  /** Always invalidate on create/update/delete (for table children like fields, segments, measures) */
  Always = "always",
  /** Check collection's is_remote_synced status */
  CollectionBased = "collection_based",
}

export type InvalidationStrategy =
  | { type: InvalidationType.Always }
  | {
      type: InvalidationType.CollectionBased;
      getOriginalCollection: (
        state: State,
        id: CollectionId,
      ) => Collection | undefined;
    };

/**
 * Type for RTK Query endpoint matchers (matchFulfilled, matchPending, etc.)
 * These are type guard functions that narrow actions to their specific type.
 */
export type EndpointMatcher = (
  action: UnknownAction,
) => action is UnknownAction & { payload: unknown; meta: unknown };

export type ModelMutationConfig = {
  modelType: string;
  createEndpoints?: EndpointMatcher[];
  updateEndpoints?: EndpointMatcher[];
  deleteEndpoints?: EndpointMatcher[];
  invalidation: InvalidationStrategy;
  getDeleteId?: (action: UnknownAction) => number | { id: number } | undefined;
};