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

Support to query view #67

Closed
1 task
zhyinjun opened this issue Dec 12, 2016 · 7 comments
Closed
1 task

Support to query view #67

zhyinjun opened this issue Dec 12, 2016 · 7 comments
Assignees
Labels

Comments

@zhyinjun
Copy link

Bug or feature request

  • Bug
  • [x ] Feature request

Description of feature (or steps to reproduce if bug)

Couch connector supports to query the view, will cloudant connector support this feature?

https://www.npmjs.com/package/loopback-connector-couch

Link to sample repo to reproduce issue (if bug)

Expected result

Actual result (if bug)

Additional information (Node.js version, LoopBack version, etc)

@dhmlau
Copy link
Member

dhmlau commented Apr 5, 2017

@raymondfeng, any thought on supporting query on view (or search indexes) for Cloudant connector?

@dhmlau dhmlau self-assigned this Apr 5, 2017
@jannyHou
Copy link
Contributor

query view would be a cloudant specific function, it will be good if we can define it in cloudant connector, like:

// in `lib/cloudant.js`
Cloudant.prototype.queryView() {
 // code
}

then in juggler lib/datasource.js, we can mix certain connector specific functions to Model, like how we mix DAO functions to model in https://github.com/strongloop/loopback-datasource-juggler/blob/master/lib/datasource.js#L585

This would also be useful across all connectors.
@raymondfeng Any thought?

@dhmlau
Copy link
Member

dhmlau commented Apr 25, 2017

@jannyHou @raymondfeng , I think it's a good idea to have a consistent story in supporting views across connectors because they are not specific to Cloudant.

Similar question in mssql: loopbackio/loopback-connector-mssql#26

@jannyHou
Copy link
Contributor

jannyHou commented May 8, 2017

  • option1: db.view(designname, viewname, [params], [callback])

    • This is the main function exported by nano to query view.
    • If our goal is to support it, we can define a method Cloudant.prototype.view(viewname, [params], cb), designname can be read from loopback model name, viewname and params are inputs.
    • endpoint: GET/Model/view
  • option2: couchdb view(map and reduce)

    • If we want to support user define map and reduce functions, they should be provided when creating the model.
    • GET/Model/view/ get all docs queried by a view
    • PUT/Model/view/ update a view(an entry in views) in a design doc
  • option3: Another thought is, make it a datasource level api, instead of a model level, like automigrate, and user can provide any designname, doesn't have to map a loopback model name.
    Now we fix ddoc name to be 'lb-index-ddoc-' + modelName, user cannot change it, which means if they already have some data in cloudant database, using their own ddoc name, and includes views in the design doc, they could not map it to our model.
    I have considered supporting discover views then make view a loopback model, but seems couchdb doesn't have an api to return all views, it's only able to return all indexes.

@jannyHou jannyHou self-assigned this May 10, 2017
@jannyHou
Copy link
Contributor

jannyHou commented May 12, 2017

Turns out the view could not be a model level api with current design, explained in #15 (comment)

Other plans:

  • strategy1: Support those design functions like view as datasource level functions, so a view don't need to be created in a lb-model's design doc.
  • srategy2: Use two design docs(one for index one for view) to represent a loopback model.

@jannyHou jannyHou mentioned this issue May 12, 2017
2 tasks
@kjdelisle kjdelisle added this to the Sprint 36 - Apex milestone May 19, 2017
@b-admike b-admike self-assigned this May 31, 2017
@b-admike
Copy link
Contributor

Steps taken to verify:

  • Create a DDoc on cloudant db called TestViewDDoc with a view hadtraining
{
  "_id": "_design/TestViewDDoc",
  "_rev": "13-2738c014de2d5b6cfc02522d1bdc45fc",
  "views": {
    "hadtraining": {
      "map": "function(doc) {\n    if (doc.training) {\n        emit(doc.number, doc.training);\n    }\n}"
    }
  }
}
  • Create some docs which satisfy the view criteria
  • Add boot script to a test LB app which is connected to the same cloudant db and queries for the view:
module.exports = function(app) {
  var ds = app.dataSources.cldntlocal;
  ds.on('connected', function() {
    // 1. Please note `ds.connector.viewDocs()` is the correct way to call it,
    // NOT `ds.viewDocs()`
    // 2. This api matches the Cloudant endpoint:
    // GET /db/_design/<design-doc>/_view/<view-name>
    ds.connector.viewDocs('TestViewDDoc', 'hadtraining', function(err, results, rows) {
      // `results` would be the data returned by quering that view
      console.log('view total rows ', results.total_rows);
      console.log('view results ', results.rows);
    });
        // Alternatively user can also specify the filter for view query
    ds.connector.viewDocs('TestViewDDoc', 'hadtraining', {key: 5},
      function(err, results) {
        console.log('view by filter total rows ', results.total_rows);
        console.log('view by filter results ', results.rows);
      });
  });
};

Results:

| => node .
Web server listening at: http://0.0.0.0:3000
Browse your REST API at http://0.0.0.0:3000/explorer
view by filter total rows  4
view by filter results  [ { id: '6d8d81bff8f54ccd35f499bc3e0799e8',
    key: 5,
    value: '10/02/2012' } ]
view total rows  4
view results  [ { id: '6d8d81bff8f54ccd35f499bc3e03fe6c',
    key: 1,
    value: '12/20/2011' },
  { id: '6d8d81bff8f54ccd35f499bc3e0410f4',
    key: 1,
    value: '12/20/2011' },
  { id: '6d8d81bff8f54ccd35f499bc3e079366',
    key: 3,
    value: '12/05/2013' },
  { id: '6d8d81bff8f54ccd35f499bc3e0799e8',
    key: 5,
    value: '10/02/2012' } ]
^C

Thanks @jannyHou for your help!

@b-admike
Copy link
Contributor

b-admike commented Jun 2, 2017

@b-admike b-admike closed this as completed Jun 2, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants