MCPcopy Create free account
hub / github.com/GoogleCloudPlatform/nodejs-docs-samples / createPool

Function createPool

cloud-sql/mysql/mysql/index.js:54–106  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

52}
53
54const createPool = async () => {
55 const config = {
56 // [START cloud_sql_mysql_mysql_limit]
57 // 'connectionLimit' is the maximum number of connections the pool is allowed
58 // to keep at once.
59 connectionLimit: 5,
60 // [END cloud_sql_mysql_mysql_limit]
61
62 // [START cloud_sql_mysql_mysql_timeout]
63 // 'connectTimeout' is the maximum number of milliseconds before a timeout
64 // occurs during the initial connection to the database.
65 connectTimeout: 10000, // 10 seconds
66 // 'acquireTimeout' is the maximum number of milliseconds to wait when
67 // checking out a connection from the pool before a timeout error occurs.
68 acquireTimeout: 10000, // 10 seconds
69 // 'waitForConnections' determines the pool's action when no connections are
70 // free. If true, the request will queued and a connection will be presented
71 // when ready. If false, the pool will call back with an error.
72 waitForConnections: true, // Default: true
73 // 'queueLimit' is the maximum number of requests for connections the pool
74 // will queue at once before returning an error. If 0, there is no limit.
75 queueLimit: 0, // Default: 0
76 // [END cloud_sql_mysql_mysql_timeout]
77
78 // [START cloud_sql_mysql_mysql_backoff]
79 // The mysql module automatically uses exponential delays between failed
80 // connection attempts.
81 // [END cloud_sql_mysql_mysql_backoff]
82 };
83
84 // Check if a Secret Manager secret version is defined
85 // If a version is defined, retrieve the secret from Secret Manager and set as the DB_PASS
86 const {CLOUD_SQL_CREDENTIALS_SECRET} = process.env;
87 if (CLOUD_SQL_CREDENTIALS_SECRET) {
88 const secrets = await accessSecretVersion(CLOUD_SQL_CREDENTIALS_SECRET);
89 try {
90 process.env.DB_PASS = secrets.toString();
91 } catch (err) {
92 err.message = `Unable to parse secret from Secret Manager. Make sure that the secret is JSON formatted: \n ${err.message} `;
93 throw err;
94 }
95 }
96
97 if (process.env.INSTANCE_HOST) {
98 // Use a TCP socket when INSTANCE_HOST (e.g., 127.0.0.1) is defined
99 return createTcpPool(config);
100 } else if (process.env.INSTANCE_UNIX_SOCKET) {
101 // Use a Unix socket when INSTANCE_UNIX_SOCKET (e.g., /cloudsql/proj:region:instance) is defined.
102 return createUnixSocketPool(config);
103 } else {
104 throw 'Set either the `INSTANCE_HOST` or `INSTANCE_UNIX_SOCKET` environment variable.';
105 }
106};
107
108const ensureSchema = async pool => {
109 // Wait for tables to be created (if they don't already exist).

Callers 1

Calls 4

toStringMethod · 0.80
accessSecretVersionFunction · 0.70
createTcpPoolFunction · 0.70
createUnixSocketPoolFunction · 0.70

Tested by

no test coverage detected