Skip to content

fix circular reference between modules db.js and db_ops.js - #1828

Closed
rogeriodegoiania wants to merge 1 commit into
mongodb:masterfrom
rogeriodegoiania:circular-reference1
Closed

fix circular reference between modules db.js and db_ops.js#1828
rogeriodegoiania wants to merge 1 commit into
mongodb:masterfrom
rogeriodegoiania:circular-reference1

Conversation

@rogeriodegoiania

Copy link
Copy Markdown

The db.js module when loaded depends on the db_ops.js module and when the db_ops.js module is loaded, it depends on the db.js module resulting in a circular reference.
The only need that the module db_ops.js has in relation to the module db.js is to have access to the constants Db.SYSTEM_ * (ex: Db.SYSTEM_NAMESPACE_COLLECTION)
This change created a new module to keep the constants called db_constants.js, and the circular dependency between the db.js and db_ops.js modules has been removed.

@mbroadst

Copy link
Copy Markdown
Member

Hi @rogeriodegoiania, it's curious that this isn't an active problem for us. Are you trying to use these files directly in your code?

@rogeriodegoiania

Copy link
Copy Markdown
Author

I use module loading "on the fly" and before version node-mongodb-native:3.1.0 we have no circular reference problem. We solve the problem by protecting the "loader" from circular references.

The standard require() returns an empty object in this scenario so the Db.SYSTEM_ constants used in the db_ops.js file will return undefined.

In the simulation below, using the nodejs native require(), I tried to show that Db.SYSTEM_USER_COLLECTION returns undefined but I really do not know if any errors can occur at this point. This being an expected behavior, we can cancel this pull request.

bug_mongodb

@p-mongo

p-mongo commented Sep 19, 2018

Copy link
Copy Markdown

db_ops does not reference db's constants in global scope, if it tried that it'd be in trouble. As it is by the time functions in db_ops that reference db's constants are run both modules are fully defined and the constants exist.

@rogeriodegoiania

Copy link
Copy Markdown
Author

Ok, I understood! thanks for the clarifications.

@p-mongo

p-mongo commented Sep 19, 2018

Copy link
Copy Markdown

Personally I wouldn't mind having a separate constants module and avoiding circular dependencies (but I am not working on the Node driver hence it's up to @mbroadst and team whether to accept this PR), I just thought I could provide an example when circular dependencies can be an issue and when they can be ok.

@daprahamian

Copy link
Copy Markdown
Contributor

This actually is a problematic error. I'm going to reopen this for review

@daprahamian daprahamian reopened this Oct 1, 2018
@daprahamian daprahamian self-assigned this Oct 1, 2018
Comment thread lib/db.js
* @type {Db}
*/

// Constants

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By removing this, it would technically break our public API, since these values were previously exposed publicly. Can we add the following code to ensure they are still exposed?

Object.assign(Db, dbConstants);

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, interesting approach.

Comment thread lib/db_constants.js
@@ -0,0 +1,9 @@
'use strict';

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we rename this file just constants.js? I imagine we have others that should move here as well

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we can rename this file.

Comment thread lib/db.js
*/

// Constants
Db.SYSTEM_NAMESPACE_COLLECTION = 'system.namespaces';

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a breaking change

@rogeriodegoiania

Copy link
Copy Markdown
Author

Sorry, I found two dependencies to db.js inside db_ops.js that I had not seen. I'm testing another alternative.

@mbroadst

mbroadst commented Oct 9, 2018

Copy link
Copy Markdown
Member

@rogeriodegoiania this missed the window for the v3.1.7 release, it will make it into the next one

@mbroadst

Copy link
Copy Markdown
Member

superseded by #1858

@mbroadst mbroadst closed this Oct 24, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants