Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 3 additions & 11 deletions src/core/engine/wasm/WasmDatabaseEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import type {
import { escapeIdentifier, validateSqlType, validateRowId, validateRowIds } from '../../sql-utils';
import { crypto } from '../../../platform/cryptoShim';
import { buildSelectQuery, buildCountQuery } from '../../query-builder';
import { applyMergePatch, computeJsonPatchUndo } from '../../json-utils';
import { applyMergePatch, computeJsonPatchUndo, parseJsonValueForPatching } from '../../json-utils';
import { getNodeFs } from '../../platform/fs';

// ============================================================================
Expand Down Expand Up @@ -589,12 +589,7 @@ export class WasmDatabaseEngine implements DatabaseOperations {
const currentResult = await this.executeQuery(`SELECT ${escapedCol} FROM ${escapedTbl} WHERE rowid = ?`, [rowIdNum]);
let currentValue = currentResult[0]?.rows[0]?.[0];

let currentObj = {};
if (typeof currentValue === 'string') {
try { currentObj = JSON.parse(currentValue); } catch (e) { console.warn('Failed to parse current JSON value for patching (updateCell)', e); }
} else if (typeof currentValue === 'object' && currentValue !== null && !(currentValue instanceof Uint8Array)) {
currentObj = currentValue;
}
const currentObj = parseJsonValueForPatching(currentValue, 'updateCell');

const patchObj = typeof patch === 'string' ? JSON.parse(patch) : patch;
const newValueObj = applyMergePatch(currentObj, patchObj);
Expand Down Expand Up @@ -904,10 +899,7 @@ export class WasmDatabaseEngine implements DatabaseOperations {
selectStmt.reset();
}

let currentObj = {};
if (typeof currentValue === 'string') {
try { currentObj = JSON.parse(currentValue); } catch (e) { console.warn('Failed to parse current JSON value for patching (updateCells)', e); }
}
const currentObj = parseJsonValueForPatching(currentValue, 'updateCells');

let patchObj;
if (typeof update.value === 'string') {
Expand Down
13 changes: 13 additions & 0 deletions src/core/json-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,19 @@ export function applyMergePatch(target: unknown, patch: unknown, depth = 0): unk
return targetObj;
}

export function parseJsonValueForPatching(val: unknown, context: string): Record<string, unknown> {
if (typeof val === 'string') {
try {
return JSON.parse(val);
} catch (e) {
console.warn(`Failed to parse current JSON value for patching (${context})`, e);
}
} else if (typeof val === 'object' && val !== null && !(val instanceof Uint8Array)) {
return val as Record<string, unknown>;
}
return {};
}

export type JsonUndoPlan =
| { kind: 'restore'; value: string }
| { kind: 'replace' };
Expand Down
Loading