Skip to content

Refactor getModels to centralize model definitions and avoid duplicate registrations#486

Merged
AZ0228 merged 1 commit intomainfrom
codex/locate-backend-standard-practice-violations
Apr 10, 2026
Merged

Refactor getModels to centralize model definitions and avoid duplicate registrations#486
AZ0228 merged 1 commit intomainfrom
codex/locate-backend-standard-practice-violations

Conversation

@AZ0228
Copy link
Copy Markdown
Collaborator

@AZ0228 AZ0228 commented Apr 10, 2026

Motivation

  • Prevent repeated model re-registration on the same DB connection and centralize model metadata to reduce duplication and errors.
  • Improve performance and robustness by lazily creating models and validating that req.db is present.

Description

  • Introduced a frozen MODEL_DEFINITIONS map that contains modelName, schema, and collection for each model instead of recreating the full models object per call.
  • Added registeredConnections as a WeakSet to track DB connections that have had their models registered and an ensureAllModelsRegistered helper to register models once per connection.
  • Added getOrCreateModel to return an existing model if present or create it with req.db.model(...) and updated getModels to validate req.db and return only requested models using the centralized definitions.
  • Preserved public API by exporting getModels and kept original collection names and schema usages.

Testing

  • No automated tests were executed as part of this change.

Codex Task

@AZ0228 AZ0228 marked this pull request as ready for review April 10, 2026 18:36
@AZ0228 AZ0228 removed the codex label Apr 10, 2026
@AZ0228 AZ0228 merged commit 3a2d37d into main Apr 10, 2026
3 checks passed
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