-
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
viewDocs #133
viewDocs #133
Conversation
fe41fa6
to
0ad4a2d
Compare
CI passed(1st commit), now it's running due to a jsdoc change. |
lib/view.js
Outdated
* @param {String} ddocName The design doc name | ||
* @param {String} viewName The view name | ||
* @param {Object} options The cloudant view filter | ||
* @param {Function} [cb] The callback function |
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.
Do we need to follow the @callback
format here for JSDocs?
lib/view.js
Outdated
* @param {Function} [cb] The callback function | ||
*/ | ||
Cloudant.prototype.viewDocs = function(ddocName, viewName, options, cb) { | ||
// ds.viewDocs(ddocName, viewName, cb); |
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.
intentional comment?
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.
yep :) the following two lines are written to make inputs compatible with the scenario in that comment.
lib/view.js
Outdated
var parsedUrl = URL.parse(url); | ||
if (parsedUrl.path && parsedUrl.path !== '/') | ||
return parsedUrl.path.split('/')[1]; | ||
else return ''; |
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.
Don't need an else
statement here. Just return.
lib/view.js
Outdated
@@ -0,0 +1,56 @@ | |||
// Copyright IBM Corp. 2015,2016. All Rights Reserved. |
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.
Should just be 2017
, not 2015,2016
.
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.
Does the copyright tool make those changes for us, now? We can always run a separate PR to update it.
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.
It does, yes. The tool could also just be run on this branch and fixed as part of this PR ;-)
lib/view.js
Outdated
/** | ||
* Get data returned by querying a view | ||
* | ||
* @param {String} ddocName The design doc name |
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.
What form is the ddocName
expected to be in?
/{db}/_design/{name}
_design/{name}
(db is implied){name}
(db and_design/
is implied)
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.
It is the third one, {name} (db and _design/ is implied)
, and this is also what the driver function mo.db.viewDocs()
expects.
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.
+1
The JSDoc should explain which format is expected.
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.
Fixing the JSDoc comment to answer my question was what I intended.
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.
/**
* Gets data at `/{db}/_design/{ddocName}/views/{viewName}`
* @param {string} ddocName The name of the design document.
* @param {string} viewName The name of the view function attached to the design document.
* //etc
*/
test/cloudant.view.test.js
Outdated
"_id": "_design/getModel", | ||
"views": { | ||
"returnModelInstances": { | ||
"map": "function(doc) { if(doc.loopback__model__name) { emit(doc.loopback__model__name, doc); }}" |
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 might find Function.prototype.toString() helpful here ;-)
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 actually prefer it, to be honest. We should make that a default expectation so that ESLint can protect us.
test/cloudant.view.test.js
Outdated
done(err); | ||
|
||
function isLBModelInstance(elem) { | ||
elem.value.hasOwnProperty('loopback__model__name'). |
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.
If the intended use-case for this feature is existing databases with existing design docs, I would advise against using property names like loopback__model__name
for a couple reasons:
- it might make code readers/reviewers think this is a field added by LoopBack
- it might make current/future maintainers think this is an official property name
Adding a property to every document in an existing CouchDB database is a very expensive operation. So expensive it might even be considered impossible by some operators. The only reasonable approach would be to have the user provide the map function and/or property name already being used for this purpose. If that isn't considered upfront, it could lead to assumptions and design decisions that will need to be undone later and may require future breaking 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.
To be more specific, I would use something like test_model_name
or similar for the property name so that it is more clear that it is not an official/magic string.
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.
When I wrote the test I didn't think into that deep, I was just thinking of a way to simply insert data (LBModel.create()
), it doesn't imply "Adding a property to every document in an existing CouchDB database".
The view could be used for non-loopback data(already exist in database), and also could be used to improve LB model's performance.
Your two concerns are reasonable to me, I will change the field to some name that not misleading.
lib/view.js
Outdated
* @param {Function} cb | ||
*/ | ||
Cloudant.prototype.viewDocs = function(ddocName, viewName, options, cb) { | ||
// ds.viewDocs(ddocName, viewName, cb); |
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.
Remove commented code.
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.
@kjdelisle ah, I rephrased the comments, it was confusing.
lib/view.js
Outdated
/** | ||
* Get data returned by querying a view | ||
* | ||
* @param {String} ddocName The design doc name |
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.
/**
* Gets data at `/{db}/_design/{ddocName}/views/{viewName}`
* @param {string} ddocName The name of the design document.
* @param {string} viewName The name of the view function attached to the design document.
* //etc
*/
test/cloudant.view.test.js
Outdated
"_id": "_design/getModel", | ||
"views": { | ||
"returnModelInstances": { | ||
"map": "function(doc) { if(doc.loopback__model__name) { emit(doc.loopback__model__name, doc); }}" |
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 actually prefer it, to be honest. We should make that a default expectation so that ESLint can protect us.
test/cloudant.view.test.js
Outdated
var _ = require('lodash'); | ||
var url = require('url'); | ||
var should = require('should'); | ||
var async = require('async'); |
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.
Sort library imports, please. :)
1bd80a1
to
1d1507f
Compare
@kjdelisle @rmg Changes applied, PTAL again. Thanks. |
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.
Couple more small tweaks and questions.
lib/view.js
Outdated
* ds.viewDocs(model, getModel, {key: 'purchase'}, cb); | ||
* ``` | ||
* | ||
* @param {String} ddocName The design doc name |
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 add ".. without {db}/_design/ prefix" (assuming that is correct).
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.
@rmg I am confused why a user would think of using part of the url as a design doc name? By reading couchdb/cloudant document, or those nodejs drivers' README.md, I don't see anywhere implies that the design document name would contain the those prefix....
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.
As a long time CouchDB user, I am confused by whether I am expected to provide the whole name or not. Since the actual Couch/Cloudant document name is actually _design/<name>
(with the /
requiring URL encoding as %2F
), it makes sense to me to pass in the full name - unless I have written my own wrapper which adds it.
It is a case where knowing more means things are harder rather than easier.
test/cloudant.view.test.js
Outdated
var samples = [ | ||
{ | ||
model: 'purchase', | ||
'customer_id': '1', |
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.
The customer_id
key shouldn't need quoting. Does eslint
think it does?
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.
nope :p changed it to Number
.
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.
Actually I meant the key it self.
lib/view.js
Outdated
|
||
var self = this; | ||
var db = this.cloudant.use(self.getDbName(self)); | ||
assert(db && typeof db.view === 'function', |
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.
This isn't checking the design doc or the datasource. this.cloudant.use
is just returning a scoped object so that methods on it use URLs based on the db name. If db.view
is ever not a function then it would mean that someone has changed the implementation of this connector to no longer use cloudant
/cloudant-nano
/nano
as the client.
Is that the purpose of this assertion?
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 thought if the dynamically loaded database doesn't exist, then this.cloudant.use('dbName')
would return undefined, but I just checked it throws error saying db doesn't exist.
Ok I will remove that assert.
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.
That sort of dynamic check would require network I/O, which would require .use()
to be async and take a callback ;-)
Thanks @rmg I applied the other two comments, and explained my confusion of the "design doc name" in #133 (comment), I am ok to add the prefix explanation in the description if you insist, am just curious why it's that important, even after having an example usage :) |
Paraphrasing my reply from the line-comment since I think it is important enough to not hide: Yes, this is a little weird, and yes, it is sort of like having a file on disk with |
@rmg Thank you for elaborating the reason, by
you are right 👍 |
test/cloudant.view.test.js
Outdated
var samples = [ | ||
{ | ||
model: 'purchase', | ||
'customer_id': 1, |
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 samples = [
{
model: 'purchase',
customer_id: 1, // <------ customer_id, not 'customer_id'
basket: ['food', 'drink'],
},
};
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.
One minor nit, but it's not super important. You do have to rebase, though, so you may as well fix it before doing that.
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.
LGTM
README.md
Outdated
// NOT `ds.viewDocs()` | ||
// 2. This api matches the Cloudant endpoint: | ||
// GET /db/_design/<design-doc>/_view/<view-name> | ||
ds.connector.viewDocs('design_doc', 'view_name', cb); |
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.
maybe expand cb
to be inline to show what we expect to see in the callback function like function (err, views)
?
@b-admike Changes applied. PTAL again, thanks. |
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.
LGTM. Nice way to write the tests!
8c1a5e2
to
fd0ccc3
Compare
* Inherit couchdb functionalities (#163) (Sakib Hasan) * Add stalebot configuration (Kevin Delisle) * Recover & reuse couchdb2 tests (jannyHou) * Fix readme (ssh24) * Create Issue and PR Templates (#171) (Sakib Hasan) * Add CODEOWNER file (Diana Lau) * Recover manipulation.test.js (#159) (Janny) * Recover juggler tests (#158) (Janny) * Require init on mocha args (ssh24) * Do not strip _rev value on create (ssh24) * Fix docs on bulk replace op hooks (ssh24) * Fix update/updateAll function (ssh24) * Add cloudant specific bulkReplace function (ssh24) * Check error and result (#149) (Janny) * Fix updateAttributes function (ssh24) * Fix doc (#148) (Janny) * viewDocs (#133) (Janny) * Return back result count in updateAll (ssh24) * Fix database name typo on README (ssh24) * Add regexp doc (#143) (Janny) * Add proxy config test (#142) (Janny) * Allow users to spawn docker and run tests (ssh24) * test: use Cloudant 2.x based image for testing (Ryan Graham) * test: replace setup.sh with test.js (Ryan Graham) * Refactor functions in cloudant (ssh24) * Allow handling of ._rev on models (#123) (Kevin Delisle) * Allow travis to run against the latest code base (#138) (Sakib Hasan) * Add docker setup (#132) (Sakib Hasan) * Fix updateOrCreate (#136) (Sakib Hasan) * Fix typo (#135) (Janny) * cloudant.test: cleanup after test runs (Kevin Delisle) * Setup Travis with Docker Compose (Kevin Delisle) * Refactor doc (#116) (Janny) * reinstate bulk update (biniam) * add array prop update tests (biniam) * update docs with current revision (biniam) * Allow id property to be a number (#115) (Sakib Hasan) * autoupdate and automigrate fix (#109) (Janny) * update readme to doc async connect (biniam) * check cloudant db in config (biniam) * call driver asynchronously (biniam) * Fix sort query builder (#107) (Janny) * Recover maxrows.test.js (#91) (Janny) * Fix regexp.test.js (#103) (Janny) * Use define function in loopback-connector (jannyHou) * add url config example (biniam) * Update connector to 4.0.0 (ssh24) * Add doc for fitler and order (jannyHou) * Add advisory note regarding update (ssh24) * Add $elemMatch for array (jannyHou) * Revert "Build selector with array type data" (jannyHou)
Description
connect to #67
viewDocs
which does view queryPlease note: