Skip to content

Commit

Permalink
docs: add knex examples tests
Browse files Browse the repository at this point in the history
Added sample tests to the `knex` examples.

Fixes: #111
Closes: #191
  • Loading branch information
ruyadorno committed Nov 24, 2023
1 parent 7e62560 commit 8b662e8
Show file tree
Hide file tree
Showing 22 changed files with 671 additions and 157 deletions.
12 changes: 12 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@ some of the most popular database libraries and frameworks.

## Available Examples

### Knex

- [MySQL (CommonJS)](./knex/mysql2/connect.cjs)
- [MySQL (ESM)](./knex/mysql2/connect.mjs)
- [MySQL (TypeScript)](./knex/mysql2/connect.ts)
- [PostgreSQL (CommonJS)](./knex/pg/connect.cjs)
- [PostgreSQL (ESM)](./knex/pg/connect.mjs)
- [PostgreSQL (TypeScript)](./knex/pg/connect.ts)
- [SQL Server (CommonJS)](./knex/tedious/connect.cjs)
- [SQL Server (ESM)](./knex/tedious/connect.mjs)
- [SQL Server (TypeScript)](./knex/tedious/connect.ts)

### TypeORM

- [MySQL (CommonJS)](./typeorm/mysql2/connect.cjs)
Expand Down
24 changes: 14 additions & 10 deletions examples/knex/mysql2.cjs → examples/knex/mysql2/connect.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
const {Connector} = require('@google-cloud/cloud-sql-connector');
const knex = require('knex');

const main = async () => {
async function connect({ instanceConnectionName, user, databaseName }) {
const connector = new Connector();
const clientOpts = await connector.getOptions({
instanceConnectionName: 'my-project:region:my-instance',
instanceConnectionName,
ipType: 'PUBLIC',
authType: 'IAM',
});
Expand All @@ -27,16 +27,20 @@ const main = async () => {
client: 'mysql2',
connection: {
...clientOpts,
user: 'my-service-account',
database: 'my-database',
user,
databaseName,
},
});

const result = await database.first(database.raw('NOW() AS now'));
console.log(`Current datetime: ${result['now']}`);

await database.destroy();
connector.close();
return {
database,
async close() {
await database.destroy();
connector.close();
}
};
};

main();
module.exports = {
connect,
};
42 changes: 42 additions & 0 deletions examples/knex/mysql2/connect.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// 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 knex from 'knex';

export async function connect({ instanceConnectionName, user, db }) {
const connector = new Connector();
const clientOpts = await connector.getOptions({
instanceConnectionName,
ipType: 'PUBLIC',
authType: 'IAM',
});

const database = knex({
client: 'mysql2',
connection: {
...clientOpts,
user,
db,
},
});

return {
database,
async close() {
await database.destroy();
connector.close();
}
};
};
24 changes: 12 additions & 12 deletions examples/knex/mysql2.ts → examples/knex/mysql2/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ import {
} from '@google-cloud/cloud-sql-connector';
import knex from 'knex';

const main = async () => {
export async function connect({instanceConnectionName, user, db}) {
const connector = new Connector();
const clientOpts = await connector.getOptions({
instanceConnectionName: 'my-project:region:my-instance',
instanceConnectionName,
ipType: IpAddressTypes.PUBLIC,
authType: AuthTypes.IAM,
});
Expand All @@ -31,16 +31,16 @@ const main = async () => {
client: 'mysql2',
connection: {
...clientOpts,
user: 'my-service-account',
database: 'my-database',
user,
db,
},
});

const result = await database.first(database.raw('NOW() AS now'));
console.log(`Current datetime: ${result['now']}`);

await database.destroy();
connector.close();
};

main();
return {
database,
async close() {
await database.destroy();
connector.close();
},
};
}
33 changes: 11 additions & 22 deletions examples/knex/mysql2.mjs → examples/knex/mysql2/test/connect.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,16 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import {Connector} from '@google-cloud/cloud-sql-connector';
import knex from 'knex';
const t = require('tap');
const {connect} = require('../connect.cjs');

