1 parent
9c23e3198b
commit
0fd1d6108f
12 files changed
+1007
-184
No files matched your search
@@ -1,7 +1,7 @@
|
||||
// @vitest-environment happy-dom
|
||||
import { computed, nextTick, ref, type Ref } from "vue";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { clearDataGridPendingSnapshot, DATA_GRID_QUICK_ENTRY_DRAFT_ROW_ID, useDataGridEditor } from "@/composables/useDataGridEditor";
|
||||
import { clearDataGridPendingSnapshot, DATA_GRID_QUICK_ENTRY_DRAFT_ROW_ID, useDataGridEditor, type CustomSaveHandler } from "@/composables/useDataGridEditor";
|
||||
import { clearDataGridClipboardCopy, parseDataGridClipboard, rememberDataGridClipboardCopy } from "@/lib/dataGrid/dataGridClipboard";
|
||||
import { buildMongoUpdateDocument, MONGO_DOCUMENT_GRID_NULL, mongoDocumentGridInputValue, mongoDocumentGridValue } from "@/lib/mongo/mongoDocumentValues";
|
||||
import type { CellValue } from "@/lib/dataGrid/cellValue";
|
||||
@@ -947,7 +947,7 @@ describe("useDataGridEditor saveChanges reload", () => {
|
||||
queryResult?: { columns: string[]; rows: CellValue[][] };
|
||||
currentPage?: Ref<number>;
|
||||
prepareFullReload?: () => void;
|
||||
customSaveHandler?: { save: ReturnType<typeof vi.fn> };
|
||||
customSaveHandler?: CustomSaveHandler;
|
||||
confirmSaveRequest?: (request: import("@/composables/useDataGridEditor").DataGridSaveConfirmationRequest) => Promise<boolean>;
|
||||
manualTransactionSessionId?: string;
|
||||
ensureManualTransactionSession?: () => Promise<string>;
|
||||
@@ -1394,6 +1394,60 @@ describe("useDataGridEditor saveChanges reload", () => {
|
||||
expect(emit).not.toHaveBeenCalledWith("reload", expect.anything());
|
||||
});
|
||||
|
||||
it("previews and confirms a guarded custom delete before execution, then reloads", async () => {
|
||||
const customSave = vi.fn().mockResolvedValue(undefined);
|
||||
const preview = vi.fn().mockResolvedValue(["DELETE FROM guarded WHERE exact_identity"]);
|
||||
const confirmSaveRequest = vi.fn().mockResolvedValue(true);
|
||||
const prepareFullReload = vi.fn();
|
||||
const { editor, emit } = createSaveTestEditor({
|
||||
customSaveHandler: { save: customSave, preview, confirmation: "influxdb-v1-delete", canDelete: true },
|
||||
confirmSaveRequest,
|
||||
prepareFullReload,
|
||||
databaseType: "influxdb",
|
||||
});
|
||||
editor.deletedRows.value.add(0);
|
||||
|
||||
await editor.saveChanges();
|
||||
|
||||
expect(preview).toHaveBeenCalledTimes(1);
|
||||
expect(confirmSaveRequest).toHaveBeenCalledWith({ updates: 0, inserts: 0, deletes: 1, targetLabel: undefined, statements: ["DELETE FROM guarded WHERE exact_identity"] });
|
||||
expect(customSave).toHaveBeenCalledTimes(1);
|
||||
expect(editor.deletedRows.value.size).toBe(0);
|
||||
expect(prepareFullReload).toHaveBeenCalledTimes(1);
|
||||
expect(emit).toHaveBeenCalledWith("reload", undefined, "", undefined, undefined, 100, 0);
|
||||
});
|
||||
|
||||
it("keeps a guarded delete staged when declined and refreshes after an execution failure", async () => {
|
||||
const preview = vi.fn().mockResolvedValue(["DELETE FROM guarded WHERE exact_identity"]);
|
||||
const declinedSave = vi.fn();
|
||||
const declined = createSaveTestEditor({
|
||||
customSaveHandler: { save: declinedSave, preview, confirmation: "influxdb-v1-delete", canDelete: true },
|
||||
confirmSaveRequest: vi.fn().mockResolvedValue(false),
|
||||
databaseType: "influxdb",
|
||||
});
|
||||
declined.editor.deletedRows.value.add(0);
|
||||
|
||||
await declined.editor.saveChanges();
|
||||
|
||||
expect(declinedSave).not.toHaveBeenCalled();
|
||||
expect(declined.editor.deletedRows.value).toEqual(new Set([0]));
|
||||
expect(declined.emit).not.toHaveBeenCalledWith("reload", expect.anything());
|
||||
|
||||
const failedSave = vi.fn().mockRejectedValue(new Error("server rejected delete"));
|
||||
const failed = createSaveTestEditor({
|
||||
customSaveHandler: { save: failedSave, preview, confirmation: "influxdb-v1-delete", reloadOnFailure: true, canDelete: true },
|
||||
confirmSaveRequest: vi.fn().mockResolvedValue(true),
|
||||
databaseType: "influxdb",
|
||||
});
|
||||
failed.editor.deletedRows.value.add(1);
|
||||
|
||||
await failed.editor.saveChanges();
|
||||
|
||||
expect(failed.editor.saveError.value).toContain("server rejected delete");
|
||||
expect(failed.editor.deletedRows.value).toEqual(new Set([1]));
|
||||
expect(failed.emit).toHaveBeenCalledWith("reload", undefined, "", undefined, undefined, 100, 0);
|
||||
});
|
||||
|
||||
// Engines that cannot roll a partial batch back (Salesforce: one REST call per
|
||||
// record) hand the operation list to the host for review before it is written.
|
||||
it("summarizes inserts, updates and deletes for the host save confirmation and writes once accepted", async () => {
|
||||
|
||||
@@ -80,18 +80,21 @@ export interface CustomSaveHandler {
|
||||
save: (changes: { dirtyRows: Map<number, Map<number, CellValue>>; newRows: CellValue[][]; newRowMeta: GridNewRowMeta[]; deletedRows: Set<number>; columns: string[]; rows: CellValue[][] }) => Promise<void>;
|
||||
applySavedChanges?: (changes: { dirtyRows: Map<number, Map<number, CellValue>>; columns: string[] }) => void;
|
||||
preview?: (changes: { dirtyRows: Map<number, Map<number, CellValue>>; newRows: CellValue[][]; newRowMeta: GridNewRowMeta[]; deletedRows: Set<number>; columns: string[]; rows: CellValue[][] }) => Promise<string[]>;
|
||||
/** Optional per-existing-row safety gate used before a deletion can be staged. */
|
||||
canDeleteRow?: (sourceIndex: number, row: readonly CellValue[]) => boolean;
|
||||
/** Selects an unconditional, statement-previewing confirmation before save. */
|
||||
confirmation?: "influxdb-v1-delete";
|
||||
/** Refresh after an execution error when a non-transactional batch may have partially succeeded. */
|
||||
reloadOnFailure?: boolean;
|
||||
canInsert?: boolean;
|
||||
canUpdate?: boolean;
|
||||
canDelete?: boolean;
|
||||
readonlyColumns?: string[];
|
||||
supportsInsert?: boolean;
|
||||
targetLabel?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Summary handed to `confirmSaveRequest` so an engine whose writes cannot be
|
||||
* rolled back (Salesforce REST: one record per request, no transactions) can
|
||||
* show the operator exactly what is about to happen before anything is sent.
|
||||
*/
|
||||
/** Summary handed to `confirmSaveRequest` for a final mutation review. */
|
||||
export interface DataGridSaveConfirmationRequest {
|
||||
/** Rows with edited cells (one write per row). */
|
||||
updates: number;
|
||||
@@ -99,7 +102,7 @@ export interface DataGridSaveConfirmationRequest {
|
||||
deletes: number;
|
||||
/** Save target name (table / sObject), when known. */
|
||||
targetLabel?: string;
|
||||
/** The statements about to be executed — for Salesforce these are `DBX SALESFORCE DML` pseudo-commands. */
|
||||
/** The SQL or pseudo-statements about to be executed. */
|
||||
statements: string[];
|
||||
}
|
||||
|
||||
@@ -135,11 +138,7 @@ export interface UseDataGridEditorOptions {
|
||||
rowStatusFilter: Ref<RowStatusFilter>;
|
||||
dataGridQuickEntryEnabled?: ComputedRef<boolean>;
|
||||
confirmDangerousRowDeletion?: ComputedRef<boolean>;
|
||||
/**
|
||||
* Optional operator review before a save is executed. Return `false` to cancel:
|
||||
* the pending edits stay in the grid and nothing is sent. Engines whose writes
|
||||
* cannot be rolled back (Salesforce REST) use this; auto-save never bypasses it.
|
||||
*/
|
||||
/** Optional operator review before a save is executed. Auto-save never bypasses it. */
|
||||
confirmSaveRequest?: ComputedRef<((request: DataGridSaveConfirmationRequest) => Promise<boolean>) | undefined>;
|
||||
/** `生成 SQL 时包含数据库名` — qualify saved tables with their database. */
|
||||
includeDatabaseNameInSaveSql?: ComputedRef<boolean>;
|
||||
@@ -1923,17 +1922,47 @@ export function useDataGridEditor(options: UseDataGridEditorOptions) {
|
||||
const shouldReloadAfterSqlSave = shouldReloadAfterSave || snapshot.dirtyRows.size > 0;
|
||||
|
||||
if (customHandler) {
|
||||
try {
|
||||
await customHandler.save({
|
||||
dirtyRows: snapshot.dirtyRows,
|
||||
newRows: snapshot.newRows,
|
||||
newRowMeta: snapshot.newRowMeta,
|
||||
deletedRows: snapshot.deletedRows,
|
||||
columns: result.value.columns,
|
||||
rows: result.value.rows,
|
||||
const customChanges = {
|
||||
dirtyRows: snapshot.dirtyRows,
|
||||
newRows: snapshot.newRows,
|
||||
newRowMeta: snapshot.newRowMeta,
|
||||
deletedRows: snapshot.deletedRows,
|
||||
columns: result.value.columns,
|
||||
rows: result.value.rows,
|
||||
};
|
||||
const confirmSaveRequest = options.confirmSaveRequest?.value;
|
||||
if (confirmSaveRequest) {
|
||||
if (saveOptions.autoSave) {
|
||||
await finishInterruptedSaveChanges(snapshot);
|
||||
return;
|
||||
}
|
||||
let statements: string[];
|
||||
try {
|
||||
if (!customHandler.preview) throw new Error("This data source cannot safely preview the pending changes.");
|
||||
statements = await customHandler.preview(customChanges);
|
||||
if (statements.length === 0) throw new Error("This data source did not produce a safe mutation preview.");
|
||||
} catch (e: any) {
|
||||
saveError.value = normalizeDataGridSaveError(databaseType.value, e);
|
||||
await finishInterruptedSaveChanges(snapshot);
|
||||
return;
|
||||
}
|
||||
const saveConfirmed = await confirmSaveRequest({
|
||||
updates: snapshot.dirtyRows.size,
|
||||
inserts: snapshot.newRows.length,
|
||||
deletes: snapshot.deletedRows.size,
|
||||
targetLabel: customHandler.targetLabel,
|
||||
statements,
|
||||
});
|
||||
if (!saveConfirmed) {
|
||||
await finishInterruptedSaveChanges(snapshot);
|
||||
return;
|
||||
}
|
||||
}
|
||||
try {
|
||||
await customHandler.save(customChanges);
|
||||
} catch (e: any) {
|
||||
saveError.value = normalizeDataGridSaveError(databaseType.value, e);
|
||||
if (customHandler.reloadOnFailure && shouldReloadAfterSave) reloadCurrentData();
|
||||
await finishInterruptedSaveChanges(snapshot);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
import { computed, onScopeDispose, ref, type ComputedRef, type Ref, type WritableComputedRef } from "vue";
|
||||
import type { DataGridSaveConfirmationRequest } from "@/composables/useDataGridEditor";
|
||||
|
||||
/** Parks a grid save until its confirmation dialog explicitly accepts or rejects it. */
|
||||
export function useDataGridSaveConfirmation(): {
|
||||
pending: Ref<DataGridSaveConfirmationRequest | null>;
|
||||
open: WritableComputedRef<boolean>;
|
||||
updates: ComputedRef<number>;
|
||||
inserts: ComputedRef<number>;
|
||||
deletes: ComputedRef<number>;
|
||||
total: ComputedRef<number>;
|
||||
targetLabel: ComputedRef<string | undefined>;
|
||||
statements: ComputedRef<string[]>;
|
||||
request: (request: DataGridSaveConfirmationRequest) => Promise<boolean>;
|
||||
confirm: () => void;
|
||||
cancel: () => void;
|
||||
} {
|
||||
const pending = ref<DataGridSaveConfirmationRequest | null>(null);
|
||||
let resolvePending: ((confirmed: boolean) => void) | undefined;
|
||||
|
||||
const open = computed({
|
||||
get: () => !!pending.value,
|
||||
set: (next: boolean) => {
|
||||
if (!next) settle(false);
|
||||
},
|
||||
});
|
||||
|
||||
const updates = computed(() => pending.value?.updates ?? 0);
|
||||
const inserts = computed(() => pending.value?.inserts ?? 0);
|
||||
const deletes = computed(() => pending.value?.deletes ?? 0);
|
||||
const total = computed(() => updates.value + inserts.value + deletes.value);
|
||||
const targetLabel = computed(() => pending.value?.targetLabel);
|
||||
const statements = computed(() => pending.value?.statements ?? []);
|
||||
|
||||
function settle(confirmed: boolean) {
|
||||
const resolve = resolvePending;
|
||||
resolvePending = undefined;
|
||||
pending.value = null;
|
||||
resolve?.(confirmed);
|
||||
}
|
||||
|
||||
function request(saveRequest: DataGridSaveConfirmationRequest): Promise<boolean> {
|
||||
return new Promise<boolean>((resolve) => {
|
||||
if (pending.value) {
|
||||
resolve(false);
|
||||
return;
|
||||
}
|
||||
pending.value = saveRequest;
|
||||
resolvePending = resolve;
|
||||
});
|
||||
}
|
||||
|
||||
function confirm() {
|
||||
settle(true);
|
||||
}
|
||||
|
||||
function cancel() {
|
||||
settle(false);
|
||||
}
|
||||
|
||||
onScopeDispose(() => settle(false));
|
||||
|
||||
return { pending, open, updates, inserts, deletes, total, targetLabel, statements, request, confirm, cancel };
|
||||
}
|
||||
@@ -1,76 +1,3 @@
|
||||
import { computed, onScopeDispose, ref, type ComputedRef, type Ref, type WritableComputedRef } from "vue";
|
||||
import type { DataGridSaveConfirmationRequest } from "@/composables/useDataGridEditor";
|
||||
|
||||
/**
|
||||
* Salesforce writes go out as one REST call per record and cannot be rolled back,
|
||||
* so every grid save is reviewed before it runs. This composable turns the
|
||||
* `confirmSaveRequest` hook from `useDataGridEditor` into a dialog decision:
|
||||
* `request()` parks the save on a promise the dialog settles.
|
||||
*
|
||||
* The pending request is transient — never persisted and never outliving the
|
||||
* component — so a dismissed grid denies the write instead of leaving it hanging.
|
||||
*/
|
||||
export function useSalesforceSaveConfirmation(): {
|
||||
pending: Ref<DataGridSaveConfirmationRequest | null>;
|
||||
open: WritableComputedRef<boolean>;
|
||||
updates: ComputedRef<number>;
|
||||
inserts: ComputedRef<number>;
|
||||
deletes: ComputedRef<number>;
|
||||
total: ComputedRef<number>;
|
||||
targetLabel: ComputedRef<string | undefined>;
|
||||
statements: ComputedRef<string[]>;
|
||||
request: (request: DataGridSaveConfirmationRequest) => Promise<boolean>;
|
||||
confirm: () => void;
|
||||
cancel: () => void;
|
||||
} {
|
||||
const pending = ref<DataGridSaveConfirmationRequest | null>(null);
|
||||
let resolvePending: ((confirmed: boolean) => void) | undefined;
|
||||
|
||||
const open = computed({
|
||||
get: () => !!pending.value,
|
||||
set: (next: boolean) => {
|
||||
if (!next) settle(false);
|
||||
},
|
||||
});
|
||||
|
||||
const updates = computed(() => pending.value?.updates ?? 0);
|
||||
const inserts = computed(() => pending.value?.inserts ?? 0);
|
||||
const deletes = computed(() => pending.value?.deletes ?? 0);
|
||||
const total = computed(() => updates.value + inserts.value + deletes.value);
|
||||
const targetLabel = computed(() => pending.value?.targetLabel);
|
||||
const statements = computed(() => pending.value?.statements ?? []);
|
||||
|
||||
function settle(confirmed: boolean) {
|
||||
const resolve = resolvePending;
|
||||
resolvePending = undefined;
|
||||
pending.value = null;
|
||||
resolve?.(confirmed);
|
||||
}
|
||||
|
||||
function request(saveRequest: DataGridSaveConfirmationRequest): Promise<boolean> {
|
||||
return new Promise<boolean>((resolve) => {
|
||||
if (pending.value) {
|
||||
// A grid only saves once at a time, but never strand a second caller:
|
||||
// deny it so that save unwinds instead of waiting on a dialog it cannot see.
|
||||
resolve(false);
|
||||
return;
|
||||
}
|
||||
pending.value = saveRequest;
|
||||
resolvePending = resolve;
|
||||
});
|
||||
}
|
||||
|
||||
function confirm() {
|
||||
settle(true);
|
||||
}
|
||||
|
||||
function cancel() {
|
||||
settle(false);
|
||||
}
|
||||
|
||||
// The dialog is the only way to settle the promise; if it goes away first (tab
|
||||
// closed mid-review) the parked save must be denied, not leaked.
|
||||
onScopeDispose(() => settle(false));
|
||||
|
||||
return { pending, open, updates, inserts, deletes, total, targetLabel, statements, request, confirm, cancel };
|
||||
}
|
||||
// Kept as a compatibility name for the focused composable tests and any callers
|
||||
// outside the desktop grid. The confirmation state itself is data-source agnostic.
|
||||
export { useDataGridSaveConfirmation as useSalesforceSaveConfirmation } from "@/composables/useDataGridSaveConfirmation";
|
||||
Reference in new issue
Block a user