-
Notifications
You must be signed in to change notification settings - Fork 239
Implement replaceOrCreate and replaceAttributes #202
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -493,6 +493,25 @@ MongoDB.prototype.updateOrCreate = function updateOrCreate(model, data, options, | |
| }); | ||
| }; | ||
|
|
||
| /** | ||
| * Replace model instance if it exists or create a new one if it doesn't | ||
| * | ||
| * @param {String} model 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); | ||
|
|
||
| var id = this.getIdValue(model, data); | ||
| var oid = this.coerceId(model, id); | ||
| var idName = this.idName(model); | ||
| data._id = data[idName]; | ||
| delete data[idName]; | ||
| this.replaceWithOptions(model, oid, data, {upsert: true}, cb); | ||
| }; | ||
|
|
||
| /** | ||
| * Delete a model instance by id | ||
| * @param {String} model The model name | ||
|
|
@@ -812,6 +831,59 @@ 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 {*} 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); | ||
| this.replaceWithOptions(model, oid, data, {upsert: false}, function(err, data) { | ||
| cb(err, data); | ||
| }); | ||
| }; | ||
|
|
||
| /** | ||
| * Update a model instance with id | ||
| * @param {String} model The name of the model | ||
| * @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) { | ||
| var self = this; | ||
| this.execute(model, 'update', {_id: id}, data, options, function(err, info) { | ||
| if (this.debug) debug('updateWithOptions.callback', model, {_id: id}, data, err, info); | ||
| if (err) return cb && cb(err); | ||
| var result; | ||
| var cbInfo = {}; | ||
| if (info.result && info.result.n == 1) { | ||
| result = data; | ||
| delete result._id; | ||
| var idName = self.idName(model); | ||
| result[idName] = id; | ||
| // create result formats: | ||
| // 2.4.x :{ ok: 1, n: 1, upserted: [ Object ] } | ||
| // { ok: 1, nModified: 0, n: 1, upserted: [ Object ] } | ||
| // | ||
| // replace result formats: | ||
| // 2.4.x: { ok: 1, n: 1 } | ||
| // { ok: 1, nModified: 1, n: 1 } | ||
| if (info.result.nModified !== undefined) { | ||
| cbInfo.isNewInstance = info.result.nModified === 0; | ||
| } | ||
| } else { | ||
| result = undefined; | ||
| } | ||
| cb && cb(err, result, cbInfo); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @raymondfeng: I had forgot to add this |
||
| }); | ||
| }; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The implementation of |
||
|
|
||
| /** | ||
| * Update properties for the model instance data | ||
| * @param {String} model The model name | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,5 +14,16 @@ if (process.env.MONGODB_VERSION && | |
| for(var i in config) { | ||
| customConfig[i] = config[i]; | ||
| } | ||
| var DB_VERSION = process.env.MONGODB_VERSION; | ||
|
|
||
| suite(global.getDataSource(customConfig), should); | ||
| if (!DB_VERSION) { | ||
| console.log('The ENV variable MONGODB_VERSION is not set.' | ||
| + ' Assuming MongoDB version 2.6 or newer.'); | ||
| } | ||
|
|
||
| var DB_HAS_2_6_FEATURES = (!DB_VERSION || | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @bajtos: Please note: if you do
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you please add a For example: if (!DB_VERSION) {
console.log('The ENV variable MONGODB_VERSION is not set. Assuming MongoDB version 2.6 or newer.');
} |
||
| semver.satisfies(DB_VERSION, '>=2.6.0')); | ||
|
|
||
| suite(global.getDataSource(customConfig), should, { | ||
| replaceOrCreateReportsNewInstance: DB_HAS_2_6_FEATURES | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
model -> modelName is more descriptive
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey Simon, I applied all of your feedback except changing
modeltomodelNamein a few places you mentioned; this change makes the code inconsistent with the rest of the code; in other words, in other methods others are usingmodelas well and refactoring the other code written by others seems huge changes...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good. The rename can be done in a separate commit/PR.