|
1 | 1 | 'use strict'
|
2 | 2 |
|
| 3 | +const NodeURL = require('url') |
| 4 | + |
3 | 5 | /**
|
4 | 6 | * This module manages MySQL connections in serverless applications.
|
5 | 7 | * More detail regarding the MySQL module can be found here:
|
@@ -50,11 +52,43 @@ module.exports = (params) => {
|
50 | 52 | const resetRetries = () => retries = 0
|
51 | 53 | const getErrorCount = () => errors
|
52 | 54 | const getConfig = () => _cfg
|
53 |
| - const config = (args) => Object.assign(_cfg,args) |
| 55 | + const config = (args) => { |
| 56 | + if (typeof args === 'string') { |
| 57 | + return Object.assign(_cfg,uriToConnectionConfig(args)) |
| 58 | + } |
| 59 | + return Object.assign(_cfg,args) |
| 60 | + } |
54 | 61 | const delay = ms => new PromiseLibrary(res => setTimeout(res, ms))
|
55 | 62 | const randRange = (min,max) => Math.floor(Math.random() * (max - min + 1)) + min
|
56 | 63 | const fullJitter = () => randRange(0, Math.min(cap, base * 2 ** retries))
|
57 | 64 | const decorrelatedJitter = (sleep=0) => Math.min(cap, randRange(base, sleep * 3))
|
| 65 | + const uriToConnectionConfig = (connectionString) => { |
| 66 | + let uri = undefined |
| 67 | + |
| 68 | + try { |
| 69 | + uri = new NodeURL.URL(connectionString) |
| 70 | + } catch (error) { |
| 71 | + throw new Error('Invalid data source URL provided') |
| 72 | + } |
| 73 | + |
| 74 | + const extraFields = {} |
| 75 | + |
| 76 | + for (const [name, value] of uri.searchParams) { |
| 77 | + extraFields[name] = value |
| 78 | + } |
| 79 | + |
| 80 | + const database = uri.pathname && uri.pathname.startsWith('/') ? uri.pathname.slice(1) : undefined |
| 81 | + |
| 82 | + const connectionFields = { |
| 83 | + host: uri.hostname ? uri.hostname : undefined, |
| 84 | + user: uri.username ? uri.username : undefined, |
| 85 | + port: uri.port ? Number(uri.port) : undefined, |
| 86 | + password: uri.password ? uri.password : undefined, |
| 87 | + database |
| 88 | + } |
| 89 | + |
| 90 | + return Object.assign(connectionFields, extraFields) |
| 91 | + } |
58 | 92 |
|
59 | 93 |
|
60 | 94 | /********************************************************************/
|
@@ -348,8 +382,7 @@ module.exports = (params) => {
|
348 | 382 | /********************************************************************/
|
349 | 383 | /** INITIALIZATION **/
|
350 | 384 | /********************************************************************/
|
351 |
| - |
352 |
| - let cfg = typeof params === 'object' && !Array.isArray(params) ? params : {} |
| 385 | + const cfg = typeof params === 'object' && !Array.isArray(params) ? params : {} |
353 | 386 |
|
354 | 387 | MYSQL = cfg.library || require('mysql')
|
355 | 388 | PromiseLibrary = cfg.promise || Promise
|
@@ -377,7 +410,14 @@ module.exports = (params) => {
|
377 | 410 | onKill = typeof cfg.onKill === 'function' ? cfg.onKill : () => {}
|
378 | 411 | onKillError = typeof cfg.onKillError === 'function' ? cfg.onKillError : () => {}
|
379 | 412 |
|
380 |
| - let connCfg = typeof cfg.config === 'object' && !Array.isArray(cfg.config) ? cfg.config : {} |
| 413 | + let connCfg = {} |
| 414 | + |
| 415 | + if (typeof cfg.config === 'object' && !Array.isArray(cfg.config)) { |
| 416 | + connCfg = cfg.config |
| 417 | + } else if (typeof params === 'string') { |
| 418 | + connCfg = params |
| 419 | + } |
| 420 | + |
381 | 421 | let escape = MYSQL.escape
|
382 | 422 | // Set MySQL configs
|
383 | 423 | config(connCfg)
|
|
0 commit comments