Skip to content

Optimize JSON patch implementation with native SQLite function#42

Closed
zknpr wants to merge 1 commit intomainfrom
json-patch-optimization-12088301536056168967
Closed

Optimize JSON patch implementation with native SQLite function#42
zknpr wants to merge 1 commit intomainfrom
json-patch-optimization-12088301536056168967

Conversation

@zknpr
Copy link
Copy Markdown
Owner

@zknpr zknpr commented Feb 7, 2026

This PR optimizes the JSON patch implementation in WasmDatabaseEngine by leveraging the native json_patch function provided by SQLite (if available). This significantly improves performance by avoiding the read-modify-write cycle in JavaScript. It also maintains a fallback to the existing JavaScript implementation for environments without json_patch support. The changes cover both single cell updates (updateCell) and batch updates (updateCellBatch), and include robust handling for object inputs.


PR created automatically by Jules for task 12088301536056168967 started by @zknpr

- Added `checkJsonPatchSupport` to `WasmDatabaseEngine` to detect native `json_patch` availability.
- Optimized `updateCell` to use `json_patch` SQL function when available.
- Optimized `updateCellBatch` to use `json_patch` SQL function for batch updates when available.
- Ensured JS fallback is used when native function is unavailable.
- Handled object inputs for patches by stringifying them before binding.
- Added comprehensive unit tests in `tests/unit/json_patch_optimization.test.ts`.

Co-authored-by: zknpr <96851588+zknpr@users.noreply.github.com>
@google-labs-jules
Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@vercel
Copy link
Copy Markdown
Contributor

vercel Bot commented Feb 7, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
sq-lite-explorer Ready Ready Preview, Comment Feb 7, 2026 1:40pm

@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello @zknpr, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a significant performance enhancement to the WasmDatabaseEngine by integrating SQLite's native json_patch function for JSON data manipulation. This change streamlines the process of updating JSON fields within the database, reducing overhead and improving efficiency, while also ensuring compatibility with environments that may not support the native function through a well-defined fallback.

Highlights

  • Performance Optimization: The JSON patch implementation in WasmDatabaseEngine now leverages the native json_patch function in SQLite, significantly improving performance by avoiding the JavaScript read-modify-write cycle.
  • Fallback Mechanism: A robust fallback to the existing JavaScript implementation is maintained for environments where the native json_patch function is not available.
  • Broad Application: The optimization applies to both single cell updates (updateCell) and batch updates (updateCellBatch), ensuring consistent performance gains across different update scenarios.
  • Input Handling: The updated implementation includes robust handling for various object inputs, ensuring compatibility and correctness.
  • New Test Coverage: New unit tests have been added to verify the correct behavior and application of the json_patch optimization for both single and batch cell updates, covering string and object patch inputs.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • package-lock.json
    • Updated package version from 1.2.5 to 1.2.7.
  • src/core/sqlite-db.ts
    • Added a private member _hasJsonPatch and a method checkJsonPatchSupport() to detect the availability of SQLite's native json_patch function.
    • Modified updateCell method to conditionally use the native json_patch SQL function for JSON updates if supported, otherwise falling back to the existing JavaScript-based logic.
    • Refactored updateCellBatch method to also conditionally utilize the native json_patch function for batch JSON updates, improving efficiency by preparing the appropriate SQL statement once.
    • Ensured proper handling of both stringified JSON patches and direct object patches for both updateCell and updateCellBatch.
  • tests/unit/json_patch_optimization.test.ts
    • Added a new test file to verify the json_patch optimization.
    • Included tests for updateCell to confirm native json_patch usage with both string and object patch inputs.
    • Included tests for updateCellBatch to confirm native json_patch usage with both string and object patch inputs for batch operations.
Activity
  • The pull request was automatically created by Jules for task 12088301536056168967, initiated by @zknpr.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a significant performance optimization by leveraging SQLite's native json_patch function where available, with a sensible fallback to the existing JavaScript implementation. The changes are well-structured and cover both single and batch cell updates. The addition of unit tests is also a great step towards ensuring the new logic is correct. I've identified a couple of areas where type safety could be improved and the new tests could be made more robust. Overall, this is a solid improvement.

