Skip to content

Commit

Permalink
feat: add sqlserver support
Browse files Browse the repository at this point in the history
This changeset introduces a new API to support SQLServer via the
`tedious` driver along with end-to-end tests and base README
documentation on how to use the connector with a SQLServer Cloud SQL
instance.

Fixes: #66
  • Loading branch information
ruyadorno committed May 25, 2023
1 parent b415a1a commit 31c3828
Show file tree
Hide file tree
Showing 8 changed files with 1,723 additions and 51 deletions.
49 changes: 49 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ driver. Currently supported drivers are:

- [`pg`](https://www.npmjs.com/package/pg) (PostgreSQL)
- [`mysql2`](https://www.npmjs.com/package/mysql2) (MySQL)
- [`tedious`](https://www.npmjs.com/package/tedious) (SQL Server)

## Installation

Expand Down Expand Up @@ -152,6 +153,54 @@ await pool.end();
connector.close();
```

### Using with SQL Server

Here is how to start a new
[`tedious`](https://www.npmjs.com/package/tedious) connection.

```js
const {Connection, Request} = require('tedious');
const {Connector} = require('@google-cloud/cloud-sql-connector');

const connector = new Connector();
const clientOpts = await connector.getTediousOptions({
instanceConnectionName: process.env.SQLSERVER_CONNECTION_NAME,
ipType: 'PUBLIC'
});
const connection = new Connection({
server: '0.0.0.0',
authentication: {
type: 'default',
options: {
userName: process.env.SQLSERVER_USER,
password: process.env.SQLSERVER_PASS,
},
},
options: {
...clientOpts,
port: 9999,
database: process.env.SQLSERVER_DB,
},
})

connection.connect(err => {
if (err) { throw err; }
let result;
const req = new Request('SELECT GETUTCDATE()', (err) => {
if (err) { throw err; }
})
req.on('error', (err) => { throw err; });
req.on('row', (columns) => { result = columns; });
req.on('requestCompleted', () => {
console.table(result);
});
connection.execSql(req);
})

connection.close();
connector.close();
```

## Supported Node.js Versions

Our client libraries follow the
Expand Down
Loading

0 comments on commit 31c3828

Please sign in to comment.