Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Implement fail-fast logic for dialect resolution, clean-up code around. * Remove method that was deprecated long time ago * Address additional comments * Try addressing comments * Set client explicitly * Fix compatibility with older Node versions
- Loading branch information
Showing
16 changed files
with
203 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
const { keys } = require('lodash'); | ||
|
||
// The client names we'll allow in the `{name: lib}` pairing. | ||
const CLIENT_ALIASES = Object.freeze({ | ||
pg: 'postgres', | ||
postgresql: 'postgres', | ||
sqlite: 'sqlite3', | ||
}); | ||
|
||
const SUPPORTED_CLIENTS = Object.freeze( | ||
[ | ||
'mssql', | ||
'mysql', | ||
'mysql2', | ||
'oracledb', | ||
'postgres', | ||
'redshift', | ||
'sqlite3', | ||
].concat(keys(CLIENT_ALIASES)) | ||
); | ||
|
||
const POOL_CONFIG_OPTIONS = Object.freeze([ | ||
'maxWaitingClients', | ||
'testOnBorrow', | ||
'fifo', | ||
'priorityRange', | ||
'autostart', | ||
'evictionRunIntervalMillis', | ||
'numTestsPerRun', | ||
'softIdleTimeoutMillis', | ||
'Promise', | ||
]); | ||
|
||
module.exports = { | ||
CLIENT_ALIASES, | ||
SUPPORTED_CLIENTS, | ||
POOL_CONFIG_OPTIONS, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,3 @@ | ||
import Raw from './raw'; | ||
import Client from './client'; | ||
const Knex = require('./knex'); | ||
|
||
import makeKnex from './util/make-knex'; | ||
import parseConnection from './util/parse-connection'; | ||
|
||
import { assign } from 'lodash'; | ||
|
||
// The client names we'll allow in the `{name: lib}` pairing. | ||
const aliases = { | ||
pg: 'postgres', | ||
postgresql: 'postgres', | ||
sqlite: 'sqlite3', | ||
}; | ||
|
||
export default function Knex(config) { | ||
if (typeof config === 'string') { | ||
return new Knex(assign(parseConnection(config), arguments[2])); | ||
} | ||
let Dialect; | ||
if (arguments.length === 0 || (!config.client && !config.dialect)) { | ||
Dialect = Client; | ||
} else if ( | ||
typeof config.client === 'function' && | ||
config.client.prototype instanceof Client | ||
) { | ||
Dialect = config.client; | ||
} else { | ||
const clientName = config.client || config.dialect; | ||
Dialect = require(`./dialects/${aliases[clientName] || | ||
clientName}/index.js`); | ||
} | ||
if (typeof config.connection === 'string') { | ||
config = assign({}, config, { | ||
connection: parseConnection(config.connection).connection, | ||
}); | ||
} | ||
return makeKnex(new Dialect(config)); | ||
} | ||
|
||
// Expose Client on the main Knex namespace. | ||
Knex.Client = Client; | ||
|
||
/* eslint no-console:0 */ | ||
|
||
Object.defineProperties(Knex, { | ||
VERSION: { | ||
get() { | ||
console.warn( | ||
'Knex.VERSION is deprecated, you can get the module version' + | ||
"by running require('knex/package').version" | ||
); | ||
return '0.12.6'; | ||
}, | ||
}, | ||
Promise: { | ||
get() { | ||
console.warn( | ||
`Knex.Promise is deprecated, either require bluebird or use the global Promise` | ||
); | ||
return require('bluebird'); | ||
}, | ||
}, | ||
}); | ||
|
||
// Run a "raw" query, though we can't do anything with it other than put | ||
// it in a query statement. | ||
Knex.raw = (sql, bindings) => { | ||
console.warn( | ||
'global Knex.raw is deprecated, use knex.raw (chain off an initialized knex object)' | ||
); | ||
return new Raw().set(sql, bindings); | ||
}; | ||
export default Knex; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import Raw from './raw'; | ||
import Client from './client'; | ||
|
||
import makeKnex from './util/make-knex'; | ||
import parseConnection from './util/parse-connection'; | ||
import { SUPPORTED_CLIENTS, CLIENT_ALIASES } from './constants'; | ||
|
||
export default function Knex(config) { | ||
// If config is string, try to parse it | ||
if (typeof config === 'string') { | ||
const parsedConfig = Object.assign(parseConnection(config), arguments[2]); | ||
return new Knex(parsedConfig); | ||
} | ||
|
||
let Dialect; | ||
// If user provided no relevant parameters, use generic client | ||
if (arguments.length === 0 || (!config.client && !config.dialect)) { | ||
Dialect = Client; | ||
} | ||
|
||
// If user provided Client constructor as a parameter, use it | ||
else if ( | ||
typeof config.client === 'function' && | ||
config.client.prototype instanceof Client | ||
) { | ||
Dialect = config.client; | ||
} | ||
|
||
// If neither applies, let's assume user specified name of a client or dialect as a string | ||
else { | ||
const clientName = config.client || config.dialect; | ||
if (!SUPPORTED_CLIENTS.includes(clientName)) { | ||
throw new Error( | ||
`knex: Unknown configuration option 'client' value ${clientName}. Note that it is case-sensitive, check documentation for supported values.` | ||
); | ||
} | ||
|
||
Dialect = require(`./dialects/${CLIENT_ALIASES[clientName] || | ||
clientName}/index.js`); | ||
} | ||
|
||
// If config connection parameter is passed as string, try to parse it | ||
if (typeof config.connection === 'string') { | ||
config = Object.assign({}, config, { | ||
connection: parseConnection(config.connection).connection, | ||
}); | ||
} | ||
return makeKnex(new Dialect(config)); | ||
} | ||
|
||
// Expose Client on the main Knex namespace. | ||
Knex.Client = Client; | ||
|
||
/* eslint no-console:0 */ | ||
|
||
Object.defineProperties(Knex, { | ||
Promise: { | ||
get() { | ||
console.warn( | ||
`Knex.Promise is deprecated, either require bluebird or use the global Promise` | ||
); | ||
return require('bluebird'); | ||
}, | ||
}, | ||
}); | ||
|
||
// Run a "raw" query, though we can't do anything with it other than put | ||
// it in a query statement. | ||
Knex.raw = (sql, bindings) => { | ||
console.warn( | ||
'global Knex.raw is deprecated, use knex.raw (chain off an initialized knex object)' | ||
); | ||
return new Raw().set(sql, bindings); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const Knex = require('../../lib/index'); | ||
const { expect } = require('chai'); | ||
|
||
describe('knex', () => { | ||
it('throws error on unsupported config client value', () => { | ||
expect(() => { | ||
Knex({ | ||
client: 'dummy', | ||
}); | ||
}).to.throw( | ||
/Unknown configuration option 'client' value dummy. Note that it is case-sensitive, check documentation for supported values/ | ||
); | ||
}); | ||
|
||
it('accepts supported config client value', () => { | ||
expect(() => { | ||
Knex({ | ||
client: 'mysql', | ||
}); | ||
}).not.to.throw(); | ||
}); | ||
|
||
it('accepts supported config client value alias', () => { | ||
expect(() => { | ||
Knex({ | ||
client: 'sqlite', | ||
}); | ||
}).not.to.throw(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.