databricks-sql-nodejs icon indicating copy to clipboard operation
databricks-sql-nodejs copied to clipboard

Example code in docs doesn't work when NodeNext is used for module resolution

Open KristaHyer opened this issue 1 year ago • 2 comments

Using this tsconfig.json I can get the example code to build without error.

tsconfig.json:

{
  "compilerOptions": {
    "lib": ["ES2022"],
    "outDir": "./dist",
    "rootDir": "./src",
    "target": "ES2022",
    "module": "commonjs",
    "moduleResolution": "node",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "resolveJsonModule": true,
    "allowSyntheticDefaultImports": true,
    "skipLibCheck": true
  }
}

Code from website - https://docs.databricks.com/en/dev-tools/nodejs-sql-driver.html#language-TypeScript:

import { DBSQLClient } from '@databricks/sql';
import IDBSQLSession from '@databricks/sql/dist/contracts/IDBSQLSession';
import IOperation from '@databricks/sql/dist/contracts/IOperation';

const serverHostname: string = process.env.DATABRICKS_SERVER_HOSTNAME || '';
const httpPath: string       = process.env.DATABRICKS_HTTP_PATH || '';
const token: string          = process.env.DATABRICKS_TOKEN || '';

if (serverHostname == '' || httpPath == '' || token == '') {
  throw new Error("Cannot find Server Hostname, HTTP Path, or personal access token. " +
                  "Check the environment variables DATABRICKS_SERVER_HOSTNAME, " +
                  "DATABRICKS_HTTP_PATH, and DATABRICKS_TOKEN.");
}

const client: DBSQLClient = new DBSQLClient();
const connectOptions = {
  host: serverHostname,
  path: httpPath,
  token: token
};

client.connect(connectOptions)
  .then(async client => {
    const session: IDBSQLSession = await client.openSession();

    const queryOperation: IOperation = await session.executeStatement(
      'SELECT * FROM samples.nyctaxi.trips LIMIT 2',
      {
        runAsync: true,
        maxRows: 10000 // This option enables the direct results feature.
      }
    );

    const result = await queryOperation.fetchAll();

    await queryOperation.close();

    console.table(result);

    await session.close();
    client.close();
  })
  .catch((error) => {
    console.error(error);
});

However, when changing the tsconfig to use NodeNext (like the below):

{
  "compilerOptions": {
    "lib": ["ES2022"],
    "outDir": "./dist",
    "rootDir": "./src",
    "target": "ES2022",
    "module": "NodeNext",
    "moduleResolution": "NodeNext",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "resolveJsonModule": true,
    "allowSyntheticDefaultImports": true,
    "skipLibCheck": true
  }
}

I see the following error on the imports: Cannot find module '@databricks/sql/dist/contracts/IDBSQLSession' or its corresponding type declarations.ts(2307) Cannot find module '@databricks/sql/dist/contracts/IOperation' or its corresponding type declarations.ts(2307)

Versions:

  • node 22
  • this is the package.json:
{
  "name": "example",
  "version": "1.0.0",
  "description": "show example of issue",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "build": "tsc"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@databricks/sql": "^1.9.0"
  },
  "devDependencies": {
    "typescript": "^5.6.3",
    "typescript-eslint": "^8.14.0"
  }
}

KristaHyer avatar Jan 24 '25 14:01 KristaHyer

I worked around this by changing the imports to be like so:

import type IDBSQLSession from "@databricks/sql/dist/contracts/IDBSQLSession.d.ts";
import type IOperation from "@databricks/sql/dist/contracts/IOperation.d.ts";

Also where the types were used I added .default, for example:

    const session: IDBSQLSession.default = await client.openSession();

KristaHyer avatar Jan 24 '25 15:01 KristaHyer

Hi @KristaHyer , the IDBSQLSession is the default export of @databricks/sql/dist/contracts/IDBSQLSession and the generated IDBSQLSession.d.ts is correct and expected, it should work with both node and ES2022. I just verified your tsconfig.json with ES2022 and it can be compiled w/o error.

jackyhu-db avatar Feb 14 '25 17:02 jackyhu-db