-
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
Open
Copilot
wants to merge
13
commits into
main
Choose a base branch
from
copilot/create-mongoose-package
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 11 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
5d81828
Initial plan
Copilot 8115044
Initial plan for mongoose package creation
Copilot 1d41511
Create mongoose package with middleware and utilities
Copilot 4c00ea6
Update database and types-database packages to use mongoose utilities
Copilot b3d1e32
Apply standard middleware to all database schemas
Copilot 0ecf498
Fix type compatibility issues and verify builds pass
Copilot 8a9c18e
Address PR feedback: use newSchema, singleton connection, Logger from…
Copilot 2c5bda0
Add socket timeout, heartbeat frequency, wait for connected event, re…
Copilot 658e94b
Convert middleware to Mongoose plugin pattern
Copilot 28bf71a
Enable timestamps option by default in newSchema and createModelFromZ…
Copilot ff3d3de
resolved merge
HughParry fd09e6f
fix: update mongoose package dependencies to match current versions
HughParry 9811d2f
Fix __v conflict in middleware and add changeset
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| # @prosopo/mongoose | ||
|
|
||
| ## 1.0.0 | ||
|
|
||
| ### Major Changes | ||
|
|
||
| - Initial release of the Mongoose utilities package | ||
| - Added `createMongooseConnection()` for creating MongoDB connections | ||
| - Added standard middleware for automatic timestamp management (createdAt, updatedAt) | ||
| - Added version increment middleware for all mutating operations | ||
| - Added `createSchemaWithMiddleware()` for creating schemas with middleware pre-applied | ||
| - Added `getOrCreateModel()` for safe model creation that handles multiple calls | ||
| - Added `createSchemaBuilder()` for fluent schema and model creation | ||
| - Added `createModelFromZodSchema()` for Zod-to-Mongoose schema mapping with validation | ||
| - Added comprehensive test coverage |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| # @prosopo/mongoose | ||
|
|
||
| Mongoose utilities and middleware for Prosopo packages. | ||
|
|
||
| This package provides: | ||
| - Mongoose connection management utilities | ||
| - Standard middleware plugin for timestamp management (createdAt, updatedAt) | ||
| - Version increment middleware for mutation operations | ||
| - Schema and model builders with automatic middleware application via plugin | ||
| - Zod-to-Mongoose schema mapping with validation | ||
| - Model caching to support multiple `.model()` calls | ||
|
|
||
| ## Features | ||
|
|
||
| ### Automatic Middleware Plugin | ||
| All schemas created with this package automatically include the standard middleware plugin: | ||
| - Version increment (`__v`) on all mutating operations | ||
| - `createdAt` timestamp (set once on creation, never overwritten) | ||
| - `updatedAt` timestamp (updated on every mutation) | ||
| - Validation enabled on all update operations | ||
|
|
||
| The middleware is applied as a Mongoose plugin, which is the recommended approach for extending schema functionality. | ||
|
|
||
| ### Zod Integration | ||
| Convert Zod schemas to Mongoose schemas with automatic validation in pre and post middleware. | ||
|
|
||
| ## Usage | ||
|
|
||
| ### Creating Schemas with newSchema() | ||
|
|
||
| ```typescript | ||
| import { newSchema } from '@prosopo/mongoose'; | ||
|
|
||
| // The standard middleware plugin is automatically applied | ||
| const UserSchema = newSchema({ | ||
| name: { type: String, required: true }, | ||
| email: { type: String, required: true } | ||
| }); | ||
| ``` | ||
|
|
||
| ### Using the Plugin Directly | ||
|
|
||
| You can also apply the standard middleware plugin to existing schemas: | ||
|
|
||
| ```typescript | ||
| import { Schema } from 'mongoose'; | ||
| import { standardMiddlewarePlugin } from '@prosopo/mongoose'; | ||
|
|
||
| const MySchema = new Schema({ | ||
| field: String | ||
| }); | ||
|
|
||
| // Apply the plugin | ||
| MySchema.plugin(standardMiddlewarePlugin); | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| { | ||
| "name": "@prosopo/mongoose", | ||
| "version": "1.0.0", | ||
| "author": "PROSOPO LIMITED <info@prosopo.io>", | ||
| "license": "Apache-2.0", | ||
| "private": false, | ||
| "engines": { | ||
| "node": "20", | ||
| "npm": "10.8.2" | ||
| }, | ||
| "scripts": { | ||
| "clean": "del-cli --verbose dist tsconfig.tsbuildinfo", | ||
| "build": "NODE_ENV=${NODE_ENV:-development}; vite build --config vite.esm.config.ts --mode $NODE_ENV", | ||
| "build:tsc": "tsc --build --verbose", | ||
| "build:cjs": "NODE_ENV=${NODE_ENV:-development}; vite build --config vite.cjs.config.ts --mode $NODE_ENV", | ||
| "typecheck": "tsc --project tsconfig.types.json", | ||
| "test": "NODE_ENV=${NODE_ENV:-test}; npx vitest run --config ./vite.test.config.ts" | ||
| }, | ||
| "main": "dist/index.js", | ||
| "type": "module", | ||
| "exports": { | ||
| ".": { | ||
| "types": "dist/index.d.ts", | ||
| "import": "./dist/index.js", | ||
| "require": "./dist/cjs/index.cjs" | ||
| } | ||
| }, | ||
| "types": "dist/index.d.ts", | ||
| "dependencies": { | ||
| "@prosopo/common": "3.1.21", | ||
| "mongodb": "6.15.0", | ||
| "mongoose": "8.13.0", | ||
| "zod": "3.23.8" | ||
| }, | ||
| "devDependencies": { | ||
| "@prosopo/config": "3.1.21", | ||
| "@types/node": "22.10.2", | ||
| "@vitest/coverage-v8": "3.0.9", | ||
| "concurrently": "9.0.1", | ||
| "del-cli": "6.0.0", | ||
| "mongodb-memory-server": "10.0.0", | ||
| "npm-run-all": "4.1.5", | ||
| "tslib": "2.7.0", | ||
| "tsx": "4.20.3", | ||
| "typescript": "5.6.2", | ||
| "vite": "6.3.5", | ||
| "vitest": "3.0.9" | ||
| }, | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "git+https://github.com/prosopo/captcha.git" | ||
| }, | ||
| "bugs": { | ||
| "url": "https://github.com/prosopo/captcha/issues" | ||
| }, | ||
| "homepage": "https://github.com/prosopo/captcha#readme", | ||
| "publishConfig": { | ||
| "registry": "https://registry.npmjs.org" | ||
| }, | ||
| "description": "Mongoose utilities and middleware for Prosopo packages", | ||
| "sideEffects": false | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.