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