()
| 72 | |
| 73 | // Initialize Knex, a Node.js SQL query builder library with built-in connection pooling. |
| 74 | const createPool = async () => { |
| 75 | // Configure which instance and what database user to connect with. |
| 76 | // Remember - storing secrets in plaintext is potentially unsafe. Consider using |
| 77 | // something like https://cloud.google.com/kms/ to help keep secrets secret. |
| 78 | const config = {pool: {}}; |
| 79 | |
| 80 | // [START cloud_sql_postgres_knex_limit] |
| 81 | // 'max' limits the total number of concurrent connections this pool will keep. Ideal |
| 82 | // values for this setting are highly variable on app design, infrastructure, and database. |
| 83 | config.pool.max = 5; |
| 84 | // 'min' is the minimum number of idle connections Knex maintains in the pool. |
| 85 | // Additional connections will be established to meet this value unless the pool is full. |
| 86 | config.pool.min = 5; |
| 87 | // [END cloud_sql_postgres_knex_limit] |
| 88 | |
| 89 | // [START cloud_sql_postgres_knex_timeout] |
| 90 | // 'acquireTimeoutMillis' is the number of milliseconds before a timeout occurs when acquiring a |
| 91 | // connection from the pool. This is slightly different from connectionTimeout, because acquiring |
| 92 | // a pool connection does not always involve making a new connection, and may include multiple retries. |
| 93 | // when making a connection |
| 94 | config.pool.acquireTimeoutMillis = 60000; // 60 seconds |
| 95 | // 'createTimeoutMillis` is the maximum number of milliseconds to wait trying to establish an |
| 96 | // initial connection before retrying. |
| 97 | // After acquireTimeoutMillis has passed, a timeout exception will be thrown. |
| 98 | config.pool.createTimeoutMillis = 30000; // 30 seconds |
| 99 | // 'idleTimeoutMillis' is the number of milliseconds a connection must sit idle in the pool |
| 100 | // and not be checked out before it is automatically closed. |
| 101 | config.pool.idleTimeoutMillis = 600000; // 10 minutes |
| 102 | // [END cloud_sql_postgres_knex_timeout] |
| 103 | |
| 104 | // [START cloud_sql_postgres_knex_backoff] |
| 105 | // 'knex' uses a built-in retry strategy which does not implement backoff. |
| 106 | // 'createRetryIntervalMillis' is how long to idle after failed connection creation before trying again |
| 107 | config.pool.createRetryIntervalMillis = 200; // 0.2 seconds |
| 108 | // [END cloud_sql_postgres_knex_backoff] |
| 109 | |
| 110 | // Check if a Secret Manager secret version is defined |
| 111 | // If a version is defined, retrieve the secret from Secret Manager and set as the DB_PASS |
| 112 | const {CLOUD_SQL_CREDENTIALS_SECRET} = process.env; |
| 113 | if (CLOUD_SQL_CREDENTIALS_SECRET) { |
| 114 | const secrets = await accessSecretVersion(CLOUD_SQL_CREDENTIALS_SECRET); |
| 115 | try { |
| 116 | process.env.DB_PASS = secrets.toString(); |
| 117 | } catch (err) { |
| 118 | err.message = `Unable to parse secret from Secret Manager. Make sure that the secret is JSON formatted: \n ${err.message} `; |
| 119 | throw err; |
| 120 | } |
| 121 | } |
| 122 | if (process.env.INSTANCE_CONNECTION_NAME) { |
| 123 | // Uses the Cloud SQL Node.js Connector when INSTANCE_CONNECTION_NAME |
| 124 | // (e.g., project:region:instance) is defined |
| 125 | if (process.env.DB_IAM_USER) { |
| 126 | // Either a DB_USER or a DB_IAM_USER should be defined. If both are |
| 127 | // defined, DB_IAM_USER takes precedence |
| 128 | return createConnectorIAMAuthnPool(config); |
| 129 | } else { |
| 130 | return createConnectorPool(config); |
| 131 | } |
no test coverage detected