Data Tasks API Reference
dataTasks uses one DataTaskJob configuration for release-scoped index checks, filtered data synchronization, local field edits, reviewed preview, affected-scope backup, and restore.
Start with Production Data Migration to choose the right tool. See Production Rollout for the complete release sequence.
Entry Point and Four Methods
Use the named ESM export:
Read the same export from the CommonJS package object:
dataTasks is not an instance method. There is no task runner or second step-based configuration.
Complete Configuration
SDK jobs can use two connected monSQLize instances; the caller owns their lifecycle. CLI jobs must use two MonSQLizeOptions values. dataTasks closes connections that it creates.
Close both SDK instances in finally before the process exits. See examples/docs/data-tasks.ts for the complete runnable lifecycle.
Job Parameters
Every collection needs non-empty indexes, data, or both.
Collection and Index Parameters
Preview calls listIndexes() first for every target collection and classifies each declaration as existing, missing, or conflict. Apply creates only missing indexes. It never drops, renames, or rebuilds a conflicting index.
identity.mode: 'fields' requires an exact non-partial unique index in the target or in the same collection's declarations. Proving a missing unique index requires a final-target-image scan. The task blocks when that scan exceeds 10,000 documents or an index shape cannot be proved exactly.
Data Parameters
Data task source reads use a bounded internal stream and do not inherit the ordinary findLimit default. Planning reads at most maxDocuments + 1 source documents so concurrent growth fails closed instead of silently truncating or loading an unbounded result.
Field edits run in this order: filter -> projection -> rename -> set -> unset -> diff. They are deliberately limited to deterministic edits of a few release fields:
- Paths cannot touch
_id, identity fields, or overlap one another. __proto__,prototype, andconstructorpath segments are rejected.- Rename blocks when the destination already contains a different value.
- Dotted paths block when an intermediate value is not an object.
setaccepts BSON-serializable literals, not functions, random values, or update expressions.
Identity and _id
Business-key matching:
Updates preserve the target _id; inserts let the target MongoDB generate _id. Use this when environments assign IDs independently.
Source-ID matching:
Inserts preserve source _id, and existing documents update only when _id matches. conflictBy detects the same business key under another _id and blocks preview.
Verification, Backup, and Lock
Index, identity, count, manifest, and actual-write checks are always full. verify.mode only changes user-selected field content verification.
Preview and Apply
Preview performs no writes. It returns index states, source/insert/update/unchanged/conflict, change samples, backup document count, and estimated bytes. Approval binds the normalized job, patched source image, target preimages, target indexes, and expiry.
Apply runs in this order:
- Acquire the optional target lease.
- Rebuild the full plan and reject approval on any hash drift.
- Write and read back the affected-scope backup.
- Read indexes again and create only reviewed missing indexes.
- Record each batch as pending before ordered data writes.
- Use target-preimage CAS filters to avoid overwriting concurrent changes.
- Read back the exact expected after-image and verify fields, identity, indexes, and manifest.
- Record final status and release the lease.
Indexes and cross-collection writes are not one MongoDB transaction. A mid-run error returns failed or partial, preserves the manifest, and never restores automatically.
Restore
Restore preview verifies that the target still matches the applied after-image and resolves pending operations when a process stopped between a database write and manifest checkpoint. Ambiguous or later target changes block with RESTORE_DRIFT.
Restore creates a safety backup before writing. Document operations use exact current-image filters, and index recovery only touches indexes actually created by the task whose definitions remain unchanged. The safety backup can itself be previewed and restored to undo a restore.
CLI
The task file must directly export the DataTaskJob:
Invalid configuration, stale approval, lock conflict, partial/failed status, checksum errors, and restore drift exit with code 1.
Errors and Boundaries
DataTaskJobError exposes code, phase, and optional collection. Stable codes are INVALID_JOB, IDENTITY_CONFLICT, INDEX_CONFLICT, APPROVAL_STALE, BACKUP_FAILED, LOCK_NOT_ACQUIRED, LOCK_LOST, APPLY_PARTIAL, RESTORE_DRIFT, and RESTORE_FAILED.
- A backup is an affected-scope rollback package, not a full backup or point-in-time restore.
- dataTasks does not synchronize Model schema, copy every source index, maintain migration directories, or run ongoing CDC.
- Use MongoDB native or managed backup tools for full moves and disaster recovery.
- Use Change Stream sync for ongoing changes after the initial backfill.