Skip to content

Commit

Permalink
Merge pull request #367 from thib3113/issue-361
Browse files Browse the repository at this point in the history
fix(moleculer-db-adapter-mongoose): remove connecting error/warning, Fix #361
  • Loading branch information
icebob authored Sep 24, 2023
2 parents 394bde3 + 2b2abbd commit 35c7df8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 29 deletions.
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
"program": "${workspaceRoot}/dev",
"cwd": "${workspaceRoot}",
"args": [
"moleculer-db-adapter-mongo",
"issue"
"moleculer-db-adapter-mongoose",
"simple"
]
},
{
Expand Down
38 changes: 17 additions & 21 deletions packages/moleculer-db-adapter-mongoose/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,15 @@ class MongooseDbAdapter {
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 = Promise.resolve(mongoose.connection);
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);
Expand All @@ -86,10 +84,8 @@ class MongooseDbAdapter {
});
}


return conn.then(_result => {
const result = _result || conn;
this.conn = conn;
return conn.then(() => {
this.conn = mongoose.connection;

if (mongoose.connection.readyState != mongoose.connection.states.connected) {
throw new MoleculerError(
Expand All @@ -99,14 +95,11 @@ class MongooseDbAdapter {
);
}

if(this.model)
this.model = _result.model(this.model["modelName"],this.model["schema"]);

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

if (result.connection)
this.db = result.connection.db;
else
this.db = result.db;
this.db = mongoose.connection.db;

if (!this.db) {
throw new MoleculerError("MongoDB connection failed to get DB object");
Expand All @@ -116,12 +109,11 @@ class MongooseDbAdapter {


/* istanbul ignore next */
result.connection.on("disconnected", () => this.service.logger.warn("Mongoose adapter has disconnected."));
result.connection.on("error", err => this.service.logger.error("MongoDB error.", err));
result.connection.on("reconnect", () => this.service.logger.info("Mongoose adapter has reconnected."));
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."));

});

}

/**
Expand Down Expand Up @@ -255,7 +247,9 @@ class MongooseDbAdapter {
* @memberof MongooseDbAdapter
*/
updateMany(query, update) {
return this.model.updateMany(query, update, { multi: true, "new": true }).then(res => res.n);
return this.model.updateMany(query, update, { multi: true, "new": true }).then(res => {
return res.modifiedCount;
});
}

/**
Expand All @@ -280,7 +274,9 @@ class MongooseDbAdapter {
* @memberof MongooseDbAdapter
*/
removeMany(query) {
return this.model.deleteMany(query).then(res => res.n);
return this.model.deleteMany(query).then(res => {
return res.deletedCount;
});
}

/**
Expand All @@ -303,7 +299,7 @@ class MongooseDbAdapter {
* @memberof MongooseDbAdapter
*/
clear() {
return this.model.deleteMany({}).then(res => res.n);
return this.model.deleteMany({}).then(res => res.deletedCount);
}

/**
Expand Down
16 changes: 10 additions & 6 deletions packages/moleculer-db-adapter-mongoose/test/unit/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ if (process.versions.node.split(".")[0] < 14) {
findOne: jest.fn(() => query()),
findById: jest.fn(() => query()),
create: jest.fn(() => Promise.resolve()),
updateMany: jest.fn(() => Promise.resolve({ n: 2 })),
updateMany: jest.fn(() => Promise.resolve({ modifiedCount: 2 })),
findByIdAndUpdate: jest.fn(() => Promise.resolve(doc)),
deleteMany: jest.fn(() => Promise.resolve({ n: 2 })),
deleteMany: jest.fn(() => Promise.resolve({ deletedCount: 2 })),
findByIdAndRemove: jest.fn(() => Promise.resolve()),
}
);
Expand Down Expand Up @@ -142,11 +142,15 @@ if (process.versions.node.split(".")[0] < 14) {
mongoose.connect = jest.fn(() => {
mongoose.connection.readyState =
mongoose.connection.states.connected;
return Promise.resolve({
connection: { ...fakeDb, db: fakeDb },
model: jest.fn(() => fakeModel),
});
return Promise.resolve();
});

mongoose.model = jest.fn(() => fakeModel);

Object.entries(fakeDb).forEach(([k, v]) => {
mongoose.connection[k] = v;
});
mongoose.connection.db = fakeDb;
});

it("call connect with uri", () => {
Expand Down

0 comments on commit 35c7df8

Please sign in to comment.