-
Notifications
You must be signed in to change notification settings - Fork 21
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
replaceOrCreate should create new instance if id provided #54
Conversation
e73e738
to
c49fa79
Compare
The change comparison looks messy.. self.count({condition_to_check_instance_exist}, function(err, cnt) {
dataExist = cnt === 1 ? true : false;
if (id && dataExist) {
// Do Replace, make sure getRevision won't run on an nonexistent instance
} else {
// Do Create
}
}) |
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.
@jannyHou Reviewed. Also please squash all your commits as one single commit.
verifyCreatedData(product); | ||
}); | ||
|
||
function verifyCreatedData(data) { |
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.
I would chose a better function name; how about verifyReturnedCallbackData
?
data.price.should.be.equal(100); | ||
|
||
// Verify DB data | ||
verifyDBData(data.id); |
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.
I believe the method name is/should be descriptive enough so the comment is unnecessary in this case...
verifyDBData(data.id); | ||
}; | ||
|
||
function verifyDBData(id) { |
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.
Nitpick: method name is good but I may still choose verifyPersistedData
... I leave it to you...Please feel free to ignore this suggestion...
@@ -68,6 +68,36 @@ describe('cloudant connector', function() { | |||
}); | |||
}; | |||
}); | |||
it('should create new instance if id provided', function(done) { |
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.
Feel free to ignore since in this file other tests use the same pattern; however, in general we use imperative mood, we do not start test names with should. Please see: https://github.com/strongloop/loopback.io/blob/gh-pages/pages/en/contrib/style-guide.md#test-naming
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.
You may use 'Primary key' instead of id
; also you could add a test when the Primary key is not called id
as well...
if (err) return cb(err); | ||
dataExist = cnt === 1 ? true : false; | ||
|
||
if (id && dataExist === true) { |
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.
How about:
if (id && dataExist) {
...
}
cb(err, self.fromDB(model, mo, doc), {isNewInstance: !rev}); | ||
mo.db.get(id, function(err, doc) { | ||
if (err) return cb(err); | ||
cb(err, self.fromDB(model, mo, doc), {isNewInstance: !rev}); |
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.
I think in this branch rev
always exists and !rev
would always be false
, right? In this case wouldn't it be more clear if you pass {isNewInstance: false}
instead? Is my understanding right here? Correct me if I'm wrong.
self._insert(model, data, function(err) { | ||
|
||
// Check if data exist first to avoid | ||
// running getCurrentRevision() again an nonexistent instance |
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.
Check if data exists to avoid running 'getCurrentRevision()' against new data.
// Check if data exist first to avoid | ||
// running getCurrentRevision() again an nonexistent instance | ||
var where = {}; where[idName] = id; | ||
var dataExist; |
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.
var where = {};
where[idName] = id;
var dataExist;
}); | ||
} | ||
}; | ||
}); |
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.
I'm not that much familiar with cloudant; is this the only way to do it in cloudant? Isn't there any atomic method to use? I trust your judgment in this regard...
If I understand this change correctly, then each When I step back a bit and look at the problem from a high level perspective, then I think it should be acceptable to require callers of patch/replace operations to provide Thoughts? |
A block of this idea is, user cannot get My suggestion is adding a config option
Since On my second thought, I think
would not be a problem in cloudant, see cloudant doc for transactions. |
Closing this pr since we agree on #55 |
Connect to https://github.com/strongloop-internal/scrum-loopback#991
Scenario:
Without fix in this pr
will fail if the instance doesn't exist in database.
The current code doesn't check instance exist before do the
getRevision()
, which cannot run against an nonexistent instance.Details see: https://github.com/strongloop-internal/scrum-loopback/issues/991#issuecomment-256077756