Skip to content

Commit

Permalink
feat: add mysql support
Browse files Browse the repository at this point in the history
This changeset introduces end-to-end tests for MySQL support via the
`mysql2` driver along with base README documentation on how to use the
connector with a MySQL Cloud SQL instance.

Fixes: #65
  • Loading branch information
ruyadorno committed Apr 3, 2023
1 parent f12e27d commit 9316804
Show file tree
Hide file tree
Showing 7 changed files with 234 additions and 12 deletions.
44 changes: 39 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ The Cloud SQL Node.js Connector is a package to be used alongside a database
driver. Currently supported drivers are:

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

## Installation

Expand All @@ -31,14 +32,18 @@ npm install @google-cloud/cloud-sql-connector
## Usage

The connector package is meant to be used alongside a database driver, in the
following example you can see how to create a new connector and get valid
options that can then be used when starting a new
following examples you can see how to create a new connector and get valid
options that can then be used when starting a new connection.

### Using with PostgreSQL

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

```js
import pg from 'pg';
import {Connector} from '@google-cloud/cloud-sql-connector';
const { Pool } = pg;
const {Pool} = pg;

const connector = new Connector();
const clientOpts = await connector.getOptions({
Expand All @@ -52,10 +57,39 @@ const pool = new Pool({
database: 'db-name',
max: 5
});
const result = await pool.query('SELECT NOW()');
const {rows} = await pool.query('SELECT NOW()');
console.table(rows); // prints returned time value from server

await pool.end();
connector.close();
```

### Using with MySQL

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

```js
import mysql from 'mysql2/promise';
import {Connector} from '@google-cloud/cloud-sql-connector';

const connector = new Connector();
const clientOpts = await connector.getOptions({
instanceConnectionName: 'my-project:region:my-instance',
type: 'PUBLIC',
});
const pool = await mysql.createPool({
...clientOpts,
user: 'my-user',
password: 'my-password',
database: 'db-name',
});
const conn = await pool.getConnection();
const [result] = await conn.query( `SELECT NOW();`);
console.table(result); // prints returned time value from server

await pool.end();
connector.close()
connector.close();
```

## Supported Node.js Versions
Expand Down
109 changes: 108 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"@types/tap": "^15.0.8",
"c8": "^7.12.0",
"gts": "^3.1.1",
"mysql2": "^3.2.0",
"nock": "^13.3.0",
"pg": "^8.10.0",
"tap": "^16.3.4",
Expand Down
38 changes: 38 additions & 0 deletions system-test/mysql2-connect.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

const t = require('tap');
const mysql = require('mysql2/promise');
const {Connector} = require('@google-cloud/cloud-sql-connector');

t.test('open connection and run basic mysql commands', async t => {
const connector = new Connector();
const clientOpts = await connector.getOptions({
instanceConnectionName: process.env.MYSQL_CONNECTION_NAME,
type: 'PUBLIC',
});
const conn = await mysql.createConnection({
...clientOpts,
user: process.env.MYSQL_USER,
password: process.env.MYSQL_PASS,
database: process.env.MYSQL_DB,
});

const [[result]] = await conn.query('SELECT NOW();');
const returnedDate = result['NOW()'];
t.ok(returnedDate.getTime(), 'should have valid returned date object');

await conn.end();
connector.close();
});
38 changes: 38 additions & 0 deletions system-test/mysql2-connect.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import t from 'tap';
import mysql from 'mysql2/promise';
import {Connector} from '@google-cloud/cloud-sql-connector';

t.test('open connection and run basic mysql commands', async t => {
const connector = new Connector();
const clientOpts = await connector.getOptions({
instanceConnectionName: process.env.MYSQL_CONNECTION_NAME,
type: 'PUBLIC',
});
const conn = await mysql.createConnection({
...clientOpts,
user: process.env.MYSQL_USER,
password: process.env.MYSQL_PASS,
database: process.env.MYSQL_DB,
});

const [[result]] = await conn.query('SELECT NOW();');
const returnedDate = result['NOW()'];
t.ok(returnedDate.getTime(), 'should have valid returned date object');

await conn.end();
connector.close();
});
8 changes: 5 additions & 3 deletions system-test/pg-connect.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ t.test('open connection and retrieves standard pg tables', async t => {
database: process.env.POSTGRES_DB,
});
client.connect();
const result = await client.query('SELECT * FROM pg_catalog.pg_tables;');

t.same(result.command, 'SELECT', 'should list correct command in response');
t.ok(result.rowCount > 0, 'should be able to retrieve list of tables');
const {
rows: [result],
} = await client.query('SELECT NOW();');
const returnedDate = result['now'];
t.ok(returnedDate.getTime(), 'should have valid returned date object');

await client.end();
connector.close();
Expand Down
8 changes: 5 additions & 3 deletions system-test/pg-connect.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ t.test('open connection and retrieves standard pg tables', async t => {
database: process.env.POSTGRES_DB,
});
client.connect();
const result = await client.query('SELECT * FROM pg_catalog.pg_tables;');

t.same(result.command, 'SELECT', 'should list correct command in response');
t.ok(result.rowCount > 0, 'should be able to retrieve list of tables');
const {
rows: [result],
} = await client.query('SELECT NOW();');
const returnedDate = result['now'];
t.ok(returnedDate.getTime(), 'should have valid returned date object');

await client.end();
connector.close();
Expand Down

0 comments on commit 9316804

Please sign in to comment.