Giter VIP home page Giter VIP logo

Comments (7)

dhmlau avatar dhmlau commented on June 23, 2024

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

from loopback-connector-cloudant.

jannyHou avatar jannyHou commented on June 23, 2024

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?

from loopback-connector-cloudant.

dhmlau avatar dhmlau commented on June 23, 2024

@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

from loopback-connector-cloudant.

jannyHou avatar jannyHou commented on June 23, 2024
  • 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.

from loopback-connector-cloudant.

jannyHou avatar jannyHou commented on June 23, 2024

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.

from loopback-connector-cloudant.

b-admike avatar b-admike commented on June 23, 2024

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!

from loopback-connector-cloudant.

b-admike avatar b-admike commented on June 23, 2024

connect to https://github.com/strongloop-internal/scrum-apex/issues/240

from loopback-connector-cloudant.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.