Comment thread src/core/sqlite-db.ts
}
if (this.checkJsonPatchSupport()) {
// Use native json_patch
const patchStr = typeof patch === 'string' ? patch : JSON.stringify(patch);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The patch parameter is typed as string | undefined in the method signature, but this line handles it as if it could also be an object. This type discrepancy is also reflected in the new regression test, which uses as any to pass an object.

This can lead to type safety issues. To make the code more robust and self-documenting, the method signature for updateCell and the DatabaseOperations interface should be updated to patch?: string | object. Since the interface is in a file not modified in this PR, I'd recommend at least adding a code comment here to explain the lenient type handling.

Comment thread src/core/sqlite-db.ts
const stmt = this.instance.prepare(sql);
try {
for (const update of columnUpdates) {
const patchStr = typeof update.value === 'string' ? update.value : JSON.stringify(update.value);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Similar to updateCell, there's a type discrepancy here. This line handles update.value as if it could be an object for json_patch operations, but its type is CellValue from the CellUpdate interface, which doesn't include objects. The new regression test for batch updates also uses as any to pass an object.

This can compromise type safety. The CellUpdate interface in types.ts should ideally be updated to allow value to be an object when operation is json_patch. Since types.ts is not in this PR, please consider adding a comment to clarify this behavior or addressing the type definition in a follow-up PR.

Comment on lines +32 to +35
const originalPrepare = engine.instance.prepare.bind(engine.instance);
engine.instance.prepare = (sql: string, params: any[]) => {
return originalPrepare(sql, params);
};
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This spy on engine.instance.prepare (and its restoration on line 51) is not used in this test, as updateCell calls executeQuery, not prepare. The spy implementation is also a no-op that doesn't collect any data. I suggest removing these lines to improve test clarity.

Comment on lines +106 to +120
it('should use json_patch when updating batch cells with object value (regression test)', async () => {
// Reset data
await engine.executeQuery("UPDATE data SET content = ? WHERE id = 2", [JSON.stringify({ a: 10, b: 20 })]);

const updates = [
{ rowId: 2, column: 'content', value: { b: 3000, c: 4000 } as any, operation: 'json_patch' }
];

await engine.updateCellBatch('data', updates);

// Verify result
const result = await engine.executeQuery("SELECT content FROM data WHERE id = 2");
const content = JSON.parse(result[0].rows[0][0]);
assert.deepStrictEqual(content, { a: 10, b: 3000, c: 4000 });
});
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This regression test correctly verifies that the update is applied successfully when an object is passed as a patch value. However, it doesn't check if the optimized json_patch native SQL function was actually used.

To make this test more robust, I suggest adding a spy on engine.instance.prepare to assert that the generated SQL contains json_patch, similar to the should use json_patch when updating batch cells test.

    it('should use json_patch when updating batch cells with object value (regression test)', async () => {
        // Reset data
        await engine.executeQuery("UPDATE data SET content = ? WHERE id = 2", [JSON.stringify({ a: 10, b: 20 })]);

        const originalPrepare = engine.instance.prepare.bind(engine.instance);
        const preparedStatements: string[] = [];
        engine.instance.prepare = (sql: string, params: any[]) => {
            preparedStatements.push(sql);
            return originalPrepare(sql, params);
        };

        const updates = [
            { rowId: 2, column: 'content', value: { b: 3000, c: 4000 } as any, operation: 'json_patch' }
        ];

        await engine.updateCellBatch('data', updates);

        // Verify result
        const result = await engine.executeQuery("SELECT content FROM data WHERE id = 2");
        const content = JSON.parse(result[0].rows[0][0]);
        assert.deepStrictEqual(content, { a: 10, b: 3000, c: 4000 });

        // Check if json_patch was used
        const usesJsonPatch = preparedStatements.some(q => q.toLowerCase().includes('json_patch'));
        assert.ok(usesJsonPatch, 'Should use json_patch SQL function for batch update with object value');

        engine.instance.prepare = originalPrepare;
    });

@zknpr
Copy link
Copy Markdown
Owner Author

zknpr commented Feb 7, 2026

Merged as part of v1.3.0 release in PR #65

@zknpr zknpr closed this Feb 7, 2026
@google-labs-jules
Copy link
Copy Markdown
Contributor

Merged as part of v1.3.0 release in PR #65

Understood. Acknowledging that this is merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant