-
Notifications
You must be signed in to change notification settings - Fork 0
Bson upgrade #4
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: master
Are you sure you want to change the base?
Bson upgrade #4
Conversation
WalkthroughThis update introduces a new Mongoose plugin module that adds pagination and search static methods to Mongoose schemas. The package dependencies are updated, including the addition of Mongoose. The main export file is modified to export the new plugin, and a comprehensive test suite is added to verify the plugin's functionality. Changes
Sequence Diagram(s)sequenceDiagram
participant App
participant MongooseModel
participant PluginStaticMethod
participant ExternalFindOrSearch
App->>MongooseModel: Call paginate/search static method
MongooseModel->>PluginStaticMethod: Invoke plugin method (paginate/search)
PluginStaticMethod->>ExternalFindOrSearch: Call external find/search function
ExternalFindOrSearch-->>PluginStaticMethod: Return results
PluginStaticMethod-->>MongooseModel: Return results
MongooseModel-->>App: Return paginated/search results
Poem
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (1)
✅ Files skipped from review due to trivial changes (1)
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
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.
Actionable comments posted: 3
🧹 Nitpick comments (4)
src/mongoose.plugin.js (1)
43-53
: Avoid silently overriding existing staticsBefore assigning
schema.statics[name]
/searchFnName
, check whether a static with that key already exists; otherwise consumers may unknowingly lose previous behaviour.if (schema.statics[name]) { throw new Error(`Static "${name}" already exists on schema`); } schema.statics[name] = findFn;src/index.js (1)
6-15
: Document the new public surface
mongoosePlugin
is now part of the public API but README / JSDoc were not updated. Exposing it without docs makes adoption harder.At minimum, add a usage snippet:
const { mongoosePlugin } = require('@tawk.to/mongo-cursor-pagination'); MySchema.plugin(mongoosePlugin);test/mongoosePlugin.js (2)
56-58
: Release DB connections to prevent Jest open-handle leaksThe test disconnects the in-memory server but keeps the Mongoose connection open, which can cause “Jest did not exit one second after the test run” warnings on some CI environments.
afterAll(async () => { + await mongoose.disconnect(); await mongod.stop(); });
51-54
:ensureIndexes
is deprecated in Mongoose 7Use
Model.createIndexes()
orModel.syncIndexes()
to avoid deprecation warnings and future breakage.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (1)
package-lock.json
is excluded by!**/package-lock.json
📒 Files selected for processing (4)
package.json
(3 hunks)src/index.js
(1 hunks)src/mongoose.plugin.js
(1 hunks)test/mongoosePlugin.js
(1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
src/mongoose.plugin.js (3)
src/index.js (3)
find
(3-3)require
(5-5)search
(4-4)src/find.js (2)
require
(5-9)_
(1-1)src/search.js (1)
_
(1-1)
🪛 Biome (1.9.4)
src/mongoose.plugin.js
[error] 43-43: Change to an optional chain.
Unsafe fix: Change to an optional chain.
(lint/complexity/useOptionalChain)
[error] 49-49: Change to an optional chain.
Unsafe fix: Change to an optional chain.
(lint/complexity/useOptionalChain)
🔇 Additional comments (1)
package.json (1)
38-42
: ```shell
#!/bin/bash
set -eecho "=== Checking [email protected] engines requirement ==="
if command -v npm >/dev/null 2>&1; then
npm view [email protected] engines --json
else
curl -s https://registry.npmjs.org/mongoose/7.0.5 | jq '.engines'
fiecho
echo "=== Checking [email protected] engines requirement ==="
if command -v npm >/dev/null 2>&1; then
npm view [email protected] engines --json
else
curl -s https://registry.npmjs.org/bson/6.7.0 | jq '.engines'
fi</details> </blockquote></details> </details> <!-- This is an auto-generated comment by CodeRabbit for review status -->
"bson": "^6.7.0", | ||
"mongoose": "^7.0.5", | ||
"object-path": "^0.11.5", |
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.
🛠️ Refactor suggestion
Consider pinning major versions or adding an integration test
bson
, mongodb
and the new mongoose
dependencies are all major-version jumps. If any public helpers still rely on an older driver API (e.g. ObjectId
helpers, cursor methods) they may break silently.
If full semantic-version tolerance is intended, add integration tests against the new driver; otherwise pin the exact major you have verified.
Also applies to: 59-59
🤖 Prompt for AI Agents
In package.json around lines 40 to 42 and line 59, the dependencies bson,
mongodb, and mongoose have been updated with major version changes which may
cause silent breaks if older driver APIs are still used. To fix this, either pin
these dependencies to the exact major versions you have tested to ensure
compatibility or add integration tests that verify all public helper functions
work correctly with the new driver versions to catch any breaking changes early.
module.exports = function (schema, options) { | ||
/** | ||
* paginate function | ||
* @param {Object} params required parameter | ||
*/ | ||
const findFn = function (params) { | ||
if (!this.collection) { | ||
throw new Error('collection property not found'); | ||
} | ||
|
||
params = _.extend({}, params); | ||
|
||
return find(this.collection, params); | ||
}; | ||
|
||
/** | ||
* search function | ||
* @param {String} searchString String to search on. Required parameter | ||
* @param {Object} params | ||
*/ | ||
const searchFn = function (searchString, params) { | ||
if (!this.collection) { | ||
throw new Error('collection property not found'); | ||
} | ||
|
||
params = _.extend({}, params); | ||
|
||
return search(this.collection, searchString, params); | ||
}; |
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.
🛠️ Refactor suggestion
Provide safer defaults & parameter validation
Small hardening tweaks:
- Default the
options
object to avoid repeated null-checks and reduce cognitive load. - Validate
searchString
to prevent accidental calls withundefined
, which can yield confusing results downstream. - Use object spread instead of
_.extend
to remove the underscore dependency inside hot paths.
-module.exports = function (schema, options) {
+module.exports = function (schema, {
+ name = 'paginate',
+ searchFnName = 'search'
+} = {}) {
/**
* paginate function
* @param {Object} params required parameter
*/
- const findFn = function (params) {
+ const findFn = function (params = {}) {
if (!this.collection) {
throw new Error('collection property not found');
}
- params = _.extend({}, params);
- return find(this.collection, params);
+ return find(this.collection, { ...params });
};
@@
- const searchFn = function (searchString, params) {
+ const searchFn = function (searchString, params = {}) {
if (!this.collection) {
throw new Error('collection property not found');
}
-
- params = _.extend({}, params);
-
- return search(this.collection, searchString, params);
+ if (typeof searchString !== 'string' || !searchString.length) {
+ throw new TypeError('searchString must be a non-empty string');
+ }
+ return search(this.collection, searchString, { ...params });
};
-
- if (options && options.name) {
- schema.statics[options.name] = findFn;
- } else {
- schema.statics.paginate = findFn;
- }
-
- if (options && options.searchFnName) {
- schema.statics[options.searchFnName] = searchFn;
- } else {
- schema.statics.search = searchFn;
- }
+ schema.statics[name] = findFn;
+ schema.statics[searchFnName] = searchFn;
};
This removes the optional-chaining lint warnings, tightens typing, and trims dependencies.
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
module.exports = function (schema, options) { | |
/** | |
* paginate function | |
* @param {Object} params required parameter | |
*/ | |
const findFn = function (params) { | |
if (!this.collection) { | |
throw new Error('collection property not found'); | |
} | |
params = _.extend({}, params); | |
return find(this.collection, params); | |
}; | |
/** | |
* search function | |
* @param {String} searchString String to search on. Required parameter | |
* @param {Object} params | |
*/ | |
const searchFn = function (searchString, params) { | |
if (!this.collection) { | |
throw new Error('collection property not found'); | |
} | |
params = _.extend({}, params); | |
return search(this.collection, searchString, params); | |
}; | |
module.exports = function (schema, { | |
name = 'paginate', | |
searchFnName = 'search' | |
} = {}) { | |
/** | |
* paginate function | |
* @param {Object} params required parameter | |
*/ | |
const findFn = function (params = {}) { | |
if (!this.collection) { | |
throw new Error('collection property not found'); | |
} | |
return find(this.collection, { ...params }); | |
}; | |
/** | |
* search function | |
* @param {String} searchString String to search on. Required parameter | |
* @param {Object} params | |
*/ | |
const searchFn = function (searchString, params = {}) { | |
if (!this.collection) { | |
throw new Error('collection property not found'); | |
} | |
if (typeof searchString !== 'string' || !searchString.length) { | |
throw new TypeError('searchString must be a non-empty string'); | |
} | |
return search(this.collection, searchString, { ...params }); | |
}; | |
schema.statics[name] = findFn; | |
schema.statics[searchFnName] = searchFn; | |
}; |
🤖 Prompt for AI Agents
In src/mongoose.plugin.js around lines 13 to 41, improve safety by defaulting
the options parameter to an empty object to avoid null checks, add validation to
ensure searchString is defined before proceeding in searchFn, and replace
_.extend calls with object spread syntax to eliminate the underscore dependency
and simplify the code.
it('returns data in the expected format', async () => { | ||
const data = await Post.paginate(); | ||
expect(hasOwnProperty.call(data, 'results')).toBe(true); | ||
expect(hasOwnProperty.call(data, 'previous')).toBe(true); | ||
expect(hasOwnProperty.call(data, 'hasPrevious')).toBe(true); | ||
expect(hasOwnProperty.call(data, 'next')).toBe(true); | ||
expect(hasOwnProperty.call(data, 'hasNext')).toBe(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.
Use a stable property-check helper
hasOwnProperty
is not guaranteed to be in scope inside CommonJS modules and will throw in strict mode. Guard with Object.prototype.hasOwnProperty.call
.
-expect(hasOwnProperty.call(data, 'results')).toBe(true);
+expect(Object.prototype.hasOwnProperty.call(data, 'results')).toBe(true);
Apply to the other assertions as well.
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
it('returns data in the expected format', async () => { | |
const data = await Post.paginate(); | |
expect(hasOwnProperty.call(data, 'results')).toBe(true); | |
expect(hasOwnProperty.call(data, 'previous')).toBe(true); | |
expect(hasOwnProperty.call(data, 'hasPrevious')).toBe(true); | |
expect(hasOwnProperty.call(data, 'next')).toBe(true); | |
expect(hasOwnProperty.call(data, 'hasNext')).toBe(true); | |
}); | |
it('returns data in the expected format', async () => { | |
const data = await Post.paginate(); | |
- expect(hasOwnProperty.call(data, 'results')).toBe(true); | |
+ expect(Object.prototype.hasOwnProperty.call(data, 'results')).toBe(true); | |
expect(hasOwnProperty.call(data, 'previous')).toBe(true); | |
expect(hasOwnProperty.call(data, 'hasPrevious')).toBe(true); | |
expect(hasOwnProperty.call(data, 'next')).toBe(true); | |
expect(hasOwnProperty.call(data, 'hasNext')).toBe(true); | |
}); |
🤖 Prompt for AI Agents
In test/mongoosePlugin.js around lines 70 to 77, the code uses
hasOwnProperty.call directly, which may not be stable or available in strict
mode. Replace all instances of hasOwnProperty.call with
Object.prototype.hasOwnProperty.call to ensure the property check is reliable
and does not throw errors. Update all assertions in this test block accordingly.
Changes Made
Potential Risks
Test Plan
Checklist
Summary by CodeRabbit
New Features
Dependency Updates
bson
andmongodb
dependencies to newer versions.mongoose
as a new dependency.Tests