Skip to main content

Local DB Commits

Source: lib/local-db-commits.ts (248 lines, 10.7KB) The local-db-commits module is the “Vault Guard” — a specialized repository that handles all task-level CRUD operations against the local SQLite database using atomic transactions and the Unified Identity strategy.

Unified Identity Strategy

To prevent “Split-Brain” duplication, the module uses the official Convex _id as the local primary key. The remoteId parameter in every function IS the Convex _id, and it is used as both the id and convex_id columns:

Core Operations

insertTaskToLocalDb()

Creates a new task with all projected instances in a single atomic transaction:
  1. INSERT OR REPLACE into local_tasks with all 17 columns
  2. Call syncInstancesToLocalDb() to batch-insert all backend-projected instances
  3. Both operations wrapped in db.withTransactionAsync()

updateTaskInLocalDb()

Updates task master rules and overwrites the future instance trajectory:
  1. UPDATE local_tasks SET with all mutable columns
  2. DELETE FROM task_instances WHERE task_id = ? AND is_manual_edit = 0 (protects manual edits)
  3. Call syncInstancesToLocalDb() to repopulate with newly projected instances

updateStrictModeInLocalDb()

Activates or extends Strict Mode:
  1. UPDATE local_tasks SET strict_until and strict_duration_days
  2. DELETE non-manual instances
  3. Re-sync instances (which now inherit the strict_until timestamp)

updateInstanceInLocalDb()

Updates a single instance with dynamic field selection:

nukeLocalDb()

Nuclear option: DELETE FROM both tables in a transaction.

High-Performance Batched Instance Insertion

The syncInstancesToLocalDb() function uses prepared statements for batched insertion:
Prepared statements avoid re-parsing the SQL for each instance, providing significant performance gains when projecting 50+ future instances.