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

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

Closed
wants to merge 1 commit into from
Closed
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
11 changes: 2 additions & 9 deletions lib/db.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

const dbConstants = require("./db_constants");
const EventEmitter = require('events').EventEmitter;
const inherits = require('util').inherits;
const getSingleProperty = require('./utils').getSingleProperty;
Expand Down Expand Up @@ -532,7 +533,7 @@ Db.prototype.listCollections = function(filter, options) {
// Return options
const _options = { transforms: listCollectionsTransforms(this.s.databaseName) };
// Get the cursor
cursor = this.collection(Db.SYSTEM_NAMESPACE_COLLECTION).find(filter, _options);
cursor = this.collection(dbConstants.SYSTEM_NAMESPACE_COLLECTION).find(filter, _options);
// Do we have a readPreference, apply it
if (options.readPreference) cursor.setReadPreference(options.readPreference);
// Set the passed in batch size if one was provided
Expand Down Expand Up @@ -973,12 +974,4 @@ Db.prototype.getLogger = function() {
* @type {Db}
*/

// Constants
Copy link
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
Author

Choose a reason for hiding this comment

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

Yes, interesting approach.

Db.SYSTEM_NAMESPACE_COLLECTION = 'system.namespaces';
Copy link
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

Db.SYSTEM_INDEX_COLLECTION = 'system.indexes';
Db.SYSTEM_PROFILE_COLLECTION = 'system.profile';
Db.SYSTEM_USER_COLLECTION = 'system.users';
Db.SYSTEM_COMMAND_COLLECTION = '$cmd';
Db.SYSTEM_JS_COLLECTION = 'system.js';

module.exports = Db;
9 changes: 9 additions & 0 deletions lib/db_constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';
Copy link
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
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.


// Constants
module.exports.SYSTEM_NAMESPACE_COLLECTION = 'system.namespaces';
module.exports.SYSTEM_INDEX_COLLECTION = 'system.indexes';
module.exports.SYSTEM_PROFILE_COLLECTION = 'system.profile';
module.exports.SYSTEM_USER_COLLECTION = 'system.users';
module.exports.SYSTEM_COMMAND_COLLECTION = '$cmd';
module.exports.SYSTEM_JS_COLLECTION = 'system.js';
8 changes: 4 additions & 4 deletions lib/operations/db_ops.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict';

const dbConstants = require("../db_constants");
const applyWriteConcern = require('../utils').applyWriteConcern;
const Code = require('mongodb-core').BSON.Code;
const resolveReadPreference = require('../utils').resolveReadPreference;
const crypto = require('crypto');
const Db = require('../db');
const debugOptions = require('../utils').debugOptions;
const handleCallback = require('../utils').handleCallback;
const MongoError = require('mongodb-core').MongoError;
Expand Down Expand Up @@ -83,7 +83,7 @@ function addUser(db, username, password, options, callback) {
const db = options.dbName ? new Db(options.dbName, db.s.topology, db.s.options) : db;

// Fetch a user collection
const collection = db.collection(Db.SYSTEM_USER_COLLECTION);
const collection = db.collection(dbConstants.SYSTEM_USER_COLLECTION);

// Check if we are inserting the first user
count(collection, {}, finalOptions, (err, count) => {
Expand Down Expand Up @@ -296,7 +296,7 @@ function createIndex(db, name, fieldOrSpec, options, callback) {
finalOptions.checkKeys = false;
// Insert document
db.s.topology.insert(
`${db.s.databaseName}.${Db.SYSTEM_INDEX_COLLECTION}`,
`${db.s.databaseName}.${dbConstants.SYSTEM_INDEX_COLLECTION}`,
doc,
finalOptions,
(err, result) => {
Expand Down Expand Up @@ -638,7 +638,7 @@ function removeUser(db, username, options, callback) {
const db = options.dbName ? new Db(options.dbName, db.s.topology, db.s.options) : db;

// Fetch a user collection
const collection = db.collection(Db.SYSTEM_USER_COLLECTION);
const collection = db.collection(dbConstants.SYSTEM_USER_COLLECTION);

// Locate the user
findOne(collection, { user: username }, finalOptions, (err, user) => {
Expand Down