Skip to content
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

Make mongoose adapter compatible with mongoose 6, 7 and 8 #397

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions packages/moleculer-db-adapter-mongoose/package-lock.json

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

4 changes: 2 additions & 2 deletions packages/moleculer-db-adapter-mongoose/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"license": "MIT",
"peerDependencies": {
"moleculer": "^0.12.0 || ^0.13.0 || ^0.14.0 || ^0.15.0",
"mongoose": "^6.5.4"
"mongoose": "^6.0.0 || ^7.0.0 || ^8.0.0"
},
"devDependencies": {
"benchmarkify": "^3.0.0",
Expand All @@ -37,7 +37,7 @@
"jest-cli": "^27.2.5",
"lolex": "^6.0.0",
"moleculer": "^0.14.22",
"mongoose": "^6.12.3",
"mongoose": "^6.13.3",
"nodemon": "^2.0.19",
"npm-check": "^5.9.2"
},
Expand Down
96 changes: 41 additions & 55 deletions packages/moleculer-db-adapter-mongoose/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,57 +62,45 @@ class MongooseDbAdapter {
*
* @memberof MongooseDbAdapter
*/
connect() {
let conn;

if (this.model) {
/* istanbul ignore next */
if (mongoose.connection.readyState == 1) {
this.db = mongoose.connection;
return Promise.resolve();
} else if (mongoose.connection.readyState == 2) {
conn = mongoose.connection.asPromise();
} else {
conn = mongoose.connect(this.uri, this.opts);
}
} else if (this.schema) {
conn = new Promise(resolve =>{
const c = mongoose.createConnection(this.uri, this.opts);
this.model = c.model(this.modelName, this.schema);
resolve(c);
});
}
async connect() {
try {
if (this.model) {
/* istanbul ignore next */
if (mongoose.connection.readyState !== 1) {
await mongoose.connect(this.uri, this.opts);
}

return conn.then(() => {
this.conn = mongoose.connection;
await mongoose.connection.asPromise();

if (mongoose.connection.readyState != mongoose.connection.states.connected) {
throw new MoleculerError(
`MongoDB connection failed . Status is "${
mongoose.connection.states[mongoose.connection._readyState]
}"`
);
}
this.conn = mongoose.connection;
this.model = mongoose.model(this.model["modelName"], this.model["schema"]);
} else if (this.schema) {
await mongoose.createConnection(this.uri, this.opts);
await mongoose.connection.asPromise();

if(this.model) {
this.model = mongoose.model(this.model["modelName"],this.model["schema"]);
}

this.db = mongoose.connection.db;

if (!this.db) {
throw new MoleculerError("MongoDB connection failed to get DB object");
this.conn = mongoose.connection;
this.model = this.conn.model(this.modelName, this.schema);
}
} catch (e) {
throw new MoleculerError(
`MongoDB connection failed . Status is "${
mongoose.connection.states[mongoose.connection.readyState]
}"`
);
}

this.service.logger.info("MongoDB adapter has connected successfully.");
this.db = this.conn.db;

if (!this.db) {
throw new MoleculerError("MongoDB connection failed to get DB object");
}

/* istanbul ignore next */
mongoose.connection.on("disconnected", () => this.service.logger.warn("Mongoose adapter has disconnected."));
mongoose.connection.on("error", err => this.service.logger.error("MongoDB error.", err));
mongoose.connection.on("reconnect", () => this.service.logger.info("Mongoose adapter has reconnected."));
this.service.logger.info("MongoDB adapter has connected successfully.");

});
/* istanbul ignore next */
this.conn.on("disconnected", () => this.service.logger.warn("Mongoose adapter has disconnected."));
this.conn.on("error", err => this.service.logger.error("MongoDB error.", err));
this.conn.on("reconnected", () => this.service.logger.info("Mongoose adapter has reconnected."));
}

/**
Expand All @@ -122,16 +110,14 @@ class MongooseDbAdapter {
*
* @memberof MongooseDbAdapter
*/
disconnect() {
return new Promise(resolve => {
if (this.db && this.db.close) {
this.db.close(resolve);
} else if (this.conn && this.conn.close) {
this.conn.close(resolve);
} else {
mongoose.connection.close(resolve);
}
});
async disconnect() {
if (typeof this.db?.close === "function") {
await this.db.close();
} else if (typeof this.conn?.close === "function") {
await this.conn.close();
} else {
await mongoose.connection.close();
}
}

/**
Expand Down Expand Up @@ -194,7 +180,7 @@ class MongooseDbAdapter {
}

/**
* Get count of filtered entites
* Get count of filtered entities
*
* Available filter props:
* - search
Expand Down Expand Up @@ -246,7 +232,7 @@ class MongooseDbAdapter {
* @memberof MongooseDbAdapter
*/
updateMany(query, update) {
return this.model.updateMany(query, update, { multi: true, "new": true }).then(res => {
return this.model.updateMany(query, update).then(res => {
return res.modifiedCount;
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ if (process.versions.node.split(".")[0] < 14) {

let fakeDb = {
on: jest.fn(),
close: jest.fn((fn) => fn()),
close: jest.fn(() => {}),
model: jest.fn(() => fakeModel),
};

Expand Down Expand Up @@ -176,7 +176,7 @@ if (process.versions.node.split(".")[0] < 14) {
expect.any(Function)
);
expect(adapter.db.on).toHaveBeenCalledWith(
"reconnect",
"reconnected",
expect.any(Function)
);
});
Expand Down Expand Up @@ -599,8 +599,7 @@ if (process.versions.node.split(".")[0] < 14) {
expect(adapter.model.updateMany).toHaveBeenCalledTimes(1);
expect(adapter.model.updateMany).toHaveBeenCalledWith(
query,
update,
{ multi: true, new: true }
update
);
});
});
Expand Down
Loading