diff --git a/functions/sql/index.js b/functions/sql/index.js deleted file mode 100644 index 3c14622302..0000000000 --- a/functions/sql/index.js +++ /dev/null @@ -1,115 +0,0 @@ -// Copyright 2018 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 -// -// http://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. - -'use strict'; - -const process = require('process'); // Allow env variable mocking - -// [START functions_sql_mysql] -const mysql = require('mysql'); -// [END functions_sql_mysql] - -// [START functions_sql_postgres] -const pg = require('pg'); -// [END functions_sql_postgres] - -// [START functions_sql_mysql] -// [START functions_sql_postgres] - -/** - * TODO(developer): specify SQL connection details - */ -const connectionName = - process.env.INSTANCE_CONNECTION_NAME || ''; -const dbUser = process.env.SQL_USER || ''; -const dbPassword = process.env.SQL_PASSWORD || ''; -const dbName = process.env.SQL_NAME || ''; - -// [END functions_sql_postgres] -// [END functions_sql_mysql] - -// [START functions_sql_mysql] -const mysqlConfig = { - connectionLimit: 1, - user: dbUser, - password: dbPassword, - database: dbName, -}; -if (process.env.NODE_ENV === 'production') { - mysqlConfig.socketPath = `/cloudsql/${connectionName}`; -} - -// Connection pools reuse connections between invocations, -// and handle dropped or expired connections automatically. -let mysqlPool; - -exports.mysqlDemo = (req, res) => { - // Initialize the pool lazily, in case SQL access isn't needed for this - // GCF instance. Doing so minimizes the number of active SQL connections, - // which helps keep your GCF instances under SQL connection limits. - if (!mysqlPool) { - mysqlPool = mysql.createPool(mysqlConfig); - } - - mysqlPool.query('SELECT NOW() AS now', (err, results) => { - if (err) { - console.error(err); - res.status(500).send(err); - } else { - res.send(JSON.stringify(results)); - } - }); - - // Close any SQL resources that were declared inside this function. - // Keep any declared in global scope (e.g. mysqlPool) for later reuse. -}; -// [END functions_sql_mysql] - -// [START functions_sql_postgres] -const pgConfig = { - max: 1, - user: dbUser, - password: dbPassword, - database: dbName, -}; - -if (process.env.NODE_ENV === 'production') { - pgConfig.host = `/cloudsql/${connectionName}`; -} - -// Connection pools reuse connections between invocations, -// and handle dropped or expired connections automatically. -let pgPool; - -exports.postgresDemo = (req, res) => { - // Initialize the pool lazily, in case SQL access isn't needed for this - // GCF instance. Doing so minimizes the number of active SQL connections, - // which helps keep your GCF instances under SQL connection limits. - if (!pgPool) { - pgPool = new pg.Pool(pgConfig); - } - - pgPool.query('SELECT NOW() as now', (err, results) => { - if (err) { - console.error(err); - res.status(500).send(err); - } else { - res.send(JSON.stringify(results)); - } - }); - - // Close any SQL resources that were declared inside this function. - // Keep any declared in global scope (e.g. mysqlPool) for later reuse. -}; -// [END functions_sql_postgres] diff --git a/functions/sql/package.json b/functions/sql/package.json deleted file mode 100644 index 8b9ab7719c..0000000000 --- a/functions/sql/package.json +++ /dev/null @@ -1,43 +0,0 @@ -{ - "name": "nodejs-docs-samples-functions-sql", - "version": "0.0.1", - "private": true, - "license": "Apache-2.0", - "author": "Google Inc.", - "repository": { - "type": "git", - "url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git" - }, - "engines": { - "node": ">=8.0.0" - }, - "scripts": { - "start-proxy-mysql": "cloud_sql_proxy -instances=$INSTANCE_CONNECTION_NAME-mysql=tcp:3306 &", - "start-proxy-pg": "cloud_sql_proxy -instances=$INSTANCE_CONNECTION_NAME-pg=tcp:5432 &", - "start-proxy": "! pgrep cloud_sql_proxy > /dev/null && npm run start-proxy-pg && npm run start-proxy-mysql || exit 0", - "kill-proxy": "killall cloud_sql_proxy", - "mocha": "mocha test/*.test.js --timeout=20000", - "test": "npm run start-proxy && npm run mocha && npm run kill-proxy" - }, - "dependencies": { - "mysql": "^2.16.0", - "pg": "^8.0.0" - }, - "devDependencies": { - "mocha": "^7.0.0", - "proxyquire": "^2.1.0", - "sinon": "^9.0.0" - }, - "cloud-repo-tools": { - "requiresKeyFile": true, - "requiresProjectId": true, - "requiredEnvVars": [ - "MYSQL_USER", - "MYSQL_PASSWORD", - "MYSQL_DATABASE", - "POSTGRES_USER", - "POSTGRES_PASSWORD", - "POSTGRES_DATABASE" - ] - } -} diff --git a/functions/sql/test/index.test.js b/functions/sql/test/index.test.js deleted file mode 100644 index dc000480a3..0000000000 --- a/functions/sql/test/index.test.js +++ /dev/null @@ -1,87 +0,0 @@ -// Copyright 2018 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 -// -// http://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. - -'use strict'; - -const sinon = require(`sinon`); -const assert = require(`assert`); -const proxyquire = require(`proxyquire`); - -const INSTANCE_PREFIX = `nodejs-docs-samples:us-central1:integration-tests-instance`; - -const getProgram = (env) => { - return proxyquire(`../`, { - process: { - env: env, - }, - }); -}; - -describe('functions_sql_mysql', () => { - it('should query MySQL', async () => { - const program = getProgram({ - INSTANCE_CONNECTION_NAME: `${INSTANCE_PREFIX}-mysql`, - SQL_USER: process.env.MYSQL_USER, - SQL_PASSWORD: process.env.MYSQL_PASSWORD, - SQL_NAME: process.env.MYSQL_DATABASE, - }); - - const resMock = { - status: sinon.stub().returnsThis(), - send: sinon.stub(), - }; - - program.mysqlDemo(null, resMock); - - // Give the query time to complete - await new Promise((resolve) => { - setTimeout(resolve, 1500); - }); - - assert.strictEqual(resMock.status.called, false); - assert.ok(resMock.send.calledOnce); - - const [response] = resMock.send.firstCall.args; - assert.ok(new RegExp(/\d{4}-\d{1,2}-\d{1,2}/).test(response.message)); - }); -}); - -describe('functions_sql_postgres', () => { - it('should query Postgres', async () => { - const program = getProgram({ - INSTANCE_CONNECTION_NAME: `${INSTANCE_PREFIX}-pg`, - SQL_USER: process.env.POSTGRES_USER, - SQL_PASSWORD: process.env.POSTGRES_PASSWORD, - SQL_NAME: process.env.POSTGRES_DATABASE, - }); - - const resMock = { - status: sinon.stub().returnsThis(), - send: sinon.stub(), - }; - - program.postgresDemo(null, resMock); - - // Give the query time to complete - await new Promise((resolve) => { - setTimeout(resolve, 1500); - }); - - assert.strictEqual(resMock.status.called, false); - assert.ok(resMock.send.calledOnce); - - const [response] = resMock.send.firstCall.args; - assert.ok(new RegExp(/\d{4}-\d{1,2}-\d{1,2}/).test(response.message)); - }); -});