From 6c67d7c69a2a4e8a538d8b49adc3ea6f847555d3 Mon Sep 17 00:00:00 2001 From: Hage Yaapa Date: Thu, 23 May 2019 18:52:31 +0530 Subject: [PATCH] chore: meaningful variable names --- lib/mongodb.js | 366 +++++++++++++++++++++--------------------- test/objectid.test.js | 30 ++-- 2 files changed, 198 insertions(+), 198 deletions(-) diff --git a/lib/mongodb.js b/lib/mongodb.js index 93e01add3..7b1fe4598 100644 --- a/lib/mongodb.js +++ b/lib/mongodb.js @@ -336,41 +336,41 @@ MongoDB.prototype.getDefaultIdType = function() { /** * Get collection name for a given model - * @param {String} model Model name + * @param {String} modelName The model name * @returns {String} collection name */ -MongoDB.prototype.collectionName = function(model) { - var modelClass = this._models[model]; +MongoDB.prototype.collectionName = function(modelName) { + var modelClass = this._models[modelName]; if (modelClass.settings.mongodb) { - model = modelClass.settings.mongodb.collection || model; + modelName = modelClass.settings.mongodb.collection || modelName; } - return model; + return modelName; }; /** * Access a MongoDB collection by model name - * @param {String} model The model name + * @param {String} modelName The model name * @returns {*} */ -MongoDB.prototype.collection = function(model) { +MongoDB.prototype.collection = function(modelName) { if (!this.db) { throw new Error(g.f('{{MongoDB}} connection is not established')); } - var collectionName = this.collectionName(model); + var collectionName = this.collectionName(modelName); return this.db.collection(collectionName); }; /*! * Convert the data from database to JSON * - * @param {String} model The model name + * @param {String} modelName The model name * @param {Object} data The data from DB */ -MongoDB.prototype.fromDatabase = function(model, data) { +MongoDB.prototype.fromDatabase = function(modelName, data) { if (!data) { return null; } - var modelInfo = this._models[model] || this.dataSource.modelBuilder.definitions[model]; + var modelInfo = this._models[modelName] || this.dataSource.modelBuilder.definitions[modelName]; var props = modelInfo.properties; for (var p in props) { var prop = props[p]; @@ -400,7 +400,7 @@ MongoDB.prototype.fromDatabase = function(model, data) { } } - data = this.fromDatabaseToPropertyNames(model, data); + data = this.fromDatabaseToPropertyNames(modelName, data); return data; }; @@ -408,17 +408,17 @@ MongoDB.prototype.fromDatabase = function(model, data) { /*! * Convert JSON to database-appropriate format * - * @param {String} model The model name + * @param {String} modelName The model name * @param {Object} data The JSON data to convert */ -MongoDB.prototype.toDatabase = function(model, data) { - const modelInstance = this._models[model].model; - var props = this._models[model].properties; +MongoDB.prototype.toDatabase = function(modelName, data) { + const modelInstance = this._models[modelName].model; + var props = this._models[modelName].properties; if (this.settings.enableGeoIndexing !== true) { visitAllProperties(data, modelInstance, coerceDecimalProperty); // Override custom column names - data = this.fromPropertyToDatabaseNames(model, data); + data = this.fromPropertyToDatabaseNames(modelName, data); return data; } @@ -435,18 +435,18 @@ MongoDB.prototype.toDatabase = function(model, data) { visitAllProperties(data, modelInstance, coerceDecimalProperty); // Override custom column names - data = this.fromPropertyToDatabaseNames(model, data); + data = this.fromPropertyToDatabaseNames(modelName, data); if (debug.enabled) debug('toDatabase data: ', util.inspect(data)); return data; }; /** * Execute a mongodb command - * @param {String} model The model name + * @param {String} modelName The model name * @param {String} command The command name * @param [...] params Parameters for the given command */ -MongoDB.prototype.execute = function(model, command) { +MongoDB.prototype.execute = function(modelName, command) { var self = this; // Get the parameters for the given command var args = [].slice.call(arguments, 2); @@ -466,7 +466,7 @@ MongoDB.prototype.execute = function(model, command) { if (err) { debug( 'Connection not established - MongoDB: model=%s command=%s -- error=%s', - model, + modelName, command, err ); @@ -478,7 +478,7 @@ MongoDB.prototype.execute = function(model, command) { function doExecute() { var collection; var context = Object.assign({}, { - model: model, + model: modelName, collection: collection, req: { command: command, @@ -487,7 +487,7 @@ MongoDB.prototype.execute = function(model, command) { }); try { - collection = self.collection(model); + collection = self.collection(modelName); } catch (err) { debug('Error: ', err); callback(err); @@ -511,7 +511,7 @@ MongoDB.prototype.execute = function(model, command) { } done(err, result); }; - debug('MongoDB: model=%s command=%s', model, command, args); + debug('MongoDB: model=%s command=%s', modelName, command, args); return collection[command].apply(collection, args); }, callback @@ -519,15 +519,15 @@ MongoDB.prototype.execute = function(model, command) { } }; -MongoDB.prototype.coerceId = function(model, id, options) { +MongoDB.prototype.coerceId = function(modelName, id, options) { // See https://github.com/strongloop/loopback-connector-mongodb/issues/206 if (id == null) return id; var self = this; var idValue = id; - var idName = self.idName(model); + var idName = self.idName(modelName); // Type conversion for id - var idProp = self.getPropertyDefinition(model, idName); + var idProp = self.getPropertyDefinition(modelName, idName); if (idProp && typeof idProp.type === 'function') { if (!(idValue instanceof idProp.type)) { idValue = idProp.type(id); @@ -537,7 +537,7 @@ MongoDB.prototype.coerceId = function(model, id, options) { } } - if (self.isObjectIDProperty(model, idProp, idValue, options)) { + if (self.isObjectIDProperty(modelName, idProp, idValue, options)) { idValue = ObjectID(idValue); } } @@ -546,39 +546,39 @@ MongoDB.prototype.coerceId = function(model, id, options) { /** * Create a new model instance for the given data - * @param {String} model The model name + * @param {String} modelName The model name * @param {Object} data The model data * @param {Function} [callback] The callback function */ -MongoDB.prototype.create = function(model, data, options, callback) { +MongoDB.prototype.create = function(modelName, data, options, callback) { var self = this; if (self.debug) { - debug('create', model, data); + debug('create', modelName, data); } - var idValue = self.getIdValue(model, data); - var idName = self.idName(model); + var idValue = self.getIdValue(modelName, data); + var idName = self.idName(modelName); if (idValue === null) { delete data[idName]; // Allow MongoDB to generate the id } else { - var oid = self.coerceId(model, idValue, options); // Is it an Object ID?c + var oid = self.coerceId(modelName, idValue, options); // Is it an Object ID?c data._id = oid; // Set it to _id if (idName !== '_id') { delete data[idName]; } } - data = self.toDatabase(model, data); + data = self.toDatabase(modelName, data); - this.execute(model, 'insertOne', data, {safe: true}, function(err, result) { + this.execute(modelName, 'insertOne', data, {safe: true}, function(err, result) { if (self.debug) { - debug('create.callback', model, err, result); + debug('create.callback', modelName, err, result); } if (err) { return callback(err); } idValue = result.ops[0]._id; - idValue = self.coerceId(model, idValue, options); + idValue = self.coerceId(modelName, idValue, options); // Wrap it to process.nextTick as delete data._id seems to be interferring // with mongo insert process.nextTick(function() { @@ -592,35 +592,35 @@ MongoDB.prototype.create = function(model, data, options, callback) { /** * Save the model instance for the given data - * @param {String} model The model name + * @param {String} modelName The model name * @param {Object} data The model data * @param {Function} [callback] The callback function */ -MongoDB.prototype.save = function(model, data, options, callback) { +MongoDB.prototype.save = function(modelName, data, options, callback) { var self = this; if (self.debug) { - debug('save', model, data); + debug('save', modelName, data); } - var idValue = self.getIdValue(model, data); - var idName = self.idName(model); + var idValue = self.getIdValue(modelName, data); + var idName = self.idName(modelName); - var oid = self.coerceId(model, idValue, options); + var oid = self.coerceId(modelName, idValue, options); data._id = oid; if (idName !== '_id') { delete data[idName]; } - data = self.toDatabase(model, data); + data = self.toDatabase(modelName, data); - this.execute(model, 'updateOne', {_id: oid}, {$set: data}, {upsert: true}, function(err, result) { + this.execute(modelName, 'updateOne', {_id: oid}, {$set: data}, {upsert: true}, function(err, result) { if (!err) { - self.setIdValue(model, data, idValue); + self.setIdValue(modelName, data, idValue); if (idName !== '_id') { delete data._id; } } if (self.debug) { - debug('save.callback', model, err, result); + debug('save.callback', modelName, err, result); } var info = {}; @@ -647,20 +647,20 @@ MongoDB.prototype.save = function(model, data, options, callback) { /** * Check if a model instance exists by id - * @param {String} model The model name + * @param {String} modelName The model name * @param {*} id The id value * @param {Function} [callback] The callback function * */ -MongoDB.prototype.exists = function(model, id, options, callback) { +MongoDB.prototype.exists = function(modelName, id, options, callback) { var self = this; if (self.debug) { - debug('exists', model, id); + debug('exists', modelName, id); } - id = self.coerceId(model, id, options); - this.execute(model, 'findOne', {_id: id}, function(err, data) { + id = self.coerceId(modelName, id, options); + this.execute(modelName, 'findOne', {_id: id}, function(err, data) { if (self.debug) { - debug('exists.callback', model, id, err, data); + debug('exists.callback', modelName, id, err, data); } callback(err, !!(!err && data)); }); @@ -668,23 +668,23 @@ MongoDB.prototype.exists = function(model, id, options, callback) { /** * Find a model instance by id - * @param {String} model The model name + * @param {String} modelName The model name * @param {*} id The id value * @param {Function} [callback] The callback function */ -MongoDB.prototype.find = function find(model, id, options, callback) { +MongoDB.prototype.find = function find(modelName, id, options, callback) { var self = this; if (self.debug) { - debug('find', model, id); + debug('find', modelName, id); } - var idName = self.idName(model); - var oid = self.coerceId(model, id, options); - this.execute(model, 'findOne', {_id: oid}, function(err, data) { + var idName = self.idName(modelName); + var oid = self.coerceId(modelName, id, options); + this.execute(modelName, 'findOne', {_id: oid}, function(err, data) { if (self.debug) { - debug('find.callback', model, id, err, data); + debug('find.callback', modelName, id, err, data); } - data = self.fromDatabase(model, data); + data = self.fromDatabase(modelName, data); if (data && idName !== '_id') { delete data._id; } @@ -703,11 +703,11 @@ Connector.defineAliases(MongoDB.prototype, 'find', 'findById'); * @param data * @returns {*} */ -MongoDB.prototype.parseUpdateData = function(model, data, options) { +MongoDB.prototype.parseUpdateData = function(modelName, data, options) { options = options || {}; var parsedData = {}; - var modelClass = this._models[model]; + var modelClass = this._models[modelName]; var allowExtendedOperators = this.settings.allowExtendedOperators; if (options.hasOwnProperty('allowExtendedOperators')) { @@ -771,33 +771,33 @@ MongoDB.prototype.parseUpdateData = function(model, data, options) { /** * Update if the model instance exists with the same id or create a new instance * - * @param {String} model The model name + * @param {String} modelName The model name * @param {Object} data The model instance data * @param {Function} [callback] The callback function */ MongoDB.prototype.updateOrCreate = function updateOrCreate( - model, + modelName, data, options, callback ) { var self = this; if (self.debug) { - debug('updateOrCreate', model, data); + debug('updateOrCreate', modelName, data); } - var id = self.getIdValue(model, data); - var idName = self.idName(model); - var oid = self.coerceId(model, id, options); + var id = self.getIdValue(modelName, data); + var idName = self.idName(modelName); + var oid = self.coerceId(modelName, id, options); delete data[idName]; - data = self.toDatabase(model, data); + data = self.toDatabase(modelName, data); // Check for other operators and sanitize the data obj - data = self.parseUpdateData(model, data, options); + data = self.parseUpdateData(modelName, data, options); this.execute( - model, + modelName, 'findOneAndUpdate', { _id: oid, @@ -810,15 +810,15 @@ MongoDB.prototype.updateOrCreate = function updateOrCreate( }, function(err, result) { if (self.debug) { - debug('updateOrCreate.callback', model, id, err, result); + debug('updateOrCreate.callback', modelName, id, err, result); } var object = result && result.value; if (!err && !object) { // No result - err = 'No ' + model + ' found for id ' + id; + err = 'No ' + modelName + ' found for id ' + id; } if (!err) { - self.setIdValue(model, object, oid); + self.setIdValue(modelName, object, oid); if (object && idName !== '_id') { delete object._id; } @@ -841,37 +841,37 @@ MongoDB.prototype.updateOrCreate = function updateOrCreate( /** * Replace model instance if it exists or create a new one if it doesn't * - * @param {String} model The name of the model + * @param {String} modelName The name of the model * @param {Object} data The model instance data * @param {Object} options The options object * @param {Function} [cb] The callback function */ -MongoDB.prototype.replaceOrCreate = function(model, data, options, cb) { - if (this.debug) debug('replaceOrCreate', model, data); +MongoDB.prototype.replaceOrCreate = function(modelName, data, options, cb) { + if (this.debug) debug('replaceOrCreate', modelName, data); - var id = this.getIdValue(model, data); - var oid = this.coerceId(model, id, options); - var idName = this.idName(model); + var id = this.getIdValue(modelName, data); + var oid = this.coerceId(modelName, id, options); + var idName = this.idName(modelName); data._id = data[idName]; delete data[idName]; - this.replaceWithOptions(model, oid, data, {upsert: true}, cb); + this.replaceWithOptions(modelName, oid, data, {upsert: true}, cb); }; /** * Delete a model instance by id - * @param {String} model The model name + * @param {String} modelName The model name * @param {*} id The id value * @param [callback] The callback function */ -MongoDB.prototype.destroy = function destroy(model, id, options, callback) { +MongoDB.prototype.destroy = function destroy(modelName, id, options, callback) { var self = this; if (self.debug) { - debug('delete', model, id); + debug('delete', modelName, id); } - id = self.coerceId(model, id, options); - this.execute(model, 'deleteOne', {_id: id}, function(err, result) { + id = self.coerceId(modelName, id, options); + this.execute(modelName, 'deleteOne', {_id: id}, function(err, result) { if (self.debug) { - debug('delete.callback', model, id, err, result); + debug('delete.callback', modelName, id, err, result); } var res = result && result.result; if (res) { @@ -910,7 +910,7 @@ function idIncluded(fields, idName) { return true; } -MongoDB.prototype.buildWhere = function(model, where, options) { +MongoDB.prototype.buildWhere = function(modelName, where, options) { var self = this; var query = {}; if (where === null || typeof where !== 'object') { @@ -919,13 +919,13 @@ MongoDB.prototype.buildWhere = function(model, where, options) { where = sanitizeFilter(where, options); - var idName = self.idName(model); + var idName = self.idName(modelName); Object.keys(where).forEach(function(k) { var cond = where[k]; if (k === 'and' || k === 'or' || k === 'nor') { if (Array.isArray(cond)) { cond = cond.map(function(c) { - return self.buildWhere(model, c, options); + return self.buildWhere(modelName, c, options); }); } query['$' + k] = cond; @@ -940,7 +940,7 @@ MongoDB.prototype.buildWhere = function(model, where, options) { propName = idName; } - var prop = self.getPropertyDefinition(model, propName); + var prop = self.getPropertyDefinition(modelName, propName); const isDecimal = prop && prop.mongodb && prop.mongodb.dataType && @@ -951,7 +951,7 @@ MongoDB.prototype.buildWhere = function(model, where, options) { } // Convert property to database column name - k = self.getDatabaseColumnName(model, k); + k = self.getDatabaseColumnName(modelName, k); var spec = false; var regexOptions = null; @@ -967,7 +967,7 @@ MongoDB.prototype.buildWhere = function(model, where, options) { cond = [].concat(cond || []); query[k] = { $in: cond.map(function(x) { - if (self.isObjectIDProperty(model, prop, x, options)) + if (self.isObjectIDProperty(modelName, prop, x, options)) return ObjectID(x); return x; }), @@ -976,7 +976,7 @@ MongoDB.prototype.buildWhere = function(model, where, options) { cond = [].concat(cond || []); query[k] = { $nin: cond.map(function(x) { - if (self.isObjectIDProperty(model, prop, x, options)) + if (self.isObjectIDProperty(modelName, prop, x, options)) return ObjectID(x); return x; }), @@ -1010,7 +1010,7 @@ MongoDB.prototype.buildWhere = function(model, where, options) { // Null: 10 query[k] = {$type: 10}; } else { - if (self.isObjectIDProperty(model, prop, cond, options)) { + if (self.isObjectIDProperty(modelName, prop, cond, options)) { cond = ObjectID(cond); } query[k] = cond; @@ -1020,11 +1020,11 @@ MongoDB.prototype.buildWhere = function(model, where, options) { return query; }; -MongoDB.prototype.buildSort = function(model, order, options) { +MongoDB.prototype.buildSort = function(modelName, order, options) { var sort = {}; - var idName = this.idName(model); + var idName = this.idName(modelName); - var modelClass = this._models[model]; + var modelClass = this._models[modelName]; var disableDefaultSort = false; if (this.settings.hasOwnProperty('disableDefaultSort')) { @@ -1038,7 +1038,7 @@ MongoDB.prototype.buildSort = function(model, order, options) { } if (!order && !disableDefaultSort) { - var idNames = this.idNames(model); + var idNames = this.idNames(modelName); if (idNames && idNames.length) { order = idNames; } @@ -1056,7 +1056,7 @@ MongoDB.prototype.buildSort = function(model, order, options) { if (key === idName) { key = '_id'; } else { - key = this.getDatabaseColumnName(model, key); + key = this.getDatabaseColumnName(modelName, key); } if (m && m[1] === 'DE') { @@ -1288,31 +1288,31 @@ MongoDB.prototype.fromDatabaseToPropertyNames = function(model, data) { /** * Find matching model instances by the filter * - * @param {String} model The model name + * @param {String} modelName The model name * @param {Object} filter The filter * @param {Function} [callback] The callback function */ -MongoDB.prototype.all = function all(model, filter, options, callback) { +MongoDB.prototype.all = function all(modelName, filter, options, callback) { var self = this; if (self.debug) { - debug('all', model, filter); + debug('all', modelName, filter); } filter = filter || {}; - var idName = self.idName(model); + var idName = self.idName(modelName); var query = {}; if (filter.where) { - query = self.buildWhere(model, filter.where, options); + query = self.buildWhere(modelName, filter.where, options); } var fields = filter.fields; // Convert custom column names - fields = self.fromPropertyToDatabaseNames(model, fields); + fields = self.fromPropertyToDatabaseNames(modelName, fields); if (fields) { var findOpts = {projection: fieldsArrayToObj(fields)}; - this.execute(model, 'find', query, findOpts, processResponse); + this.execute(modelName, 'find', query, findOpts, processResponse); } else { - this.execute(model, 'find', query, processResponse); + this.execute(modelName, 'find', query, processResponse); } function processResponse(err, cursor) { @@ -1327,7 +1327,7 @@ MongoDB.prototype.all = function all(model, filter, options, callback) { // don't apply sorting if dealing with a geo query if (!hasNearFilter(filter.where)) { - var order = self.buildSort(model, filter.order, options); + var order = self.buildSort(modelName, filter.order, options); cursor.sort(order); } @@ -1345,25 +1345,25 @@ MongoDB.prototype.all = function all(model, filter, options, callback) { cursor.toArray(function(err, data) { if (self.debug) { - debug('all', model, filter, err, data); + debug('all', modelName, filter, err, data); } if (err) { return callback(err); } var objs = data.map(function(o) { if (shouldSetIdValue) { - self.setIdValue(model, o, o._id); + self.setIdValue(modelName, o, o._id); } // Don't pass back _id if the fields is set if (deleteMongoId) { delete o._id; } - o = self.fromDatabase(model, o); + o = self.fromDatabase(modelName, o); return o; }); if (filter && filter.include) { - self._models[model].model.include( + self._models[modelName].model.include( objs, filter.include, options, @@ -1378,31 +1378,31 @@ MongoDB.prototype.all = function all(model, filter, options, callback) { /** * Delete all instances for the given model - * @param {String} model The model name + * @param {String} modelName The model name * @param {Object} [where] The filter for where * @param {Function} [callback] The callback function */ MongoDB.prototype.destroyAll = function destroyAll( - model, + modelName, where, options, callback ) { var self = this; if (self.debug) { - debug('destroyAll', model, where); + debug('destroyAll', modelName, where); } if (!callback && 'function' === typeof where) { callback = where; where = undefined; } - where = self.buildWhere(model, where, options); + where = self.buildWhere(modelName, where, options); if (debug.enabled) debug('destroyAll where %s', util.inspect(where)); - this.execute(model, 'deleteMany', where || {}, function(err, info) { + this.execute(modelName, 'deleteMany', where || {}, function(err, info) { if (err) return callback && callback(err); - if (self.debug) debug('destroyAll.callback', model, where, err, info); + if (self.debug) debug('destroyAll.callback', modelName, where, err, info); var affectedCount = info.result ? info.result.n : undefined; @@ -1415,21 +1415,21 @@ MongoDB.prototype.destroyAll = function destroyAll( /** * Count the number of instances for the given model * - * @param {String} model The model name + * @param {String} modelName The model name * @param {Function} [callback] The callback function * @param {Object} filter The filter for where * */ -MongoDB.prototype.count = function count(model, where, options, callback) { +MongoDB.prototype.count = function count(modelName, where, options, callback) { var self = this; if (self.debug) { - debug('count', model, where); + debug('count', modelName, where); } - where = self.buildWhere(model, where, options) || {}; + where = self.buildWhere(modelName, where, options) || {}; const method = Object.keys(where).length === 0 ? 'estimatedDocumentCount' : 'countDocuments'; - this.execute(model, method, where, function(err, count) { + this.execute(modelName, method, where, function(err, count) { if (self.debug) { - debug('count.callback', model, err, count); + debug('count.callback', modelName, err, count); } if (callback) { callback(err, count); @@ -1439,16 +1439,16 @@ MongoDB.prototype.count = function count(model, where, options, callback) { /** * Replace properties for the model instance data - * @param {String} model The name of the model + * @param {String} modelName The model name * @param {*} id The instance id * @param {Object} data The model data * @param {Object} options The options object * @param {Function} [cb] The callback function */ -MongoDB.prototype.replaceById = function replace(model, id, data, options, cb) { - if (this.debug) debug('replace', model, id, data); - var oid = this.coerceId(model, id, options); - this.replaceWithOptions(model, oid, data, {upsert: false}, function( +MongoDB.prototype.replaceById = function replace(modelName, id, data, options, cb) { + if (this.debug) debug('replace', modelName, id, data); + var oid = this.coerceId(modelName, id, options); + this.replaceWithOptions(modelName, oid, data, {upsert: false}, function( err, data ) { @@ -1465,21 +1465,21 @@ function errorIdNotFoundForReplace(idValue) { /** * Update a model instance with id - * @param {String} model The name of the model + * @param {String} modelName The model name * @param {Object} id The id of the model instance * @param {Object} data The property/value pairs to be updated or inserted if {upsert: true} is passed as options * @param {Object} options The options you want to pass for update, e.g, {upsert: true} * @callback {Function} [cb] Callback function */ -MongoDB.prototype.replaceWithOptions = function(model, id, data, options, cb) { +MongoDB.prototype.replaceWithOptions = function(modelName, id, data, options, cb) { var self = this; - var idName = self.idName(model); + var idName = self.idName(modelName); delete data[idName]; - this.execute(model, 'replaceOne', {_id: id}, data, options, function( + this.execute(modelName, 'replaceOne', {_id: id}, data, options, function( err, info ) { - debug('updateWithOptions.callback', model, {_id: id}, data, err, info); + debug('updateWithOptions.callback', modelName, {_id: id}, data, err, info); if (err) return cb && cb(err); var result; var cbInfo = {}; @@ -1509,12 +1509,12 @@ MongoDB.prototype.replaceWithOptions = function(model, id, data, options, cb) { /** * Update properties for the model instance data - * @param {String} model The model name + * @param {String} modelName The model name * @param {Object} data The model data * @param {Function} [callback] The callback function */ MongoDB.prototype.updateAttributes = function updateAttrs( - model, + modelName, id, data, options, @@ -1522,13 +1522,13 @@ MongoDB.prototype.updateAttributes = function updateAttrs( ) { var self = this; - data = self.toDatabase(model, data || {}); + data = self.toDatabase(modelName, data || {}); // Check for other operators and sanitize the data obj - data = self.parseUpdateData(model, data, options); + data = self.parseUpdateData(modelName, data, options); if (self.debug) { - debug('updateAttributes', model, id, data); + debug('updateAttributes', modelName, id, data); } if (Object.keys(data).length === 0) { @@ -1540,11 +1540,11 @@ MongoDB.prototype.updateAttributes = function updateAttrs( return; } - var oid = self.coerceId(model, id, options); - var idName = this.idName(model); + var oid = self.coerceId(modelName, id, options); + var idName = this.idName(modelName); this.execute( - model, + modelName, 'findOneAndUpdate', { _id: oid, @@ -1555,14 +1555,14 @@ MongoDB.prototype.updateAttributes = function updateAttrs( }, function(err, result) { if (self.debug) { - debug('updateAttributes.callback', model, id, err, result); + debug('updateAttributes.callback', modelName, id, err, result); } var object = result && result.value; if (!err && !object) { // No result - err = errorIdNotFoundForUpdate(model, id); + err = errorIdNotFoundForUpdate(modelName, id); } - self.setIdValue(model, object, id); + self.setIdValue(modelName, object, id); if (object && idName !== '_id') { delete object._id; } @@ -1582,13 +1582,13 @@ function errorIdNotFoundForUpdate(modelvalue, idValue) { /** * Update all matching instances - * @param {String} model The model name + * @param {String} modelName The model name * @param {Object} where The search criteria * @param {Object} data The property/value pairs to be updated * @callback {Function} cb Callback function */ MongoDB.prototype.update = MongoDB.prototype.updateAll = function updateAll( - model, + modelName, where, data, options, @@ -1596,20 +1596,20 @@ MongoDB.prototype.update = MongoDB.prototype.updateAll = function updateAll( ) { var self = this; if (self.debug) { - debug('updateAll', model, where, data); + debug('updateAll', modelName, where, data); } - var idName = this.idName(model); + var idName = this.idName(modelName); - where = self.buildWhere(model, where, options); + where = self.buildWhere(modelName, where, options); delete data[idName]; - data = self.toDatabase(model, data); + data = self.toDatabase(modelName, data); // Check for other operators and sanitize the data obj - data = self.parseUpdateData(model, data, options); + data = self.parseUpdateData(modelName, data, options); this.execute( - model, + modelName, 'updateMany', where, data, @@ -1618,7 +1618,7 @@ MongoDB.prototype.update = MongoDB.prototype.updateAll = function updateAll( if (err) return cb && cb(err); if (self.debug) - debug('updateAll.callback', model, where, data, err, info); + debug('updateAll.callback', modelName, where, data, err, info); var affectedCount = info.result ? info.result.n : undefined; @@ -1674,8 +1674,8 @@ MongoDB.prototype.autoupdate = function(models, cb) { async.each( models, - function(model, modelCallback) { - var indexes = self._models[model].settings.indexes || []; + function(modelName, modelCallback) { + var indexes = self._models[modelName].settings.indexes || []; var indexList = []; var index = {}; var options = {}; @@ -1700,7 +1700,7 @@ MongoDB.prototype.autoupdate = function(models, cb) { } else if (Array.isArray(indexes)) { indexList = indexList.concat(indexes); } - var properties = self._models[model].properties; + var properties = self._models[modelName].properties; /* eslint-disable one-var */ for (var p in properties) { if (properties[p].index) { @@ -1763,7 +1763,7 @@ MongoDB.prototype.autoupdate = function(models, cb) { debug('createIndex: ', index); } self - .collection(model) + .collection(modelName) .createIndex( index.fields || index.keys, index.options, @@ -1808,10 +1808,10 @@ MongoDB.prototype.automigrate = function(models, cb) { // Make it serial as multiple models might map to the same collection async.eachSeries( models, - function(model, modelCallback) { - var collectionName = self.collectionName(model); + function(modelName, modelCallback) { + var collectionName = self.collectionName(modelName); if (self.debug) { - debug('drop collection %s for model %s', collectionName, model); + debug('drop collection %s for model %s', collectionName, modelName); } self.db.dropCollection(collectionName, function(err, collection) { @@ -1819,7 +1819,7 @@ MongoDB.prototype.automigrate = function(models, cb) { debug( 'Error dropping collection %s for model %s: ', collectionName, - model, + modelName, err ); if ( @@ -1835,7 +1835,7 @@ MongoDB.prototype.automigrate = function(models, cb) { } // Recreate the collection if (self.debug) { - debug('create collection %s for model %s', collectionName, model); + debug('create collection %s for model %s', collectionName, modelName); } self.db.createCollection(collectionName, modelCallback); }); @@ -1873,7 +1873,7 @@ MongoDB.prototype.ping = function(cb) { * Check whether the property is an ObjectID (or Array thereof) * */ -MongoDB.prototype.isObjectIDProperty = function(model, prop, value, options) { +MongoDB.prototype.isObjectIDProperty = function(modelName, prop, value, options) { if ( prop && (prop.type === ObjectID || @@ -1881,7 +1881,7 @@ MongoDB.prototype.isObjectIDProperty = function(model, prop, value, options) { ) { return true; } else if ('string' === typeof value) { - var settings = this._models[model] && this._models[model].settings; + var settings = this._models[modelName] && this._models[modelName].settings; options = options || {}; var strict = (settings && settings.strictObjectIDCoercion) || @@ -1916,26 +1916,26 @@ exports.sanitizeFilter = sanitizeFilter; * * Only supported on mongodb 2.6+ * - * @param {String} model The model name + * @param {String} modelName The model name * @param {Object} data The model instance data * @param {Object} filter The filter * @param {Function} [callback] The callback function */ -function optimizedFindOrCreate(model, filter, data, options, callback) { +function optimizedFindOrCreate(modelName, filter, data, options, callback) { var self = this; if (self.debug) { - debug('findOrCreate', model, filter, data); + debug('findOrCreate', modelName, filter, data); } if (!callback) callback = options; - var idValue = self.getIdValue(model, data); - var idName = self.idName(model); + var idValue = self.getIdValue(modelName, data); + var idName = self.idName(modelName); if (idValue == null) { delete data[idName]; // Allow MongoDB to generate the id } else { - var oid = self.coerceId(model, idValue, options); // Is it an Object ID? + var oid = self.coerceId(modelName, idValue, options); // Is it an Object ID? data._id = oid; // Set it to _id if (idName !== '_id') { delete data[idName]; @@ -1948,23 +1948,23 @@ function optimizedFindOrCreate(model, filter, data, options, callback) { if (filter.where[idName]) { var id = filter.where[idName]; delete filter.where[idName]; - id = self.coerceId(model, id, options); + id = self.coerceId(modelName, id, options); filter.where._id = id; } - query = self.buildWhere(model, filter.where, options); + query = self.buildWhere(modelName, filter.where, options); } - var sort = self.buildSort(model, filter.order, options); + var sort = self.buildSort(modelName, filter.order, options); var projection = fieldsArrayToObj(filter.fields); - this.collection(model).findOneAndUpdate( + this.collection(modelName).findOneAndUpdate( query, {$setOnInsert: data}, {projection: projection, sort: sort, upsert: true}, function(err, result) { if (self.debug) { - debug('findOrCreate.callback', model, filter, err, result); + debug('findOrCreate.callback', modelName, filter, err, result); } if (err) { return callback(err); @@ -1975,10 +1975,10 @@ function optimizedFindOrCreate(model, filter, data, options, callback) { if (created && (value == null || Object.keys(value).length == 0)) { value = data; - self.setIdValue(model, value, result.lastErrorObject.upserted); + self.setIdValue(modelName, value, result.lastErrorObject.upserted); } else { - value = self.fromDatabase(model, value); - self.setIdValue(model, value, value._id); + value = self.fromDatabase(modelName, value); + self.setIdValue(modelName, value, value._id); } if (value && idName !== '_id') { @@ -1986,7 +1986,7 @@ function optimizedFindOrCreate(model, filter, data, options, callback) { } if (filter && filter.include) { - self._models[model].model.include([value], filter.include, function( + self._models[modelName].model.include([value], filter.include, function( err, data ) { diff --git a/test/objectid.test.js b/test/objectid.test.js index a1f86bcf1..67807a3fd 100644 --- a/test/objectid.test.js +++ b/test/objectid.test.js @@ -7,21 +7,21 @@ require('./init.js'); -var db, Book, Chapter; +var ds, Book, Chapter; describe('ObjectID', function() { before(function() { - db = global.getDataSource(); - Book = db.define('Book'); - Chapter = db.define('Chapter'); + ds = global.getDataSource(); + Book = ds.define('Book'); + Chapter = ds.define('Chapter'); Book.hasMany('chapters'); Chapter.belongsTo('book'); }); it('should cast foreign keys as ObjectID', function(done) { Chapter.beforeCreate = function(next, data) { - data.bookId.should.be.an.instanceOf(db.ObjectID); - this.bookId.should.be.an.instanceOf(db.ObjectID); + data.bookId.should.be.an.instanceOf(ds.ObjectID); + this.bookId.should.be.an.instanceOf(ds.ObjectID); next(); }; @@ -32,36 +32,36 @@ describe('ObjectID', function() { }); it('should convert 24 byte hex string as ObjectID', function() { - var ObjectID = db.connector.getDefaultIdType(); + var ObjectID = ds.connector.getDefaultIdType(); var str = '52fcef5c0325ace8dcb7a0bd'; - ObjectID(str).should.be.an.instanceOf(db.ObjectID); + ObjectID(str).should.be.an.instanceOf(ds.ObjectID); }); it('should not convert 12 byte string as ObjectID', function() { - var ObjectID = db.connector.getDefaultIdType(); + var ObjectID = ds.connector.getDefaultIdType(); var str = 'line-by-line'; ObjectID(str).should.be.equal(str); }); it('should keep mongodb ObjectID as is', function() { - var ObjectID = db.connector.getDefaultIdType(); - var id = new db.ObjectID(); - ObjectID(id).should.be.an.instanceOf(db.ObjectID); + var ObjectID = ds.connector.getDefaultIdType(); + var id = new ds.ObjectID(); + ObjectID(id).should.be.an.instanceOf(ds.ObjectID); }); it('should keep non-string id as it', function() { - var ObjectID = db.connector.getDefaultIdType(); + var ObjectID = ds.connector.getDefaultIdType(); var id = 123; ObjectID(id).should.be.equal(123); }); it('coerces ObjectID', function() { - const coercedId = db.connector.isObjectIDProperty('Book', {}, '52fcef5c0325ace8dcb7a0bd'); + const coercedId = ds.connector.isObjectIDProperty('Book', {}, '52fcef5c0325ace8dcb7a0bd'); coercedId.should.be.True(); }); it('given strictObjectIDCoercion: true, does not coerce ObjectID', function() { - const coercedId = db.connector.isObjectIDProperty( + const coercedId = ds.connector.isObjectIDProperty( 'Book', {}, '52fcef5c0325ace8dcb7a0bd',