What is @types/tedious?
@types/tedious provides TypeScript type definitions for the 'tedious' package, which is a TDS (Tabular Data Stream) protocol implementation for Node.js. It allows you to connect to and interact with Microsoft SQL Server databases.
What are @types/tedious's main functionalities?
Connecting to a SQL Server
This code demonstrates how to establish a connection to a SQL Server using the 'tedious' package. The configuration object includes server details, authentication options, and database name.
const { Connection } = require('tedious');
const config = {
server: 'your_server_name',
authentication: {
type: 'default',
options: {
userName: 'your_username',
password: 'your_password'
}
},
options: {
database: 'your_database_name'
}
};
const connection = new Connection(config);
connection.on('connect', err => {
if (err) {
console.error('Connection failed', err);
} else {
console.log('Connected');
}
});
Executing a SQL Query
This code demonstrates how to execute a SQL query using the 'tedious' package. It creates a new Request object with the SQL query and a callback function to handle the results.
const { Request } = require('tedious');
const request = new Request('SELECT * FROM your_table_name', (err, rowCount, rows) => {
if (err) {
console.error('Query failed', err);
} else {
console.log(`${rowCount} rows returned`);
rows.forEach(row => {
console.log(row);
});
}
});
connection.execSql(request);
Handling SQL Server Events
This code demonstrates how to handle various events emitted by the SQL Server connection, such as errors and connection closure.
connection.on('error', err => {
console.error('Connection error', err);
});
connection.on('end', () => {
console.log('Connection closed');
});
Other packages similar to @types/tedious
mssql
The 'mssql' package is a Microsoft SQL Server client for Node.js. It supports both Promises and async/await syntax, making it easier to work with modern JavaScript. Compared to 'tedious', 'mssql' provides a higher-level API and additional features like connection pooling.
node-mssql
The 'node-mssql' package is another SQL Server client for Node.js, built on top of 'tedious'. It offers a more user-friendly API and additional functionalities such as connection pooling and simplified query execution. It is often preferred for its ease of use and better integration with modern JavaScript features.