Skip to content

Commit

Permalink
Modify mongoose call lean().exec() throw is not a function
Browse files Browse the repository at this point in the history
  • Loading branch information
kingchang authored Nov 18, 2024
1 parent fa7fd65 commit 65ee51c
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion src/plugins/MongoosePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,46 @@ class MongoosePlugin implements SwPlugin {
(span as any).mongooseInCall = false;

if (!hasCB) {
if (ret && typeof ret.then === 'function') {
if (ret && typeof ret === 'object' && ret.constructor.name === 'Query') {
console.log("2");

Check failure on line 132 in src/plugins/MongoosePlugin.ts

View workflow job for this annotation

GitHub Actions / Build (14)

Replace `"2"` with `'2'`

Check failure on line 132 in src/plugins/MongoosePlugin.ts

View workflow job for this annotation

GitHub Actions / Lint

Replace `"2"` with `'2'`

Check failure on line 132 in src/plugins/MongoosePlugin.ts

View workflow job for this annotation

GitHub Actions / Build (18)

Replace `"2"` with `'2'`

Check failure on line 132 in src/plugins/MongoosePlugin.ts

View workflow job for this annotation

GitHub Actions / Build (16)

Replace `"2"` with `'2'`
// Mongoose Query object
const originalThen = ret.then;
const originalExec = ret.exec;
const originalLean = ret.lean;

// Preserve the query chain methods
ret.then = function (...args) {
return SwPlugin_1.wrapPromise(span, originalThen.apply(this, args));

Check failure on line 140 in src/plugins/MongoosePlugin.ts

View workflow job for this annotation

GitHub Actions / Build (14)

Replace `················` with `··············`

Check failure on line 140 in src/plugins/MongoosePlugin.ts

View workflow job for this annotation

GitHub Actions / Build (14)

'SwPlugin_1' is not defined

Check failure on line 140 in src/plugins/MongoosePlugin.ts

View workflow job for this annotation

GitHub Actions / Lint

Replace `················` with `··············`

Check failure on line 140 in src/plugins/MongoosePlugin.ts

View workflow job for this annotation

GitHub Actions / Lint

'SwPlugin_1' is not defined

Check failure on line 140 in src/plugins/MongoosePlugin.ts

View workflow job for this annotation

GitHub Actions / Build (18)

Replace `················` with `··············`

Check failure on line 140 in src/plugins/MongoosePlugin.ts

View workflow job for this annotation

GitHub Actions / Build (18)

'SwPlugin_1' is not defined

Check failure on line 140 in src/plugins/MongoosePlugin.ts

View workflow job for this annotation

GitHub Actions / Build (16)

Replace `················` with `··············`

Check failure on line 140 in src/plugins/MongoosePlugin.ts

View workflow job for this annotation

GitHub Actions / Build (16)

'SwPlugin_1' is not defined
};

ret.exec = function (...args) {
return SwPlugin_1.wrapPromise(span, originalExec.apply(this, args));

Check failure on line 144 in src/plugins/MongoosePlugin.ts

View workflow job for this annotation

GitHub Actions / Build (14)

Delete `··`

Check failure on line 144 in src/plugins/MongoosePlugin.ts

View workflow job for this annotation

GitHub Actions / Build (14)

'SwPlugin_1' is not defined

Check failure on line 144 in src/plugins/MongoosePlugin.ts

View workflow job for this annotation

GitHub Actions / Lint

Delete `··`

Check failure on line 144 in src/plugins/MongoosePlugin.ts

View workflow job for this annotation

GitHub Actions / Lint

'SwPlugin_1' is not defined

Check failure on line 144 in src/plugins/MongoosePlugin.ts

View workflow job for this annotation

GitHub Actions / Build (18)

Delete `··`

Check failure on line 144 in src/plugins/MongoosePlugin.ts

View workflow job for this annotation

GitHub Actions / Build (18)

'SwPlugin_1' is not defined

Check failure on line 144 in src/plugins/MongoosePlugin.ts

View workflow job for this annotation

GitHub Actions / Build (16)

Delete `··`

Check failure on line 144 in src/plugins/MongoosePlugin.ts

View workflow job for this annotation

GitHub Actions / Build (16)

'SwPlugin_1' is not defined
};

ret.lean = function (...args) {
const leanQuery = originalLean.apply(this, args);

Check failure on line 148 in src/plugins/MongoosePlugin.ts

View workflow job for this annotation

GitHub Actions / Build (14)

Replace `················` with `··············`

Check failure on line 148 in src/plugins/MongoosePlugin.ts

View workflow job for this annotation

GitHub Actions / Lint

Replace `················` with `··············`

Check failure on line 148 in src/plugins/MongoosePlugin.ts

View workflow job for this annotation

GitHub Actions / Build (18)

Replace `················` with `··············`

Check failure on line 148 in src/plugins/MongoosePlugin.ts

View workflow job for this annotation

GitHub Actions / Build (16)

Replace `················` with `··············`
// Preserve other chain methods on lean result

Check failure on line 149 in src/plugins/MongoosePlugin.ts

View workflow job for this annotation

GitHub Actions / Build (14)

Delete `··`

Check failure on line 149 in src/plugins/MongoosePlugin.ts

View workflow job for this annotation

GitHub Actions / Lint

Delete `··`

Check failure on line 149 in src/plugins/MongoosePlugin.ts

View workflow job for this annotation

GitHub Actions / Build (18)

Delete `··`

Check failure on line 149 in src/plugins/MongoosePlugin.ts

View workflow job for this annotation

GitHub Actions / Build (16)

Delete `··`
leanQuery.then = ret.then;

Check failure on line 150 in src/plugins/MongoosePlugin.ts

View workflow job for this annotation

GitHub Actions / Build (14)

Replace `················` with `··············`

Check failure on line 150 in src/plugins/MongoosePlugin.ts

View workflow job for this annotation

GitHub Actions / Lint

Replace `················` with `··············`

Check failure on line 150 in src/plugins/MongoosePlugin.ts

View workflow job for this annotation

GitHub Actions / Build (18)

Replace `················` with `··············`

Check failure on line 150 in src/plugins/MongoosePlugin.ts

View workflow job for this annotation

GitHub Actions / Build (16)

Replace `················` with `··············`
leanQuery.exec = ret.exec;

Check failure on line 151 in src/plugins/MongoosePlugin.ts

View workflow job for this annotation

GitHub Actions / Build (14)

Delete `··`

Check failure on line 151 in src/plugins/MongoosePlugin.ts

View workflow job for this annotation

GitHub Actions / Lint

Delete `··`

Check failure on line 151 in src/plugins/MongoosePlugin.ts

View workflow job for this annotation

GitHub Actions / Build (18)

Delete `··`

Check failure on line 151 in src/plugins/MongoosePlugin.ts

View workflow job for this annotation

GitHub Actions / Build (16)

Delete `··`
return leanQuery;

Check failure on line 152 in src/plugins/MongoosePlugin.ts

View workflow job for this annotation

GitHub Actions / Build (14)

Replace `················` with `··············`

Check failure on line 152 in src/plugins/MongoosePlugin.ts

View workflow job for this annotation

GitHub Actions / Lint

Replace `················` with `··············`

Check failure on line 152 in src/plugins/MongoosePlugin.ts

View workflow job for this annotation

GitHub Actions / Build (18)

Replace `················` with `··············`

Check failure on line 152 in src/plugins/MongoosePlugin.ts

View workflow job for this annotation

GitHub Actions / Build (16)

Replace `················` with `··············`
};

// Wrap other common query methods that might be chained
const chainMethods = ['select', 'sort', 'skip', 'limit', 'populate'];
chainMethods.forEach(method => {
if (ret[method]) {
const original = ret[method];
ret[method] = function (...args) {
const result = original.apply(this, args);
result.then = ret.then;
result.exec = ret.exec;
result.lean = ret.lean;
return result;
};
}
});
return ret;
} else if (ret && typeof ret.then === 'function') {
// generic Promise check
ret = wrapPromise(span, ret);
} else {
Expand Down

0 comments on commit 65ee51c

Please sign in to comment.