const connector = new Connector();
const clientOpts = await connector.getOptions({
instanceConnectionName: 'my-project:region:my-instance',
ipType: 'PUBLIC',
authType: 'IAM',
t.test('mysql knex cjs', async t => {
const {database, close} = await connect({
instanceConnectionName: process.env.MYSQL_IAM_CONNECTION_NAME,
user: process.env.MYSQL_IAM_USER,
db: process.env.MYSQL_DB,
});
const {now} = await database.first(database.raw('NOW() AS now'));
t.ok(now.getTime(), 'should have valid returned date object');
await close();
});

const database = knex({
client: 'mysql2',
connection: {
...clientOpts,
user: 'my-service-account',
database: 'my-database',
},
});

const result = await database.first(database.raw('NOW() AS now'));
console.log(`Current datetime: ${result['now']}`);

await database.destroy();
connector.close();
33 changes: 11 additions & 22 deletions examples/knex/pg.mjs → examples/knex/mysql2/test/connect.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,16 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import {Connector} from '@google-cloud/cloud-sql-connector';
import knex from 'knex';
import t from 'tap';
import {connect} from '../connect.mjs';

const connector = new Connector();
const clientOpts = await connector.getOptions({
instanceConnectionName: 'my-project:region:my-instance',
ipType: 'PUBLIC',
authType: 'IAM',
t.test('mysql knex mjs', async t => {
const {database, close} = await connect({
instanceConnectionName: process.env.MYSQL_IAM_CONNECTION_NAME,
user: process.env.MYSQL_IAM_USER,
db: process.env.MYSQL_DB,
});
const {now} = await database.first(database.raw('NOW() AS now'));
t.ok(now.getTime(), 'should have valid returned date object');
await close();
});

const database = knex({
client: 'pg',
connection: {
...clientOpts,
user: 'my-service-account@my-project.iam',
database: 'my-database',
},
});

const result = await database.first(database.raw('NOW() AS now'));
console.log(`Current datetime: ${result['now']}`);

await database.destroy();
connector.close();
27 changes: 27 additions & 0 deletions examples/knex/mysql2/test/connect.ts
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('mysql knex ts', async t => {
const {database, close} = await connect({
instanceConnectionName: process.env.MYSQL_IAM_CONNECTION_NAME,
user: process.env.MYSQL_IAM_USER,
db: process.env.MYSQL_DB,
});
const {now} = await database.first(database.raw('NOW() AS now'));
t.ok(now.getTime(), 'should have valid returned date object');
await close();
});
24 changes: 14 additions & 10 deletions examples/knex/pg.cjs → examples/knex/pg/connect.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
const {Connector} = require('@google-cloud/cloud-sql-connector');
const knex = require('knex');

const main = async () => {
async function connect({ instanceConnectionName, user, db }) {
const connector = new Connector();
const clientOpts = await connector.getOptions({
instanceConnectionName: 'my-project:region:my-instance',
instanceConnectionName,
ipType: 'PUBLIC',
authType: 'IAM',
});
Expand All @@ -27,16 +27,20 @@ const main = async () => {
client: 'pg',
connection: {
...clientOpts,
user: 'my-service-account@my-project.iam',
database: 'my-database',
user,
database: db,
},
});

const result = await database.first(database.raw('NOW() AS now'));
console.log(`Current datetime: ${result['now']}`);

await database.destroy();
connector.close();
return {
database,
async close() {
await database.destroy();
connector.close();
}
};
};

main();
module.exports = {
connect,
};
42 changes: 42 additions & 0 deletions examples/knex/pg/connect.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// 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 knex from 'knex';

export async function connect({ instanceConnectionName, user, db }) {
const connector = new Connector();
const clientOpts = await connector.getOptions({
instanceConnectionName,
ipType: 'PUBLIC',
authType: 'IAM',
});

const database = knex({
client: 'pg',
connection: {
...clientOpts,
user,
database: db,
},
});

return {
database,
async close() {
await database.destroy();
connector.close();
}
};
};
32 changes: 20 additions & 12 deletions examples/knex/pg.ts → examples/knex/pg/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,18 @@ import {
} from '@google-cloud/cloud-sql-connector';
import knex from 'knex';

const main = async () => {
export async function connect({
instanceConnectionName,
user,
db,
}: {
instanceConnectionName: string;
user: string;
db: string;
}) {
const connector = new Connector();
const clientOpts = await connector.getOptions({
instanceConnectionName: 'my-project:region:my-instance',
instanceConnectionName,
ipType: IpAddressTypes.PUBLIC,
authType: AuthTypes.IAM,
});
Expand All @@ -31,16 +39,16 @@ const main = async () => {
client: 'pg',
connection: {
...clientOpts,
user: 'my-service-account@my-project.iam',
database: 'my-database',
user,
database: db,
},
});

const result = await database.first(database.raw('NOW() AS now'));
console.log(`Current datetime: ${result['now']}`);

await database.destroy();
connector.close();
};

main();
return {
database,
async close() {
await database.destroy();
connector.close();
},
};
}
Loading

0 comments on commit 8b662e8

Please sign in to comment.