Skip to content

Instantly share code, notes, and snippets.

@grant
Created January 9, 2020 15:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grant/5b841fbbff9775228d56ba27c6727830 to your computer and use it in GitHub Desktop.
Save grant/5b841fbbff9775228d56ba27c6727830 to your computer and use it in GitHub Desktop.
Cloud Run Storage – Cloud SQL
const mysql = require('promise-mysql');
// Create a pooled connection to MySQL
let pool;
const createPool = async () => {
pool = await mysql.createPool({
user: process.env.DB_USER, // e.g. 'my-db-user'
password: process.env.DB_PASS, // e.g. 'my-db-password'
database: process.env.DB_NAME, // e.g. 'my-database'
// If connecting via unix domain socket, specify the path
socketPath: `/cloudsql/${process.env.CLOUD_SQL_CONNECTION_NAME}`,
// If connecting via TCP, enter the IP and port instead
// host: 'localhost',
// port: 3306,
//...
});
};
createPool();
async function readCustomers(req, res) {
// Get the 5 newest customers.
const customers = await pool.query(
'SELECT id, name FROM customers ORDER BY id DESC LIMIT 5'
);
res.send({ customers });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment