Created
January 9, 2020 15:37
-
-
Save grant/5b841fbbff9775228d56ba27c6727830 to your computer and use it in GitHub Desktop.
Cloud Run Storage – Cloud SQL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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