()
| 52 | } |
| 53 | |
| 54 | const createPool = async () => { |
| 55 | const config = {pool: {}}; |
| 56 | // [START cloud_sql_sqlserver_mssql_timeout] |
| 57 | // 'connectionTimeout` is the maximum number of milliseconds to wait trying to establish an |
| 58 | // initial connection. After the specified amount of time, an exception will be thrown. |
| 59 | config.connectionTimeout = 30000; |
| 60 | // 'acquireTimeoutMillis' is the number of milliseconds before a timeout occurs when acquiring a |
| 61 | // connection from the pool. |
| 62 | config.pool.acquireTimeoutMillis = 30000; |
| 63 | // 'idleTimeoutMillis' is the number of milliseconds a connection must sit idle in the pool |
| 64 | // and not be checked out before it is automatically closed |
| 65 | (config.pool.idleTimeoutMillis = 600000), |
| 66 | // [END cloud_sql_sqlserver_mssql_timeout] |
| 67 | // [START cloud_sql_sqlserver_mssql_limit] |
| 68 | // 'max' limits the total number of concurrent connections this pool will keep. Ideal |
| 69 | // values for this setting are highly variable on app design, infrastructure, and database. |
| 70 | (config.pool.max = 5); |
| 71 | // 'min' is the minimum number of idle connections maintained in the pool. |
| 72 | // Additional connections will be established to meet this value unless the pool is full. |
| 73 | config.pool.min = 1; |
| 74 | // [END cloud_sql_sqlserver_mssql_limit] |
| 75 | |
| 76 | // [START cloud_sql_sqlserver_mssql_backoff] |
| 77 | // The node-mssql module uses a built-in retry strategy which does not implement backoff. |
| 78 | // 'createRetryIntervalMillis' is the number of milliseconds to wait in between retries. |
| 79 | config.pool.createRetryIntervalMillis = 200; |
| 80 | // [END cloud_sql_sqlserver_mssql_backoff] |
| 81 | |
| 82 | // Check if a Secret Manager secret version is defined |
| 83 | // If a version is defined, retrieve the secret from Secret Manager and set as the DB_PASS |
| 84 | const {CLOUD_SQL_CREDENTIALS_SECRET} = process.env; |
| 85 | if (CLOUD_SQL_CREDENTIALS_SECRET) { |
| 86 | const secrets = await accessSecretVersion(CLOUD_SQL_CREDENTIALS_SECRET); |
| 87 | try { |
| 88 | process.env.DB_PASS = secrets.toString(); |
| 89 | } catch (err) { |
| 90 | err.message = `Unable to parse secret from Secret Manager. Make sure that the secret is JSON formatted: \n ${err.message} `; |
| 91 | throw err; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | return createTcpPool(config); |
| 96 | }; |
| 97 | |
| 98 | const ensureSchema = async pool => { |
| 99 | // Wait for tables to be created (if they don't already exist). |
no test coverage detected