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

Cannot find module 'msnodesqlv8/types' or its corresponding type declarations. #334

Open
ahjulstad opened this issue May 7, 2024 · 2 comments

Comments

@ahjulstad
Copy link

I am developing small Express server in Typescript. I am unable to get type declarations to work, specifically this line:

import { PoolStatusRecord, PoolOptions,  Pool } from "msnodesqlv8/types

gives error message:

TSError: ⨯ Unable to compile TypeScript:
src/index.ts:3:54 - error TS2307: Cannot find module 'msnodesqlv8/types' or its corresponding type declarations.

3 import { PoolStatusRecord, PoolOptions,  Pool } from "msnodesqlv8/types"

I can const sql = require("msnodesqlv8"); and query like this:

app.get("/api/xxx", async (req: Request, res: Response) => {
  res.setHeader("Content-Type", "application/json");

  try {
    sql.query(conn_str, querystr, (err: any, result: any) => {
      // Send the query result as the response
      console.log("Received");
      if (err) {
        console.error("Error executing SQL query:", err);
        res.status(500).send("Internal Server Error");
      } else {
        const jsonData = JSON.stringify(result);
        res.send(jsonData);
      }
    });
  } catch (error) {
    console.error("Error executing SQL query:", error);
    res.status(500).send("Internal Server Error");
  }
});

but I wanted to get the pool to work. (maybe that is not necessary)

package.json :

{
  "name": "bsservermock",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "dependencies": {
    "@types/cors": "^2.8.17",
    "@types/express": "^4.17.21",
    "@types/mssql": "^9.1.5",
    "@types/node": "^20.12.10",
    "cors": "^2.8.5",
    "dotenv": "^16.4.5",
    "express": "^4.19.2",
    "msnodesqlv8": "^4.2.1",
    "mssql": "^10.0.2",
    "nodemon": "^3.1.0",
    "tedious": "^18.2.0",
    "ts-node-dev": "^2.0.0"
  },
  "scripts": {
    "build": "npx tsc",
    "start": "node dist/index.js",
    "dev": "nodemon src/index.ts"
  },
  "devDependencies": {
    "typescript": "^5.4.5"
  }
}
node --version
v20.11.1
@TimelordUK
Copy link
Owner

if you use latest version does this notation work for you

import sql from 'msnodesqlv8'
import Connection = MsNodeSqlV8.Connection
import ConnectionPromises = MsNodeSqlV8.ConnectionPromises
async function t() {
    const connectionString  = "Driver={ODBC Driver 17 for SQL Server};Server=(localdb)\\node;Database=scratch;Trusted_Connection=yes;"
    const con:Connection = await sql.promises.open(connectString)
    const promises:ConnectionPromises  = con.promises
    const res = await promises.query('select @@servername as server')
    console.log(JSON.stringify(res, null, 4))
    await con.promises.close()
}

t().then(() => {
    console.log('closed')
})

@ahjulstad
Copy link
Author

Yes, it does. Me learning Typescript, possibly...

import Pool = MsNodeSqlV8.Pool
import PoolOptions = MsNodeSqlV8.PoolOptions

const options: PoolOptions = {
  connectionString: conn_str,
  ceiling: 3
}
const pool: Pool = new sql.Pool(options);
pool.open()

and then doing this in the Express .get handler:

    pool.query(query_str, (err: any, result: any) => {
    //sql.query(conn_str, query_str, (err: any, result: any) => {
      // Send the query result as the response
      console.log("Received");

... it seems to work well (and perhaps a bit quicker)

Thank you very much.

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