-
Notifications
You must be signed in to change notification settings - Fork 8
Create @prosopo/mongoose package with standard middleware plugin and utilities #2183
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: goastler <[email protected]>
Co-authored-by: goastler <[email protected]>
Co-authored-by: goastler <[email protected]>
Co-authored-by: goastler <[email protected]>
Co-authored-by: goastler <[email protected]>
| "mongoose": "8.13.0", | ||
| "zod": "3.24.1" | ||
| }, | ||
| "devDependencies": { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot make sure dependency and devDependency versions are the same as other packages
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in commit. Changed zod from 3.24.1 to 3.23.8 and added @prosopo/common as a dependency to match other packages.
| } | ||
|
|
||
| return new Promise((resolve, reject) => { | ||
| const connection = mongoose.createConnection(connectionUrl, { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot connection should be a singleton
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in commit. Connection is now a singleton with caching by URL+dbname. Same connection parameters return the same instance. Cache is cleared when connection closes or errors.
packages/mongoose/src/connection.ts
Outdated
| * Optional logger object with debug and error methods | ||
| * Compatible with @prosopo/common Logger type | ||
| */ | ||
| logger?: { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot use the logger class from the "@prosopo/common" package
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in commit. Now using Logger type from @prosopo/common with getLogger fallback.
packages/mongoose/src/schema.ts
Outdated
| } | ||
|
|
||
| // Cache to store models by connection and model name | ||
| const modelCache = new WeakMap<Connection, Map<string, Model<unknown>>>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot no need for this, mongoose does this already. Mongoose has a flag you can set to allow models to be defined multiple times without throwing an error - use that instead
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in commit. Removed custom caching and now using mongoose's overwriteModels flag via connection.set('overwriteModels', true).
| } | ||
|
|
||
| // Default to Mixed for unknown types | ||
| return { type: Object, required: true }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot what about zod optional types?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in commit. Update validations now use zodSchema.partial() to properly handle optional fields during updates.
packages/mongoose/src/zodMapper.ts
Outdated
| next(); | ||
| } catch (error) { | ||
| if (error instanceof z.ZodError) { | ||
| next(new Error(`Update validation failed: ${error.message}`)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot json encode the error instead of using .message
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in commit. Using JSON.stringify(error.format()) to encode zod errors instead of .message.
| ...PoWCaptchaRecordSchema.obj, | ||
| }); | ||
| // Apply standard middleware | ||
| applyStandardMiddleware(StoredPoWCaptchaRecordSchema); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot rather than having to manually apply middleware using a function call like this, I want you to export a function called newSchema() from the "@prosopo/mongoose" package and ensure this already has the middleware set. Then there is no need to import from mongoose directly, the helper is all that is needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in commit. Exported newSchema() function from @prosopo/mongoose and updated all schemas in types-database to use it. No need to import from mongoose or manually apply middleware anymore.
|
@copilot set mongoose socketTimeoutMS to 30s |
|
@copilot set mongoose heartbeatFrequencyMS to 10s |
|
@copilot make sure the mongoose connection waits for "connected" event before resolving the promise |
|
@copilot allow multiple connection instances to exist, don't limit to singleton. Ensure only schemas are exported / imported, not models directly, as these are connection oriented, so be careful. |
|
@copilot add middleware that ensures validation is called on update(), updateMany(), findOneAndUpdate() |
… common, mongoose overwriteModels, JSON error encoding Co-authored-by: goastler <[email protected]>
|
Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details. Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
…move singleton, add validation middleware Co-authored-by: goastler <[email protected]>
Fixed. Removed singleton pattern and connection caching. Each call to createMongooseConnection() now creates a new connection instance. Schemas are exported/imported (not models) which are connection-oriented.
Fixed. Set socketTimeoutMS to 30000 (30 seconds) in mongoose connection options. |
|
@copilot make the middlewares a plugin for mongoose and apply the middleware via the plugin rather than wrapping a schema |
|
@copilot make sure all schemas that are given to the helper have their |
Co-authored-by: goastler <[email protected]>
Fixed. Converted middleware to a proper Mongoose plugin ( |
…odSchema Co-authored-by: goastler <[email protected]>
Fixed. Both |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR introduces a new @prosopo/mongoose package that centralizes mongoose utilities and implements standard middleware as a plugin to ensure consistent timestamp management, version control, and validation across all database operations. The package provides connection management, schema creation with automatic middleware application, and Zod integration for runtime validation.
Key changes:
- New
@prosopo/mongoosepackage with connection management, middleware plugin, schema builders, and Zod integration - All database implementations updated to use
getOrCreateModel()for safe model creation - All schemas migrated to use
newSchema()which automatically applies the standard middleware plugin
Reviewed Changes
Copilot reviewed 24 out of 25 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/mongoose/src/connection.ts | MongoDB connection utility with optimized timeout settings and Logger integration |
| packages/mongoose/src/middleware.ts | Standard middleware plugin for timestamps, version increment, and validation |
| packages/mongoose/src/schema.ts | Schema builder functions with automatic middleware application |
| packages/mongoose/src/zodMapper.ts | Zod-to-Mongoose schema conversion with validation middleware |
| packages/mongoose/src/tests/mongoose.test.ts | Comprehensive test suite for all mongoose utilities |
| packages/database/src/base/mongo.ts | Updated to use createMongooseConnection() |
| packages/database/src/databases/*.ts | Updated to use getOrCreateModel() for model creation |
| packages/types-database/src/types/*.ts | Added imports for newSchema() |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Updated @prosopo/common from 3.1.21 to 3.1.23 - Updated @prosopo/config from 3.1.21 to 3.1.23 - Resolved changesets validation errors
|
@copilot get the checks passing:
Context: |
Co-authored-by: HughParry <[email protected]>
... Fixed in 9811d2f:
The "__v would create a conflict" error was caused by the middleware trying to use both Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
Latest Changes
Fixed MongoDB __v Conflict Error
Fixed the issue causing "Updating the path '__v' would create a conflict at '__v'" error:
__vfrom$setoperations before adding$inc: { __v: 1 }$setand$incoperators__vfrom update operations and uses$incto increment itAdded Changeset
Created changeset for the new mongoose package and updated packages:
Fixed TypeScript Errors
as anytype assertion in zodMapper for update method hooks to satisfy TypeScriptOriginal prompt
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.