Skip to content

Instantly share code, notes, and snippets.

@francoisauclair911
Last active January 17, 2019 20:05
Show Gist options
  • Save francoisauclair911/f98ff28a799928770d7564f485904727 to your computer and use it in GitHub Desktop.
Save francoisauclair911/f98ff28a799928770d7564f485904727 to your computer and use it in GitHub Desktop.
const mysql = require('mysql');
/**
* TODO(developer): specify SQL connection details
*/
const connectionName =
process.env.INSTANCE_CONNECTION_NAME || 'live-attendance-d001e:us-east1:test';
const dbUser = process.env.SQL_USER || 'myusername';
const dbPassword = process.env.SQL_PASSWORD || 'mypassword';
const dbName = process.env.SQL_NAME || 'mydatabase';
const mysqlConfig = {
socketPath: '/cloudsql/live-attendance-d001e:us-east1:test',
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(`INSERT INTO stats (email, referrals, source) values ("${req.body.email}", 1, "${req.body.source}")`, (err, results) => {
if (err) {
console.error(err);
res.set('Access-Control-Allow-Origin', "*")
res.set('Access-Control-Allow-Methods', 'GET, POST')
res.set('Access-Control-Allow-Headers', 'Content-Type');
res.status(500).send(err);
} else {
res.set('Access-Control-Allow-Origin', "*")
res.set('Access-Control-Allow-Methods', 'GET, POST')
res.set('Access-Control-Allow-Headers', 'Content-Type');
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.
};
/*
const mysql = require('mysql');
// const cors = require('cors');
// cors({origin: true});
let connection = mysql.createConnection({
socketPath: '/cloudsql/live-attendance-d001e:us-east1:test',
host: '35.237.10.61',
user: 'francoboy7',
database: 'test_table',
password: 'root'
});
exports.mysqlDemo = (req, res) => {
connection.connect(function(err) {
if (err) {
console.error('Error connecting: ' + err.stack);
return;
}
console.log('Connected as thread id: ' + connection.threadId);
});
connection.query(
`INSERT INTO stats (email, referrals, source) values ("${req.body.email}", 1, "${req.body.source}")`,
function(error, results, fields) {
if (error) throw error;
res.set('Access-Control-Allow-Origin', "*")
res.set('Access-Control-Allow-Methods', 'GET, POST')
res.set('Access-Control-Allow-Headers', 'Content-Type');
res.send(results);
}
);
connection.end();
};
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment