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

Cloudant example #48

Open
fredguth opened this issue Jul 31, 2022 · 4 comments
Open

Cloudant example #48

fredguth opened this issue Jul 31, 2022 · 4 comments

Comments

@fredguth
Copy link

It works with couch but I couldn't make it work with cloudant. Is there an example?

@fynnlyte
Copy link
Collaborator

Regarding your questions in #45:

A side question: what is a serverScope? I am using Cloudant and I login as admin using IAM env variables.

It's the object returned by @cloudant/cloudant when instantiating by calling with your config. Not the new IBM cloud SDK.

This package previously used @cloudant/cloudant as dependency in order to be compatible with Cloudant both with IAM and legacy authentication when setting up the _users database as described here, but I migrated to nano when the support for @cloudant/cloudant was discontinued.

I'm not sure whether Cloudant can still be used that way, but since it's in the docs it should still be possible unless you're on transaction engine. I migrated to self hosted Couch DB a while ago. If that way of using cloudant is still supported, it should work by:

If you'd want to use the new SDK with couch-auth, you'd need to provide a wrapper an object that does .request and other used DB operations the way nano does it.

It works with couch but I couldn't make it work with cloudant. Is there an example?

I don't have anything available right now.

@fredguth
Copy link
Author

This was very helpful, thanks. I am not in a transaction engine AFAIK. I love to use PouchDB and CouchDB, but I just hate devops. This is the reason I was looking for Cloudant (The only "CouchDB" as a service I found).

Here is what I tried:

if (!process.env.CLOUDANT_URL || !process.env.CLOUDANT_APIKEY) {
  console.error(
    "Please create CLOUDANT_URL & CLOUDANT_APIKEY environment variables before running. See README for details."
  );
  process.exit(1);
}

const express = require("express");
const bodyParser = require("body-parser");
const { CloudantV1 } = require("@ibm-cloud/cloudant");
const client = CloudantV1.newInstance();
const { CouchAuth } = require("@perfood/couch-auth");
var config = {
  dbServer: {
    protocol: "https://",
    host: process.env.CLOUDANT_HOST,
    iamApiKey: process.env.CLOUDANT_APIKEY,
    couchAuthOnCloudant: true,
    user: process.env.CLOUDANT_USERNAME,
  },
  testMode: {
    noEmail: true,
  },
};
// constants
const PORT = 8080; // the default for Code Engine
const HOST = "0.0.0.0"; // listen on all network interfaces
const app = express();
const auth = new CouchAuth(config);

// middlewares
app.use(express.static("public"));
app.use(bodyParser.json());
app.use("/auth", auth.router);

app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);

client.getServerInformation().then((serverInformation) => {
  console.log(`Cloudant:${JSON.stringify(serverInformation)}`);
});

There is an error in nano.

@fynnlyte
Copy link
Collaborator

fynnlyte commented Aug 1, 2022

Does it work like this?:

const { CouchAuth } = require("@perfood/couch-auth");
const Cloudant = require('@cloudant/cloudant')
const serverScope = Cloudant({
      url: `https://${process.env.CLOUDANT_USER}:${process.env.CLOUDANT_PASS}@${process.env.CLOUDANT_USER}.cloudantnosqldb.appdomain.cloud`,
      plugins: [
        { iamauth: { iamApiKey: process.env.CLOUDANT_APIKEY } },
        { retry: { retryInitialDelayMsecs: 750 } }
      ],
      maxAttempt: 2
    });
const auth = new CouchAuth(config, serverScope);

@fredguth
Copy link
Author

fredguth commented Aug 3, 2022

I will test this as soon as Cloudant let me in my own account. Jeez. Maybe I shouldn't be using CLoudant afterall.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants