moleculer-db icon indicating copy to clipboard operation
moleculer-db copied to clipboard

Mongoose 6

Open d0whc3r opened this issue 4 years ago • 8 comments

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch [email protected] for the project I'm working on.

With these modifications it could be compatible with mongoose 6, maybe there are more modifications to do, but it works for now

Here is the diff that solved my problem:

diff --git a/node_modules/moleculer-db-adapter-mongoose/index.d.ts b/node_modules/moleculer-db-adapter-mongoose/index.d.ts
index 633a64a..b00367c 100644
--- a/node_modules/moleculer-db-adapter-mongoose/index.d.ts
+++ b/node_modules/moleculer-db-adapter-mongoose/index.d.ts
@@ -1,7 +1,6 @@
 declare module "moleculer-db-adapter-mongoose" {
 	import { Service, ServiceBroker } from "moleculer";
 	import {
-		ConnectionOptions,
 		Document,
 		Query as DocumentQuery, //reff: https://github.com/Automattic/mongoose/issues/10036#issuecomment-803144616
 		Model,
@@ -41,7 +40,7 @@ declare module "moleculer-db-adapter-mongoose" {
 
 	class MongooseDbAdapter<TDocument extends Document> {
 		uri: string;
-		opts?: ConnectionOptions;
+		opts?: any;
 		broker: ServiceBroker;
 		service: Service;
 		model: Model<TDocument>;
@@ -52,7 +51,7 @@ declare module "moleculer-db-adapter-mongoose" {
 		/**
 		 * Creates an instance of MongooseDbAdapter.
 		 */
-		constructor(uri: string, opts?: ConnectionOptions);
+		constructor(uri: string, opts?: any);
 		/**
 		 * Initialize adapter
 		 */
diff --git a/node_modules/moleculer-db-adapter-mongoose/src/index.js b/node_modules/moleculer-db-adapter-mongoose/src/index.js
index c0d95e5..0cc760b 100644
--- a/node_modules/moleculer-db-adapter-mongoose/src/index.js
+++ b/node_modules/moleculer-db-adapter-mongoose/src/index.js
@@ -11,10 +11,6 @@ const Promise	= require("bluebird");
 const { ServiceSchemaError } = require("moleculer").Errors;
 const mongoose  = require("mongoose");
 
-mongoose.set("useNewUrlParser", true);
-mongoose.set("useFindAndModify", false);
-mongoose.set("useCreateIndex", true);
-
 class MongooseDbAdapter {
 
 	/**
@@ -74,12 +70,12 @@ class MongooseDbAdapter {
 				this.db = mongoose.connection;
 				return Promise.resolve();
 			} else if (mongoose.connection.readyState == 2) {
-				conn = mongoose.connection;
+				conn = mongoose.connection.asPromise();
 			} else {
 				conn = mongoose.connect(this.uri, this.opts);
 			}
 		} else if (this.schema) {
-			conn = mongoose.createConnection(this.uri, this.opts);
+			conn = mongoose.createConnection(this.uri, this.opts).asPromise();
 			this.model = conn.model(this.modelName, this.schema);
 		}
 
@@ -95,9 +91,9 @@ class MongooseDbAdapter {
 			this.service.logger.info("MongoDB adapter has connected successfully.");
 
 			/* istanbul ignore next */
-			this.db.on("disconnected", () => this.service.logger.warn("Mongoose adapter has disconnected."));
-			this.db.on("error", err => this.service.logger.error("MongoDB error.", err));
-			this.db.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."));
 		});
 	}
 

d0whc3r avatar Aug 25 '21 18:08 d0whc3r

remove , typing is not a good option, as it has an impact on existing applications.

What are the advantages of using version 6 of mongoose?

devalexandre avatar Nov 19 '21 05:11 devalexandre

@devalexandre Being able to use the timeseries features of mongodb introduced in version 5.0

Nevermind-s avatar Jan 26 '22 22:01 Nevermind-s

Here's an edit with the new typings of mongoose 6.0

diff --git a/node_modules/moleculer-db-adapter-mongoose/index.d.ts b/node_modules/moleculer-db-adapter-mongoose/index.d.ts
index 633a64a..8ed3276 100644
--- a/node_modules/moleculer-db-adapter-mongoose/index.d.ts
+++ b/node_modules/moleculer-db-adapter-mongoose/index.d.ts
@@ -1,7 +1,7 @@
 declare module "moleculer-db-adapter-mongoose" {
 	import { Service, ServiceBroker } from "moleculer";
 	import {
-		ConnectionOptions,
+		ConnectOptions,
 		Document,
 		Query as DocumentQuery, //reff: https://github.com/Automattic/mongoose/issues/10036#issuecomment-803144616
 		Model,
@@ -41,7 +41,7 @@ declare module "moleculer-db-adapter-mongoose" {
 
 	class MongooseDbAdapter<TDocument extends Document> {
 		uri: string;
-		opts?: ConnectionOptions;
+		opts?: ConnectOptions;
 		broker: ServiceBroker;
 		service: Service;
 		model: Model<TDocument>;
@@ -52,7 +52,7 @@ declare module "moleculer-db-adapter-mongoose" {
 		/**
 		 * Creates an instance of MongooseDbAdapter.
 		 */
-		constructor(uri: string, opts?: ConnectionOptions);
+		constructor(uri: string, opts?: ConnectOptions);
 		/**
 		 * Initialize adapter
 		 */
diff --git a/node_modules/moleculer-db-adapter-mongoose/src/index.js b/node_modules/moleculer-db-adapter-mongoose/src/index.js
index deff2b5..42ed2de 100644
--- a/node_modules/moleculer-db-adapter-mongoose/src/index.js
+++ b/node_modules/moleculer-db-adapter-mongoose/src/index.js
@@ -11,9 +11,6 @@ const Promise	= require("bluebird");
 const { ServiceSchemaError } = require("moleculer").Errors;
 const mongoose  = require("mongoose");
 
-mongoose.set("useNewUrlParser", true);
-mongoose.set("useFindAndModify", false);
-mongoose.set("useCreateIndex", true);
 
 class MongooseDbAdapter {
 
@@ -74,12 +71,12 @@ class MongooseDbAdapter {
 				this.db = mongoose.connection;
 				return Promise.resolve();
 			} else if (mongoose.connection.readyState == 2) {
-				conn = mongoose.connection;
+				conn = mongoose.connection.asPromise();
 			} else {
 				conn = mongoose.connect(this.uri, this.opts);
 			}
 		} else if (this.schema) {
-			conn = mongoose.createConnection(this.uri, this.opts);
+			conn = mongoose.createConnection(this.uri, this.opts).asPromise();
 			this.model = conn.model(this.modelName, this.schema);
 		}
 
@@ -95,9 +92,9 @@ class MongooseDbAdapter {
 			this.service.logger.info("MongoDB adapter has connected successfully.");
 
 			/* istanbul ignore next */
-			this.db.on("disconnected", () => this.service.logger.warn("Mongoose adapter has disconnected."));
-			this.db.on("error", err => this.service.logger.error("MongoDB error.", err));
-			this.db.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."));
 		});
 	}
 

Nevermind-s avatar Jan 27 '22 16:01 Nevermind-s

hi guys, any concerns on this? since cloud.mongodb.com is using mongo v5 next week and we will need mongoose 6 to connect to the db

luuduchieu avatar Feb 10 '22 11:02 luuduchieu

I'm doing some tests about it.

devalexandre avatar Mar 11 '22 23:03 devalexandre

Hi @devalexandre, any update on this?

lakshdhamija avatar Apr 05 '22 12:04 lakshdhamija

Hi @devalexandre, any update on this?

I'm testing, but why use mongoose 6?

devalexandre avatar Apr 05 '22 23:04 devalexandre

@devalexandre Being able to use the timeseries features of mongodb introduced in version 5.0

I did some test, but when use mongoose 6, have some problem.

[2022-04-06T01:23:18.765Z] INFO  melissa-403360/POSTS: MongoDB adapter has connected successfully.
[2022-04-06T01:23:18.771Z] INFO  melissa-403360/REGISTRY: 'posts' service is registered.
[2022-04-06T01:23:18.771Z] INFO  melissa-403360/POSTS: Service 'posts' started.
[2022-04-06T01:23:18.773Z] INFO  melissa-403360/BROKER: ✔ ServiceBroker with 2 service(s) started successfully in 37ms.
ERROR MongooseError: Operation `posts.insertOne()` buffering timed out after 10000ms
    at Timeout.<anonymous> (/home/alexandre/opensource/moleculer-mongo-6/node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js:151:23)
    at listOnTimeout (internal/timers.js:557:17)
    at processTimers (internal/timers.js:500:7)

because need update in package.json in adapter, it break. I create a draft for update it.

https://github.com/moleculerjs/moleculer-db/pull/314

devalexandre avatar Apr 06 '22 01:04 devalexandre