TypeScript support?

I can’t figure out how to use the SDK 3.1 with my TypeScript project.

The only module declared (in .d.ts) is “couchbase” , containing no members (except connect function) and none of the types are exported.

attempting to import or use anything from couchbase fails:

import {Cluster} from “couchbase” - fails, Cluster is declared but not exported
import {Bucket} from “couchbase” - fails, Bucket is declared, but not exported, etc

On top of this, the type definitions appear to be out of date (UPDATE: see below, the types are not out of date, the documentation is)

As such, I’m totally lost as to how to use this SDK with TypeScript.

I’m also exp. the same issue. As a temporary workaround I have a build script that exports all the declared members from the types.d.ts file. To get familiar with the SDK/API changes you could search/replace declare with export declare. Note that when they fix the types.d.ts file during their build process it could break your imports. Hope this helps

-Mark

This is precisely what I don’t want to do. I have to imagine we’re using the SDK in some unintended manner. Can it really be true that the entire Node SDK exposes only a single exported property (lcbVersion) and a single exported method (connect) across the entire module?

In addition, the documentation for the 3.1 SDK appears to be outdated/incorrect:

//WRONG - unsupported by SDK 3.1
const cb = require('couchbase')
const cluster = new cb.Cluster('couchbase://localhost', {
     username: 'user',
     password: 'pass'
 })
//CORRECT
import * as couchbase from "couchbase"
const cluster = await couchbase.connect('couchbase://localhost', {
       username:'user',
       password: 'pass'
 })

Help!

I found ONE way to import that seems to be holding for now.

Critically, it uses a completely different connection method than is detailed in the Node SDK 3.1 documentation AND it imports couchbase as a whole.

app.ts:

//couchbase types
import * as couchbase from "couchbase"

export class App {
    cluster: Cluster | null = null
    async connectToDb() {
        //connect using the SDK's API, rather than instruction in docs
        // i.e. new couchbase.Cluster() is unsupported in SDK 3.1
        const cluster = await couchbase.connect('couchbase://localhost', {
            username: 'user',
            password: 'pass'
        }
         //type works correctly - you MUST await or then(), because the Cluster
         // instance is only available via the returned Promise
        this.cluster = cluster
    }
}

If this is the correct usage in 3.1, please update docs at once.l

For me your approach works to get the compiler working. However, when I use it, I get run-time exceptions:

ReferenceError: SearchQuery is not defined

The cluster works / connects, but it seems everything else in the SDK is unavailable since the members aren’t exported in the definition file.

For SDK v 3.1.0, doing:

import * as couchbase from "couchbase"

gives me access to the non-exported types (I only need Cluster and Bucket for now, but other types including SearchQuery are accessible via the import without Error)

Here’s a working query example (after setting this.cluster (above)):

async performQuery() {
    const res = await this.cluster!.query("SELECT bucket.* FROM bucket WHERE meta().id = 'mydocument'")
    //Loop thru rows, each is an object
    res.rows.forEach((row: Object) => {
         //cast the the row to my expected type
         const myobject = <MyClass>row
         console.log(`doing something with cluster: ${myobject.prop1}`)
    })
}

My tsconfig.json file looks like:

{
  "compilerOptions": {
    /* Basic Options */
    "target": "ES6",
    "module": "commonjs",
    /* skip corresponding '.d.ts' file. */
    "declaration": false,
    /* Redirect output structure to the directory. */
    "outDir": "dist",
    /* Enable all strict type-checking options. */
    "strict": true,
    /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
    "allowSyntheticDefaultImports": true,
    /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
    "esModuleInterop": true,
    //allow Javascript imports in TypeScript
    "allowJs": true
  }
}

Hope this helps

@Robert_Mirabelle thanks for the help. I was able to get a basic query working with your sample, but still hit a reference error when executing a full text search using the API. Going wait on this a bit with the hope the SDK dev(s) will review this thread and update the def file and / or create some working TypeScript samples.

result = await cluster.searchQuery(this.config.bucket, SearchQuery.queryString(query), { skip: start, limit: limit });

Output from above:
testing RobertsTest…
*** cluster created ***
query +docType:RequestHeader
ReferenceError: SearchQuery is not defined