Home > database >  When using mssql queries in Nodejs, Can't I use it like mysql2?
When using mssql queries in Nodejs, Can't I use it like mysql2?

Time:02-24

I used queries very simply when used mysql2 in Nodejs.

('select * from table where A = ? AND B = ?', [val1, val2])

But I'm using mssql this time, and it seems very uncomfortable.

const result = await pool.request()
.input('input_parameter', sql.Int, req.query.input_parameter)
.query('select * from mytable where id = @input_parameter')      

})

When using the mssql module, can't I use it as simple as mysql2?

CodePudding user response:

According to the docs the closest thing is the tagged template literal syntax:

const conn = await sql.connect(config);
const result = await conn.query`select * from table where A = ${val1} AND b = ${val2}`;

Since it's a tagged template literal, not just a template string, the module can do sanitization for you. (Just be mindful of the syntax: not conn.query(...).)

You can see the implementation for those template strings here, if you're curious.

  • Related