Commit 4ce3cc5
committed
fix(api): make database seeding idempotent and preserve IDs
Corrects the `DatabaseSeedingService` to ensure the seeding process is
idempotent and correctly preserves the relationships between fixture
documents.
The previous implementation had two critical bugs:
1. It generated a new, random `ObjectId` on every run, which broke all
predefined relationships between entities.
2. It used an empty filter `{}` in its `replaceOne` operation, causing
it to overwrite existing documents incorrectly on subsequent runs.
This fix addresses these issues by:
- Using the predefined hex string ID from each fixture item to create a
deterministic `ObjectId`.
- Using `updateOne` with a specific `_id` in the filter, ensuring that
the correct document is targeted.
- Using the `$set` operator with `upsert: true` to safely insert or
update the document, making the entire process idempotent.1 parent 6e84bcf commit 4ce3cc5
1 file changed
+10
-5
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
80 | 80 | | |
81 | 81 | | |
82 | 82 | | |
83 | | - | |
84 | | - | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
85 | 87 | | |
86 | 88 | | |
87 | 89 | | |
88 | | - | |
89 | | - | |
90 | | - | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
91 | 96 | | |
92 | 97 | | |
93 | 98 | | |
| |||
0 commit comments