0

I want to use CouchDB as database-backend in a NodeJS app with Typescript. CouchDb-Nano is used for this, since it provides the required Typings. So I installed both packages:

  "devDependencies": {
    "@types/nano": "^6.4.5"
  },
  "dependencies": {
    "nano": "^6.4.3"
  }

I found this question for the correct TS import syntax. It doesn't work for me. By playing around, I found the following compiling:

import Nano from "nano";
let nano = Nano("http://localhost:5984");

But my intellisens in VS code seems totally different. For example, the docs say that nano has a attribute called db which several methods like this for selecting a database:

var alice = nano.db.use('alice');

This code gave me a error, that no attribute called dbexists. Intellisense show me only auth, config, session as attributes:

VS Code intellisense screenshot

According to the comment-header, the typings are for couchdb-nano (no other project which is called nano, too) and also for version 6.4, which is used here.

So what I'm doing wrong?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Daniel
  • 968
  • 3
  • 13
  • 32

3 Answers3

1

Looks like you should be able to cast the Nano function to the ServerScope interface and then intellisense should work when interacting with your nano object.

import Nano, { ServerScope } from "nano";

const nano = Nano("http://localhost:5984") as ServerScope;
const alice = nano.db.use("alice");
Harry
  • 353
  • 2
  • 9
  • Look twice before you get stuck on this example above. It was very helpful too but the answers below were much better since they separate `instance` and `database`. – PatrikJ Aug 09 '20 at 09:36
1

Found out that this issue is caused by an ambiguous return value in the type defintion:

declare function nano(
  config: nano.Configuration | string
): nano.ServerScope | nano.DocumentScope<any>;

The nano function can have nano.ServerScope or nano.DocumentScope<any> as return value. By doing some reverse engineering, I learned that ServerScope is the right class for me. It provides properties like db, which are present in the documentation.

So we need to explicitly cast here:

let nanoInstance = <Nano.ServerScope>Nano("http://localhost:5984");

Now all the example-codes from the docs works well like this:

nanoInstance.db.create("test123");

What's the difference?

I'm too new in nosql-databases for exactly explaining the difference. But i'd assume that by providing some kind of url-parameter, we can directly connect to a single document, instead of the entire database. I'll try to edit this post when I know more. For the moment, this is not primary relevant for me.

Daniel
  • 968
  • 3
  • 13
  • 32
  • `nano.db.create('test123');` would create a database named `test123` and this statement: `const db = nano.db.use('test123');` will use the `test123` database as `db`. Then you can do lots of things to the `test123` database like `db.multipart.insert(...)` and many more. Take a look at [this documentation](https://github.com/apache/couchdb-nano). – Megidd Apr 18 '18 at 16:34
1

I'm currently using nano with typescript, and intellisense seems to be working fine. I had it setup this way.

import Nano from 'nano';

const instance: Nano.ServerScope = Nano('http://localhost:5984/');
const db: Nano.DocumentScope<{}> = instance.db.use('alice');

For the dependencies, I'm using:

"dependencies": {
  "nano": "^7.0.0"
},
"devDependencies": {
  "@types/nano": "^6.4.6"
}
Adrian
  • 347
  • 4
  • 13