-
Notifications
You must be signed in to change notification settings - Fork 122
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
mongoose adapter trouble with connect #378
Comments
Please create a fix PR and I will help with tests |
I dont think #370 is good solution
this line will create new connection but the model can be connected before
This case was handled in current version and in my pull moleculer-db/packages/moleculer-db-adapter-mongoose/src/index.js Lines 70 to 76 in 81f4f3f
I think #370 is good to upgrade mongoose to newer version but handle connect/disconnect is not |
@0x0a0d can you describe "model" ? because actually, model is not directly used, but extracted ... So, not connected to the global connection .
( + main concept of moleculer-db is "database per service" . Reusing global connection is not "database per service" : https://moleculer.services/docs/0.14/moleculer-db ) |
See this require('dotenv').config()
const { createConnection, Schema } = require('mongoose')
process.env.MONGO_URI = process.env.MONGO_URI || 'mongodb://localhost:27017/test'
!async function(){
const conn1 = await createConnection(process.env.MONGO_URI).asPromise()
const conn2 = await createConnection(process.env.MONGO_URI).asPromise()
const foo = conn1.model('foo', new Schema({}))
const bar = conn2.model('bar', foo.schema)
console.log(`Connection of foo and bar is ${foo.db === bar.db ? 'same' : 'different'}`)
await conn1.close()
await conn2.close()
}() Your code create new connection for every services but in some cases, dev can do this const shareConnection = createConnection(...)
const fooModel = shareConnection.model('foo', {...})
const barModel = shareConnection.model('foo', {...})
const serviceFoo = {
...,
model: fooModel,
}
const serviceBar = {
...,
model: barModel,
} |
@0x0a0d yes, and if dev do this, they don't respect the base concept of moleculer-db : "Database per service" pattern : https://moleculer.services/docs/0.14/moleculer-db . |
I dont think one database mean one database connection |
yes . But in my opinion (and just my opinion here), a service need to manage his own connection, to be totally autonomous . If another service crash the connection or disconnect it, it need to doesn't impact other services . If a service want to reuse his connection, he can acces it from functions . |
see this there are 2 ways to create a model
that mean: if model is existed, there are 50% chances a connection was made before at the end, this also just my opinion, lets icebod decide |
yes, @icebob will decide . About passing schema or model . I see model only like "sugar" . because schema need two parameters ( schema + schemaName ) |
As I see the mongoose connection handling is not an easy problem. All adapters (except mongoose) create a custom connection to the db. So it would be good to follow the same logic in Mongoose as well, but using @thib3113 @0x0a0d |
My pull code follow current adapter mongoose logic
|
Actually, the connection from the
( so only one parameter instead of two with I understand that reusing the global connection can save a connection to mongodb ... But so the services will be linked together . (and so => if I stop serviceA, serviceB will crash because connection is closed ?) . A always think it's better to follow the same path than other modules ... one connection per service, and so they are totally independent . @icebob you are the expert here, it's up to you to do a choice |
I don't want to kill any common use-case. My suggestion is to add a Is it acceptable to both of you? |
I created a pull #338 that fix a problem in
connect()
method of adapter mongooseToday, I've created a new project with moleculer-db-adapter-mongoose and I still got error message when connect to mongodb
problem
The code below can be used to reproduce the problem
Here is screenshot
You can see that the
foo
service was not make connection to db server but success at retryFor more detail, here what happen
schema
, so the line below resolve with theconn
frommongoose.createConnection
moleculer-db/packages/moleculer-db-adapter-mongoose/src/index.js
Lines 80 to 85 in 072f1b6
conn.then()
, but instead checkconn
is connected or not, the code usemongoose.connection
that always point tomongoose.connections[0]
(default mongoose connection, but with any call ofmongoose.createConnection
will result to append newconnection
tomongoose.connections
).connect()
was thrown an error and catched bymoleculer-db
that asked adapter to reconnect.moleculer-db/packages/moleculer-db/src/index.js
Lines 1107 to 1110 in 072f1b6
At line 83, when adapter got
schema
field, adapter create amodel
moleculer-db/packages/moleculer-db-adapter-mongoose/src/index.js
Line 83 in 072f1b6
When adapter do
connect()
again, becausemodel
field is set, so it jump to below code that usemongoose.connection
to connectmoleculer-db/packages/moleculer-db-adapter-mongoose/src/index.js
Lines 70 to 80 in 072f1b6
So at this time, connection was successfully
But for every services use
schema
will share same mongoose connectionupdated code
screenshot
fix
I copied use this.model.db as NativeConnection instance #338
connect
anddisconnect
methods, create a patch file using bypatch-package
module that fix this problempatches.zip
Unzip this file to root path then run
npx patch-package
or copy below lines to overwrite connect, disconnect method in
moleculer-db-adapter-mongoose/src/index.js
moleculer-db/packages/moleculer-db-adapter-mongoose/src/index.js
Lines 59 to 151 in 81f4f3f
need support
@icebob if you think this fix is correct, can you (or someone else) can help me to write tests? I not good to write test and dont have enough time at this moment
The text was updated successfully, but these errors were encountered: