diff --git a/examples/README.md b/examples/README.md index 69302368..98dc083e 100644 --- a/examples/README.md +++ b/examples/README.md @@ -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) diff --git a/examples/knex/mysql2.cjs b/examples/knex/mysql2/connect.cjs similarity index 73% rename from examples/knex/mysql2.cjs rename to examples/knex/mysql2/connect.cjs index b11e5c52..91420bf6 100644 --- a/examples/knex/mysql2.cjs +++ b/examples/knex/mysql2/connect.cjs @@ -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', }); @@ -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, +}; diff --git a/examples/knex/mysql2/connect.mjs b/examples/knex/mysql2/connect.mjs new file mode 100644 index 00000000..1965e2af --- /dev/null +++ b/examples/knex/mysql2/connect.mjs @@ -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(); + } + }; +}; diff --git a/examples/knex/mysql2.ts b/examples/knex/mysql2/connect.ts similarity index 74% rename from examples/knex/mysql2.ts rename to examples/knex/mysql2/connect.ts index b9fc67ef..9a204bd9 100644 --- a/examples/knex/mysql2.ts +++ b/examples/knex/mysql2/connect.ts @@ -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, }); @@ -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(); + }, + }; +} diff --git a/examples/knex/mysql2.mjs b/examples/knex/mysql2/test/connect.cjs similarity index 50% rename from examples/knex/mysql2.mjs rename to examples/knex/mysql2/test/connect.cjs index 4cdbcd12..dea5c77a 100644 --- a/examples/knex/mysql2.mjs +++ b/examples/knex/mysql2/test/connect.cjs @@ -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(); diff --git a/examples/knex/pg.mjs b/examples/knex/mysql2/test/connect.mjs similarity index 50% rename from examples/knex/pg.mjs rename to examples/knex/mysql2/test/connect.mjs index ed3ae847..7d205deb 100644 --- a/examples/knex/pg.mjs +++ b/examples/knex/mysql2/test/connect.mjs @@ -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(); diff --git a/examples/knex/mysql2/test/connect.ts b/examples/knex/mysql2/test/connect.ts new file mode 100644 index 00000000..c05b18c9 --- /dev/null +++ b/examples/knex/mysql2/test/connect.ts @@ -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 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(); +}); diff --git a/examples/knex/pg.cjs b/examples/knex/pg/connect.cjs similarity index 72% rename from examples/knex/pg.cjs rename to examples/knex/pg/connect.cjs index 20e984c8..e3c1ee24 100644 --- a/examples/knex/pg.cjs +++ b/examples/knex/pg/connect.cjs @@ -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', }); @@ -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, +}; diff --git a/examples/knex/pg/connect.mjs b/examples/knex/pg/connect.mjs new file mode 100644 index 00000000..ddb2f156 --- /dev/null +++ b/examples/knex/pg/connect.mjs @@ -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(); + } + }; +}; diff --git a/examples/knex/pg.ts b/examples/knex/pg/connect.ts similarity index 73% rename from examples/knex/pg.ts rename to examples/knex/pg/connect.ts index de92fad0..d0bb0563 100644 --- a/examples/knex/pg.ts +++ b/examples/knex/pg/connect.ts @@ -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, }); @@ -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(); + }, + }; +} diff --git a/examples/knex/pg/test/connect.cjs b/examples/knex/pg/test/connect.cjs new file mode 100644 index 00000000..952cea44 --- /dev/null +++ b/examples/knex/pg/test/connect.cjs @@ -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. + +const t = require('tap'); +const {connect} = require('../connect.cjs'); + +t.test('pg knex cjs', async t => { + const {database, close} = await connect({ + instanceConnectionName: process.env.POSTGRES_IAM_CONNECTION_NAME, + user: process.env.POSTGRES_IAM_USER, + db: process.env.POSTGRES_DB, + }); + const {now} = await database.first(database.raw('NOW() AS now')); + t.ok(now.getTime(), 'should have valid returned date object'); + await close(); +}); diff --git a/examples/knex/pg/test/connect.mjs b/examples/knex/pg/test/connect.mjs new file mode 100644 index 00000000..5badd5f6 --- /dev/null +++ b/examples/knex/pg/test/connect.mjs @@ -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('pg knex mjs', async t => { + const {database, close} = await connect({ + instanceConnectionName: process.env.POSTGRES_IAM_CONNECTION_NAME, + user: process.env.POSTGRES_IAM_USER, + db: process.env.POSTGRES_DB, + }); + const {now} = await database.first(database.raw('NOW() AS now')); + t.ok(now.getTime(), 'should have valid returned date object'); + await close(); +}); diff --git a/examples/knex/pg/test/connect.ts b/examples/knex/pg/test/connect.ts new file mode 100644 index 00000000..49861b4b --- /dev/null +++ b/examples/knex/pg/test/connect.ts @@ -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('pg knex ts', async t => { + const {database, close} = await connect({ + instanceConnectionName: String(process.env.POSTGRES_IAM_CONNECTION_NAME), + user: String(process.env.POSTGRES_IAM_USER), + db: String(process.env.POSTGRES_DB), + }); + const {now} = await database.first(database.raw('NOW() AS now')); + t.ok(now.getTime(), 'should have valid returned date object'); + await close(); +}); diff --git a/examples/knex/tedious.mjs b/examples/knex/tedious.mjs deleted file mode 100644 index a660bf40..00000000 --- a/examples/knex/tedious.mjs +++ /dev/null @@ -1,46 +0,0 @@ -// 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'; - -const connector = new Connector(); -const clientOpts = await connector.getTediousOptions({ - instanceConnectionName: 'my-project:region:my-instance', - ipType: 'PUBLIC', - authType: 'PASSWORD', -}); - -const database = knex({ - client: 'mssql', - connection: { - // `server` property is not used when connector is provided, but it is a required property - // due to a bug in the tedious driver (ref: https://github.com/tediousjs/tedious/issues/1541). - // There is a pending fix (ref: https://github.com/tediousjs/tedious/pull/1542), but until - // it is released, we need to provide a dummy value. - server: '0.0.0.0', - user: 'my-user', - password: 'my-password', - database: 'my-database', - options: { - ...clientOpts, - }, - }, -}); - -const result = await database.first(database.raw('GETUTCDATE() AS now')); -console.log(`Current datetime: ${result['now']}`); - -await database.destroy(); -connector.close(); diff --git a/examples/knex/tedious.cjs b/examples/knex/tedious/connect.cjs similarity index 78% rename from examples/knex/tedious.cjs rename to examples/knex/tedious/connect.cjs index 580712bf..9c049eb5 100644 --- a/examples/knex/tedious.cjs +++ b/examples/knex/tedious/connect.cjs @@ -15,10 +15,10 @@ const {Connector} = require('@google-cloud/cloud-sql-connector'); const knex = require('knex'); -const main = async () => { +async function connect({ instanceConnectionName, user, password, db }) { const connector = new Connector(); const clientOpts = await connector.getTediousOptions({ - instanceConnectionName: 'my-project:region:my-instance', + instanceConnectionName, ipType: 'PUBLIC', authType: 'PASSWORD', }); @@ -31,20 +31,24 @@ const main = async () => { // There is a pending fix (ref: https://github.com/tediousjs/tedious/pull/1542), but until // it is released, we need to provide a dummy value. server: '0.0.0.0', - user: 'my-user', - password: 'my-password', - database: 'my-database', + user, + password, + database: db, options: { ...clientOpts, }, }, }); - const result = await database.first(database.raw('GETUTCDATE() 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, +}; diff --git a/examples/knex/tedious/connect.mjs b/examples/knex/tedious/connect.mjs new file mode 100644 index 00000000..182714e2 --- /dev/null +++ b/examples/knex/tedious/connect.mjs @@ -0,0 +1,50 @@ +// 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, password, db }) { + const connector = new Connector(); + const clientOpts = await connector.getTediousOptions({ + instanceConnectionName, + ipType: 'PUBLIC', + authType: 'PASSWORD', + }); + + const database = knex({ + client: 'mssql', + connection: { + // `server` property is not used when connector is provided, but it is a required property + // due to a bug in the tedious driver (ref: https://github.com/tediousjs/tedious/issues/1541). + // There is a pending fix (ref: https://github.com/tediousjs/tedious/pull/1542), but until + // it is released, we need to provide a dummy value. + server: '0.0.0.0', + user, + password, + database: db, + options: { + ...clientOpts, + }, + }, + }); + + return { + database, + async close() { + await database.destroy(); + connector.close(); + } + }; +}; diff --git a/examples/knex/tedious.ts b/examples/knex/tedious/connect.ts similarity index 79% rename from examples/knex/tedious.ts rename to examples/knex/tedious/connect.ts index 50c6423f..7663573f 100644 --- a/examples/knex/tedious.ts +++ b/examples/knex/tedious/connect.ts @@ -19,10 +19,10 @@ import { } from '@google-cloud/cloud-sql-connector'; import knex from 'knex'; -const main = async () => { +export async function connect({instanceConnectionName, user, password, db}) { const connector = new Connector(); const clientOpts = await connector.getTediousOptions({ - instanceConnectionName: 'my-project:region:my-instance', + instanceConnectionName, ipType: IpAddressTypes.PUBLIC, authType: AuthTypes.PASSWORD, }); @@ -35,20 +35,20 @@ const main = async () => { // There is a pending fix (ref: https://github.com/tediousjs/tedious/pull/1542), but until // it is released, we need to provide a dummy value. server: '0.0.0.0', - user: 'my-user', - password: 'my-password', - database: 'my-database', + user, + password, + database: db, options: { ...clientOpts, }, }, }); - const result = await database.first(database.raw('GETUTCDATE() AS now')); - console.log(`Current datetime: ${result['now']}`); - - await database.destroy(); - connector.close(); -}; - -main(); + return { + database, + async close() { + await database.destroy(); + connector.close(); + }, + }; +} diff --git a/examples/knex/tedious/test/connect.cjs b/examples/knex/tedious/test/connect.cjs new file mode 100644 index 00000000..7f93da11 --- /dev/null +++ b/examples/knex/tedious/test/connect.cjs @@ -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 {connect} = require('../connect.cjs'); + +t.test('tedious knex cjs', async t => { + const { database, close } = await connect({ + instanceConnectionName: process.env.SQLSERVER_CONNECTION_NAME, + user: process.env.SQLSERVER_USER, + password: process.env.SQLSERVER_PASS, + database: process.env.SQLSERVER_DB, + }); + const {now} = await database.first(database.raw('GETUTCDATE() AS now')); + t.ok(now.getTime(), 'should have valid returned date object'); + await close(); +}); diff --git a/examples/knex/tedious/test/connect.mjs b/examples/knex/tedious/test/connect.mjs new file mode 100644 index 00000000..07ec344e --- /dev/null +++ b/examples/knex/tedious/test/connect.mjs @@ -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. + +import t from 'tap'; +import {connect} from '../connect.mjs'; + +t.test('tedious knex mjs', async t => { + const { database, close } = await connect({ + instanceConnectionName: process.env.SQLSERVER_CONNECTION_NAME, + user: process.env.SQLSERVER_USER, + password: process.env.SQLSERVER_PASS, + database: process.env.SQLSERVER_DB, + }); + const {now} = await database.first(database.raw('GETUTCDATE() AS now')); + t.ok(now.getTime(), 'should have valid returned date object'); + await close(); +}); diff --git a/examples/knex/tedious/test/connect.ts b/examples/knex/tedious/test/connect.ts new file mode 100644 index 00000000..819d2c1a --- /dev/null +++ b/examples/knex/tedious/test/connect.ts @@ -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. + +import t from 'tap'; +import {connect} from '../connect'; + +t.test('tedious knex ts', async t => { + const {database, close} = await connect({ + instanceConnectionName: process.env.SQLSERVER_CONNECTION_NAME, + user: process.env.SQLSERVER_USER, + password: process.env.SQLSERVER_PASS, + database: process.env.SQLSERVER_DB, + }); + const {now} = await database.first(database.raw('GETUTCDATE() AS now')); + t.ok(now.getTime(), 'should have valid returned date object'); + await close(); +}); diff --git a/package-lock.json b/package-lock.json index e6cc325f..44c26b7b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -23,6 +23,7 @@ "@typescript-eslint/eslint-plugin": "^6.3.0", "eslint-plugin-prettier": "^5.0.0", "gts": "^5.0.0", + "knex": "^3.0.1", "mssql": "^9.3.2", "mysql2": "^3.2.0", "nock": "^13.3.0", @@ -3445,6 +3446,12 @@ "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==", "dev": true }, + "node_modules/colorette": { + "version": "2.0.19", + "resolved": "https://registry.npmjs.org/colorette/-/colorette-2.0.19.tgz", + "integrity": "sha512-3tlv/dIP7FWvj3BsbHrGLJ6l/oKh1O3TcgBqMn+yyCagOxc23fyzDS6HypQbgxWbkpDnf52p1LuR4eWDQ/K9WQ==", + "dev": true + }, "node_modules/combined-stream": { "version": "1.0.8", "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", @@ -4211,6 +4218,15 @@ "url": "https://opencollective.com/eslint" } }, + "node_modules/esm": { + "version": "3.2.25", + "resolved": "https://registry.npmjs.org/esm/-/esm-3.2.25.tgz", + "integrity": "sha512-U1suiZ2oDVWv4zPO56S0NcR5QriEahGtdN2OR6FiOG4WJvcjBVFB0qI4+eKoWFH483PKGuLuu6V8Z4T5g63UVA==", + "dev": true, + "engines": { + "node": ">=6" + } + }, "node_modules/espree": { "version": "9.6.1", "resolved": "https://registry.npmjs.org/espree/-/espree-9.6.1.tgz", @@ -4677,6 +4693,15 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/get-package-type": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/get-package-type/-/get-package-type-0.1.0.tgz", + "integrity": "sha512-pjzuKtY64GYfWizNAJ0fr9VqttZkNiK2iS430LtIHzjBEr6bX8Am2zm4sW4Ro5wjWW5cAlRL1qAMTcXbjNAO2Q==", + "dev": true, + "engines": { + "node": ">=8.0.0" + } + }, "node_modules/get-stream": { "version": "6.0.1", "resolved": "https://registry.npmjs.org/get-stream/-/get-stream-6.0.1.tgz", @@ -4705,6 +4730,12 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/getopts": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/getopts/-/getopts-2.3.0.tgz", + "integrity": "sha512-5eDf9fuSXwxBL6q5HX+dhDj+dslFGWzU5thZ9kNKUkcPtaPdatmUFKwHFrLb/uf/WpA4BHET+AX3Scl56cAjpA==", + "dev": true + }, "node_modules/glob": { "version": "7.2.3", "resolved": "https://registry.npmjs.org/glob/-/glob-7.2.3.tgz", @@ -5726,6 +5757,15 @@ "node": ">= 0.4" } }, + "node_modules/interpret": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/interpret/-/interpret-2.2.0.tgz", + "integrity": "sha512-Ju0Bz/cEia55xDwUWEa8+olFpCiQoypjnQySseKtmjNrnps3P+xfpUmGr90T7yjlVJmOtybRvPXhKMbHr+fWnw==", + "dev": true, + "engines": { + "node": ">= 0.10" + } + }, "node_modules/ip": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/ip/-/ip-2.0.0.tgz", @@ -6378,6 +6418,81 @@ "node": ">=0.10.0" } }, + "node_modules/knex": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/knex/-/knex-3.0.1.tgz", + "integrity": "sha512-ruASxC6xPyDklRdrcDy6a9iqK+R9cGK214aiQa+D9gX2ZnHZKv6o6JC9ZfgxILxVAul4bZ13c3tgOAHSuQ7/9g==", + "dev": true, + "dependencies": { + "colorette": "2.0.19", + "commander": "^10.0.0", + "debug": "4.3.4", + "escalade": "^3.1.1", + "esm": "^3.2.25", + "get-package-type": "^0.1.0", + "getopts": "2.3.0", + "interpret": "^2.2.0", + "lodash": "^4.17.21", + "pg-connection-string": "2.6.1", + "rechoir": "^0.8.0", + "resolve-from": "^5.0.0", + "tarn": "^3.0.2", + "tildify": "2.0.0" + }, + "bin": { + "knex": "bin/cli.js" + }, + "engines": { + "node": ">=16" + }, + "peerDependenciesMeta": { + "better-sqlite3": { + "optional": true + }, + "mysql": { + "optional": true + }, + "mysql2": { + "optional": true + }, + "pg": { + "optional": true + }, + "pg-native": { + "optional": true + }, + "sqlite3": { + "optional": true + }, + "tedious": { + "optional": true + } + } + }, + "node_modules/knex/node_modules/commander": { + "version": "10.0.1", + "resolved": "https://registry.npmjs.org/commander/-/commander-10.0.1.tgz", + "integrity": "sha512-y4Mg2tXshplEbSGzx7amzPwKKOCGuoSRP/CjEdwwk0FOGlUbq6lKuoyDZTNZkmxHdJtp54hdfY/JUrdL7Xfdug==", + "dev": true, + "engines": { + "node": ">=14" + } + }, + "node_modules/knex/node_modules/pg-connection-string": { + "version": "2.6.1", + "resolved": "https://registry.npmjs.org/pg-connection-string/-/pg-connection-string-2.6.1.tgz", + "integrity": "sha512-w6ZzNu6oMmIzEAYVw+RLK0+nqHPt8K3ZnknKi+g48Ak2pr3dtljJW3o+D/n2zzCG07Zoe9VOX3aiKpj+BN0pjg==", + "dev": true + }, + "node_modules/knex/node_modules/resolve-from": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/resolve-from/-/resolve-from-5.0.0.tgz", + "integrity": "sha512-qYg9KP24dD5qka9J47d0aVky0N+b4fTU89LN9iDnjB5waksiC49rvMB0PrUJQGoTmH50XPiqOvAjDfaijGxYZw==", + "dev": true, + "engines": { + "node": ">=8" + } + }, "node_modules/levn": { "version": "0.4.1", "resolved": "https://registry.npmjs.org/levn/-/levn-0.4.1.tgz", @@ -8496,6 +8611,18 @@ "node": ">=8.10.0" } }, + "node_modules/rechoir": { + "version": "0.8.0", + "resolved": "https://registry.npmjs.org/rechoir/-/rechoir-0.8.0.tgz", + "integrity": "sha512-/vxpCXddiX8NGfGO/mTafwjq4aFa/71pvamip0++IQk3zG8cbCj0fifNPrjjF1XMXUne91jL9OoxmdykoEtifQ==", + "dev": true, + "dependencies": { + "resolve": "^1.20.0" + }, + "engines": { + "node": ">= 10.13.0" + } + }, "node_modules/redent": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/redent/-/redent-3.0.0.tgz", @@ -9741,6 +9868,15 @@ "integrity": "sha512-w89qg7PI8wAdvX60bMDP+bFoD5Dvhm9oLheFp5O4a2QF0cSBGsBX4qZmadPMvVqlLJBBci+WqGGOAPvcDeNSVg==", "dev": true }, + "node_modules/tildify": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/tildify/-/tildify-2.0.0.tgz", + "integrity": "sha512-Cc+OraorugtXNfs50hU9KS369rFXCfgGLpfCfvlc+Ud5u6VWmUQsOAa9HbTvheQdYnrdJqqv1e5oIqXppMYnSw==", + "dev": true, + "engines": { + "node": ">=8" + } + }, "node_modules/titleize": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/titleize/-/titleize-3.0.0.tgz", @@ -13175,6 +13311,12 @@ "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==", "dev": true }, + "colorette": { + "version": "2.0.19", + "resolved": "https://registry.npmjs.org/colorette/-/colorette-2.0.19.tgz", + "integrity": "sha512-3tlv/dIP7FWvj3BsbHrGLJ6l/oKh1O3TcgBqMn+yyCagOxc23fyzDS6HypQbgxWbkpDnf52p1LuR4eWDQ/K9WQ==", + "dev": true + }, "combined-stream": { "version": "1.0.8", "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", @@ -13711,6 +13853,12 @@ "integrity": "sha512-wpc+LXeiyiisxPlEkUzU6svyS1frIO3Mgxj1fdy7Pm8Ygzguax2N3Fa/D/ag1WqbOprdI+uY6wMUl8/a2G+iag==", "dev": true }, + "esm": { + "version": "3.2.25", + "resolved": "https://registry.npmjs.org/esm/-/esm-3.2.25.tgz", + "integrity": "sha512-U1suiZ2oDVWv4zPO56S0NcR5QriEahGtdN2OR6FiOG4WJvcjBVFB0qI4+eKoWFH483PKGuLuu6V8Z4T5g63UVA==", + "dev": true + }, "espree": { "version": "9.6.1", "resolved": "https://registry.npmjs.org/espree/-/espree-9.6.1.tgz", @@ -14061,6 +14209,12 @@ "hasown": "^2.0.0" } }, + "get-package-type": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/get-package-type/-/get-package-type-0.1.0.tgz", + "integrity": "sha512-pjzuKtY64GYfWizNAJ0fr9VqttZkNiK2iS430LtIHzjBEr6bX8Am2zm4sW4Ro5wjWW5cAlRL1qAMTcXbjNAO2Q==", + "dev": true + }, "get-stream": { "version": "6.0.1", "resolved": "https://registry.npmjs.org/get-stream/-/get-stream-6.0.1.tgz", @@ -14077,6 +14231,12 @@ "get-intrinsic": "^1.1.1" } }, + "getopts": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/getopts/-/getopts-2.3.0.tgz", + "integrity": "sha512-5eDf9fuSXwxBL6q5HX+dhDj+dslFGWzU5thZ9kNKUkcPtaPdatmUFKwHFrLb/uf/WpA4BHET+AX3Scl56cAjpA==", + "dev": true + }, "glob": { "version": "7.2.3", "resolved": "https://registry.npmjs.org/glob/-/glob-7.2.3.tgz", @@ -14753,6 +14913,12 @@ "side-channel": "^1.0.4" } }, + "interpret": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/interpret/-/interpret-2.2.0.tgz", + "integrity": "sha512-Ju0Bz/cEia55xDwUWEa8+olFpCiQoypjnQySseKtmjNrnps3P+xfpUmGr90T7yjlVJmOtybRvPXhKMbHr+fWnw==", + "dev": true + }, "ip": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/ip/-/ip-2.0.0.tgz", @@ -15245,6 +15411,48 @@ "integrity": "sha512-dcS1ul+9tmeD95T+x28/ehLgd9mENa3LsvDTtzm3vyBEO7RPptvAD+t44WVXaUjTBRcrpFeFlC8WCruUR456hw==", "dev": true }, + "knex": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/knex/-/knex-3.0.1.tgz", + "integrity": "sha512-ruASxC6xPyDklRdrcDy6a9iqK+R9cGK214aiQa+D9gX2ZnHZKv6o6JC9ZfgxILxVAul4bZ13c3tgOAHSuQ7/9g==", + "dev": true, + "requires": { + "colorette": "2.0.19", + "commander": "^10.0.0", + "debug": "4.3.4", + "escalade": "^3.1.1", + "esm": "^3.2.25", + "get-package-type": "^0.1.0", + "getopts": "2.3.0", + "interpret": "^2.2.0", + "lodash": "^4.17.21", + "pg-connection-string": "2.6.1", + "rechoir": "^0.8.0", + "resolve-from": "^5.0.0", + "tarn": "^3.0.2", + "tildify": "2.0.0" + }, + "dependencies": { + "commander": { + "version": "10.0.1", + "resolved": "https://registry.npmjs.org/commander/-/commander-10.0.1.tgz", + "integrity": "sha512-y4Mg2tXshplEbSGzx7amzPwKKOCGuoSRP/CjEdwwk0FOGlUbq6lKuoyDZTNZkmxHdJtp54hdfY/JUrdL7Xfdug==", + "dev": true + }, + "pg-connection-string": { + "version": "2.6.1", + "resolved": "https://registry.npmjs.org/pg-connection-string/-/pg-connection-string-2.6.1.tgz", + "integrity": "sha512-w6ZzNu6oMmIzEAYVw+RLK0+nqHPt8K3ZnknKi+g48Ak2pr3dtljJW3o+D/n2zzCG07Zoe9VOX3aiKpj+BN0pjg==", + "dev": true + }, + "resolve-from": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/resolve-from/-/resolve-from-5.0.0.tgz", + "integrity": "sha512-qYg9KP24dD5qka9J47d0aVky0N+b4fTU89LN9iDnjB5waksiC49rvMB0PrUJQGoTmH50XPiqOvAjDfaijGxYZw==", + "dev": true + } + } + }, "levn": { "version": "0.4.1", "resolved": "https://registry.npmjs.org/levn/-/levn-0.4.1.tgz", @@ -16818,6 +17026,15 @@ "picomatch": "^2.2.1" } }, + "rechoir": { + "version": "0.8.0", + "resolved": "https://registry.npmjs.org/rechoir/-/rechoir-0.8.0.tgz", + "integrity": "sha512-/vxpCXddiX8NGfGO/mTafwjq4aFa/71pvamip0++IQk3zG8cbCj0fifNPrjjF1XMXUne91jL9OoxmdykoEtifQ==", + "dev": true, + "requires": { + "resolve": "^1.20.0" + } + }, "redent": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/redent/-/redent-3.0.0.tgz", @@ -17707,6 +17924,12 @@ "integrity": "sha512-w89qg7PI8wAdvX60bMDP+bFoD5Dvhm9oLheFp5O4a2QF0cSBGsBX4qZmadPMvVqlLJBBci+WqGGOAPvcDeNSVg==", "dev": true }, + "tildify": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/tildify/-/tildify-2.0.0.tgz", + "integrity": "sha512-Cc+OraorugtXNfs50hU9KS369rFXCfgGLpfCfvlc+Ud5u6VWmUQsOAa9HbTvheQdYnrdJqqv1e5oIqXppMYnSw==", + "dev": true + }, "titleize": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/titleize/-/titleize-3.0.0.tgz", diff --git a/package.json b/package.json index 1c51adb8..dafce5f9 100644 --- a/package.json +++ b/package.json @@ -64,6 +64,7 @@ "eslint-plugin-prettier": "^5.0.0", "gts": "^5.0.0", "mssql": "^9.3.2", + "knex": "^3.0.1", "mysql2": "^3.2.0", "nock": "^13.3.0", "pg": "^8.10.0",