LibreChat/api/db
Adi Singhal 6466483ae3
🐛 fix: Resolve MeiliSearch Startup Sync Failure from Model Loading Order (#12397)
* fix: resolve MeiliSearch startup sync failure from model loading order

`indexSync.js` captures `mongoose.models.Message` and
`mongoose.models.Conversation` at module load time (lines 9-10).
Since PR #11830 moved model registration into `createModels()`,
these references are always `undefined` because `require('./indexSync')`
ran before `createModels(mongoose)` in `api/db/index.js`.

Move the `require('./indexSync')` below `createModels(mongoose)` so
Mongoose models are registered before `indexSync.js` captures them.

* fix: defer model lookups in indexSync, add load-order regression test

- Move mongoose.models.Message and mongoose.models.Conversation lookups
  from module-level into performSync() and the indexSync() catch block,
  eliminating the fragile load-order dependency that caused the original
  regression
- Add guard assertion that throws a clear error if models are missing
- Add comment in api/db/index.js documenting the createModels ordering
  constraint
- Add api/db/index.spec.js regression test to prevent future re-introduction
  of the load-order bug

* fix: strengthen regression test to verify call order, add guard in setTimeout path

- Rewrite index.spec.js to track call sequence via callOrder array,
  ensuring createModels executes before indexSync module loads
  (verified: test fails if order is reversed)
- Add missing model guard assertion in the setTimeout fallback path
  in indexSync.js for consistency with performSync

---------

Co-authored-by: Danny Avila <danny@librechat.ai>
2026-03-25 14:02:44 -04:00
..
connect.js 🪂 fix: Handle MongoDB Connection Errors to Prevent Process Crashes (#11809) 2026-02-16 16:23:59 -05:00
index.js 🐛 fix: Resolve MeiliSearch Startup Sync Failure from Model Loading Order (#12397) 2026-03-25 14:02:44 -04:00
index.spec.js 🐛 fix: Resolve MeiliSearch Startup Sync Failure from Model Loading Order (#12397) 2026-03-25 14:02:44 -04:00
indexSync.js 🐛 fix: Resolve MeiliSearch Startup Sync Failure from Model Loading Order (#12397) 2026-03-25 14:02:44 -04:00
indexSync.spec.js 🫙 fix: Force MeiliSearch Full Sync on Empty Index State (#12202) 2026-03-12 20:43:23 -04:00
models.js 🏗️ refactor: Extract DB layers to data-schemas for shared use (#7650) 2025-05-30 22:18:13 -04:00
utils.js 🔎 fix: Include Legacy Documents With Undefined _meiliIndex in Search Sync (#11745) 2026-02-12 18:05:53 -05:00
utils.spec.js 🔎 fix: Include Legacy Documents With Undefined _meiliIndex in Search Sync (#11745) 2026-02-12 18:05:53 -05:00