* Instantiate sequelize with name of database, username and password. * * @example * // without password / with blank password * const sequelize = new Sequelize('database', 'username', null, { * dialect: 'mysql' * }) * * // with password and options * const sequelize = n
(database, username, password, options)
| 183 | * @param {boolean} [options.logQueryParameters=false] A flag that defines if show bind parameters in log. |
| 184 | */ |
| 185 | constructor(database, username, password, options) { |
| 186 | let config; |
| 187 | |
| 188 | if (arguments.length === 1 && typeof database === 'object') { |
| 189 | // new Sequelize({ ... options }) |
| 190 | options = database; |
| 191 | config = _.pick(options, 'host', 'port', 'database', 'username', 'password'); |
| 192 | } else if (arguments.length === 1 && typeof database === 'string' || arguments.length === 2 && typeof username === 'object') { |
| 193 | // new Sequelize(URI, { ... options }) |
| 194 | |
| 195 | config = {}; |
| 196 | options = username || {}; |
| 197 | |
| 198 | const urlParts = url.parse(arguments[0], true); |
| 199 | |
| 200 | options.dialect = urlParts.protocol.replace(/:$/, ''); |
| 201 | options.host = urlParts.hostname; |
| 202 | |
| 203 | if (options.dialect === 'sqlite' && urlParts.pathname && !urlParts.pathname.startsWith('/:memory')) { |
| 204 | const storagePath = path.join(options.host, urlParts.pathname); |
| 205 | options.storage = path.resolve(options.storage || storagePath); |
| 206 | } |
| 207 | |
| 208 | if (urlParts.pathname) { |
| 209 | config.database = urlParts.pathname.replace(/^\//, ''); |
| 210 | } |
| 211 | |
| 212 | if (urlParts.port) { |
| 213 | options.port = urlParts.port; |
| 214 | } |
| 215 | |
| 216 | if (urlParts.auth) { |
| 217 | const authParts = urlParts.auth.split(':'); |
| 218 | |
| 219 | config.username = authParts[0]; |
| 220 | |
| 221 | if (authParts.length > 1) |
| 222 | config.password = authParts.slice(1).join(':'); |
| 223 | } |
| 224 | |
| 225 | if (urlParts.query) { |
| 226 | // Allow host query argument to override the url host. |
| 227 | // Enables specifying domain socket hosts which cannot be specified via the typical |
| 228 | // host part of a url. |
| 229 | if (urlParts.query.host) { |
| 230 | options.host = urlParts.query.host; |
| 231 | } |
| 232 | |
| 233 | if (options.dialectOptions) { |
| 234 | Object.assign(options.dialectOptions, urlParts.query); |
| 235 | } else { |
| 236 | options.dialectOptions = urlParts.query; |
| 237 | if (urlParts.query.options) { |
| 238 | try { |
| 239 | const o = JSON.parse(urlParts.query.options); |
| 240 | options.dialectOptions.options = o; |
| 241 | } catch (e) { |
| 242 | // Nothing to do, string is not a valid JSON |
nothing calls this directly
no test coverage detected