-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds TypeORM samples along with a new CI target to run tests against the provided samples, making sure they're always valid usage. Fixes: #112 Co-authored-by: Jack Wotherspoon <[email protected]>
- Loading branch information
1 parent
d07da5b
commit 19066b1
Showing
24 changed files
with
1,511 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Cloud SQL Node.js Connector Examples | ||
|
||
Welcome to the **Cloud SQL Node.js Connector** examples directory. Here you can | ||
find a collection of ready-to-use samples showing how to use the library with | ||
some of the most popular database libraries and frameworks. | ||
|
||
## Available Examples | ||
|
||
### TypeORM | ||
|
||
- [MySQL (CommonJS)](./typeorm/mysql2/connect.cjs) | ||
- [MySQL (ESM)](./typeorm/mysql2/connect.mjs) | ||
- [MySQL (TypeScript)](./typeorm/mysql2/connect.ts) | ||
- [PostgreSQL (CommonJS)](./typeorm/pg/connect.cjs) | ||
- [PostgreSQL (ESM)](./typeorm/pg/connect.mjs) | ||
- [PostgreSQL (TypeScript)](./typeorm/pg/connect.ts) | ||
- [SQL Server (CommonJS)](./typeorm/tedious/connect.cjs) | ||
- [SQL Server (ESM)](./typeorm/tedious/connect.mjs) | ||
- [SQL Server (TypeScript)](./typeorm/tedious/connect.ts) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// 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 {Connector} = require('@google-cloud/cloud-sql-connector'); | ||
const typeorm = require('typeorm'); | ||
|
||
async function connect({ instanceConnectionName, username, database }) { | ||
const connector = new Connector(); | ||
const clientOpts = await connector.getOptions({ | ||
instanceConnectionName, | ||
ipType: 'PUBLIC', | ||
authType: 'IAM', | ||
}); | ||
const dataSource = new typeorm.DataSource({ | ||
type: 'mysql', | ||
username, | ||
database, | ||
extra: clientOpts, | ||
}); | ||
await dataSource.initialize() | ||
|
||
return { | ||
dataSource, | ||
async close() { | ||
await dataSource.destroy(); | ||
connector.close(); | ||
} | ||
}; | ||
}; | ||
|
||
module.exports = { | ||
connect, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// 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 {Connector} from '@google-cloud/cloud-sql-connector'; | ||
import {DataSource} from 'typeorm'; | ||
|
||
export async function connect({ instanceConnectionName, username, database }) { | ||
const connector = new Connector(); | ||
const clientOpts = await connector.getOptions({ | ||
instanceConnectionName, | ||
ipType: 'PUBLIC', | ||
authType: 'IAM', | ||
}); | ||
const dataSource = new DataSource({ | ||
type: 'mysql', | ||
username, | ||
database, | ||
extra: clientOpts, | ||
}); | ||
await dataSource.initialize() | ||
|
||
return { | ||
dataSource, | ||
async close() { | ||
await dataSource.destroy(); | ||
connector.close(); | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// 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 {Connector} from '@google-cloud/cloud-sql-connector'; | ||
import {DataSource} from 'typeorm'; | ||
|
||
export async function connect({instanceConnectionName, username, database}) { | ||
const connector = new Connector(); | ||
const clientOpts = await connector.getOptions({ | ||
instanceConnectionName, | ||
ipType: 'PUBLIC', | ||
authType: 'IAM', | ||
}); | ||
const dataSource = new DataSource({ | ||
type: 'mysql', | ||
username, | ||
database, | ||
extra: clientOpts, | ||
}); | ||
await dataSource.initialize(); | ||
|
||
return { | ||
dataSource, | ||
async close() { | ||
await dataSource.destroy(); | ||
connector.close(); | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// 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 connector = require('../connect.cjs'); | ||
|
||
t.test('mysql2 typeorm cjs', async t => { | ||
const { dataSource, close } = await connector.connect({ | ||
instanceConnectionName: process.env.MYSQL_IAM_CONNECTION_NAME, | ||
username: process.env.MYSQL_IAM_USER, | ||
database: process.env.MYSQL_DB, | ||
}); | ||
const [{ now }] = await dataSource.manager.query('SELECT NOW() as now') | ||
t.ok(now.getTime(), 'should have valid returned date object'); | ||
await close(); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// 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 {connect} from '../connect.mjs'; | ||
|
||
t.test('mysql2 typeorm mjs', async t => { | ||
const { dataSource, close } = await connect({ | ||
instanceConnectionName: process.env.MYSQL_IAM_CONNECTION_NAME, | ||
username: process.env.MYSQL_IAM_USER, | ||
database: process.env.MYSQL_DB, | ||
}); | ||
const [{ now }] = await dataSource.manager.query('SELECT NOW() as now'); | ||
t.ok(now.getTime(), 'should have valid returned date object'); | ||
await close(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// 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 {connect} from '../connect'; | ||
|
||
t.test('mysql typeorm ts', async t => { | ||
const {dataSource, close} = await connect({ | ||
instanceConnectionName: process.env.MYSQL_IAM_CONNECTION_NAME, | ||
username: process.env.MYSQL_IAM_USER, | ||
database: process.env.MYSQL_DB, | ||
}); | ||
const [{now}] = await dataSource.manager.query('SELECT NOW() as now'); | ||
t.ok(now.getTime(), 'should have valid returned date object'); | ||
await close(); | ||
}); |
Oops, something went wrong.