Skip to content

Commit

Permalink
fix: resolved mongoose error ".make is not a function" (#1304)
Browse files Browse the repository at this point in the history
  • Loading branch information
kirrg001 authored Sep 2, 2024
1 parent 2fccbaa commit 728f7a4
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 21 deletions.
74 changes: 61 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,11 @@
"mongodb": "^6.8.0",
"mongodb-v4": "npm:mongodb@^4.16.0",
"mongodb-v5": "npm:mongodb@^5.4.0",
"mongoose": "^8.5.4",
"mongoose": "^8.6.0",
"mongoose-v5": "npm:mongoose@^5.13.22",
"mongoose-v6": "npm:mongoose@^6.12.7",
"mongoose-v7": "npm:mongoose@^7.6.10",
"mongoose-v854": "npm:[email protected]",
"morgan": "^1.10.0",
"mssql": "^11.0.1",
"mssql-v10": "npm:mssql@^10.0.4",
Expand Down
4 changes: 3 additions & 1 deletion packages/collector/test/tracing/database/mongoose/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ const ATLAS_USER = process.env.ATLAS_USER || '';
const ATLAS_PASSWORD = process.env.ATLAS_PASSWORD || '';
const USE_ATLAS = process.env.USE_ATLAS === 'true';

const isLatestMajor = process.env.MONGOOSE_VERSION === 'latest' || process.env.MONGOOSE_VERSION === 'v854';

let connectString;
if (USE_ATLAS) {
connectString = `mongodb+srv://${ATLAS_USER}:${ATLAS_PASSWORD}@${ATLAS_CLUSTER}/mongoose?retryWrites=true&w=majority`;
Expand All @@ -51,7 +53,7 @@ mongoose.model(
);
const Person = mongoose.model('Person');

if (process.env.MONGOOSE_VERSION === 'latest' || process.env.MONGOOSE_VERSION === 'v7') {
if (isLatestMajor || process.env.MONGOOSE_VERSION === 'v7') {
(async () => {
try {
await mongoose.connect(connectString);
Expand Down
4 changes: 3 additions & 1 deletion packages/collector/test/tracing/database/mongoose/app.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ const ATLAS_USER = process.env.ATLAS_USER || '';
const ATLAS_PASSWORD = process.env.ATLAS_PASSWORD || '';
const USE_ATLAS = process.env.USE_ATLAS === 'true';

const isLatestMajor = process.env.MONGOOSE_VERSION === 'latest' || process.env.MONGOOSE_VERSION === 'v854';

let connectString;
if (USE_ATLAS) {
connectString = `mongodb+srv://${ATLAS_USER}:${ATLAS_PASSWORD}@${ATLAS_CLUSTER}/mongoose?retryWrites=true&w=majority`;
Expand All @@ -49,7 +51,7 @@ mongoose.model(
);
const Person = mongoose.model('Person');

if (process.env.MONGOOSE_VERSION === 'latest' || process.env.MONGOOSE_VERSION === 'v7') {
if (isLatestMajor || process.env.MONGOOSE_VERSION === 'v7') {
(async () => {
try {
await mongoose.connect(connectString);
Expand Down
10 changes: 6 additions & 4 deletions packages/collector/test/tracing/database/mongoose/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ const USE_ATLAS = process.env.USE_ATLAS === 'true';
// v5 uses mongodb v3
// v6 uses mongodb v4
// v7 uses mongodb v5
// v8 uses mongodb v6
// v8 (latest) uses mongodb v6

['latest', 'v7', 'v6', 'v5'].forEach(version => {
['latest', 'v854', 'v7', 'v6', 'v5'].forEach(version => {
let mochaSuiteFn = supportedVersion(process.versions.node) ? describe : describe.skip;

if (version === 'latest') {
const isLatestMajor = version === 'latest' || version === 'v854';

if (isLatestMajor) {
mochaSuiteFn = semver.lt(process.versions.node, '16.0.0') ? describe.skip : mochaSuiteFn;
} else if (version === 'v7') {
mochaSuiteFn = semver.lt(process.versions.node, '14.0.0') ? describe.skip : mochaSuiteFn;
Expand All @@ -35,7 +37,7 @@ const USE_ATLAS = process.env.USE_ATLAS === 'true';
}

// NOTE: require-mock is not working with esm apps. There is also no need to run the ESM APP for all versions.
if (process.env.RUN_ESM && version !== 'latest') return;
if (process.env.RUN_ESM && !isLatestMajor) return;

mochaSuiteFn(`tracing/mongoose@${version}`, function () {
const timeout = USE_ATLAS ? config.getTestTimeout() * 2 : config.getTestTimeout();
Expand Down
7 changes: 6 additions & 1 deletion packages/core/src/tracing/tracingUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,11 +259,16 @@ exports.findCallback = (/** @type {string | any[]} */ originalArgs) => {
let originalCallback;
let callbackIndex = -1;

// CASE: libraries pass a class into a function as argument
const isClass = (/** @type {any} */ fn) => {
return typeof fn === 'function' && /^\s*class\s+/.test(Function.prototype.toString.call(fn));
};

// If there is any function that takes two or more functions as an argument,
// the convention would be to pass in the callback as the last argument, thus searching
// from the end backwards might be marginally safer.
for (let i = originalArgs.length - 1; i >= 0; i--) {
if (typeof originalArgs[i] === 'function') {
if (typeof originalArgs[i] === 'function' && !isClass(originalArgs[i])) {
originalCallback = originalArgs[i];
callbackIndex = i;
break;
Expand Down

0 comments on commit 728f7a4

Please sign in to comment.