2016-05-17 01:01:34 +10:00
|
|
|
import Raw from './raw';
|
|
|
|
import Client from './client';
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
import makeKnex from './util/make-knex';
|
|
|
|
import parseConnection from './util/parse-connection';
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2018-07-09 08:10:34 -04:00
|
|
|
import { assign } from 'lodash';
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
// The client names we'll allow in the `{name: lib}` pairing.
|
|
|
|
const aliases = {
|
2018-07-09 08:10:34 -04:00
|
|
|
pg: 'postgres',
|
|
|
|
postgresql: 'postgres',
|
|
|
|
sqlite: 'sqlite3',
|
2016-05-17 01:01:34 +10:00
|
|
|
};
|
|
|
|
|
|
|
|
export default function Knex(config) {
|
2016-03-02 17:07:05 +01:00
|
|
|
if (typeof config === 'string') {
|
2018-07-09 08:10:34 -04:00
|
|
|
return new Knex(assign(parseConnection(config), arguments[2]));
|
2016-03-02 17:07:05 +01:00
|
|
|
}
|
2016-05-17 01:01:34 +10:00
|
|
|
let Dialect;
|
2016-03-02 17:07:05 +01:00
|
|
|
if (arguments.length === 0 || (!config.client && !config.dialect)) {
|
2018-07-09 08:10:34 -04:00
|
|
|
Dialect = Client;
|
|
|
|
} else if (
|
|
|
|
typeof config.client === 'function' &&
|
|
|
|
config.client.prototype instanceof Client
|
|
|
|
) {
|
|
|
|
Dialect = config.client;
|
2016-03-02 17:07:05 +01:00
|
|
|
} else {
|
2018-06-18 21:36:02 +02:00
|
|
|
const clientName = config.client || config.dialect;
|
2018-07-09 08:10:34 -04:00
|
|
|
Dialect = require(`./dialects/${aliases[clientName] ||
|
|
|
|
clientName}/index.js`);
|
2016-03-02 17:07:05 +01:00
|
|
|
}
|
|
|
|
if (typeof config.connection === 'string') {
|
2018-07-09 08:10:34 -04:00
|
|
|
config = assign({}, config, {
|
|
|
|
connection: parseConnection(config.connection).connection,
|
|
|
|
});
|
2016-03-02 17:07:05 +01:00
|
|
|
}
|
2018-07-09 08:10:34 -04:00
|
|
|
return makeKnex(new Dialect(config));
|
2016-03-02 17:07:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Expose Client on the main Knex namespace.
|
2018-06-18 21:36:02 +02:00
|
|
|
Knex.Client = Client;
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2018-05-29 17:42:03 +02:00
|
|
|
/* eslint no-console:0 */
|
|
|
|
|
2016-09-13 08:19:47 -04:00
|
|
|
Object.defineProperties(Knex, {
|
|
|
|
VERSION: {
|
|
|
|
get() {
|
2018-05-29 17:42:03 +02:00
|
|
|
console.warn(
|
2016-09-13 08:19:47 -04:00
|
|
|
'Knex.VERSION is deprecated, you can get the module version' +
|
2018-07-09 08:10:34 -04:00
|
|
|
"by running require('knex/package').version"
|
2018-06-18 21:36:02 +02:00
|
|
|
);
|
2018-07-09 08:10:34 -04:00
|
|
|
return '0.12.6';
|
|
|
|
},
|
2016-09-13 08:19:47 -04:00
|
|
|
},
|
|
|
|
Promise: {
|
|
|
|
get() {
|
2018-07-09 08:10:34 -04:00
|
|
|
console.warn(
|
|
|
|
`Knex.Promise is deprecated, either require bluebird or use the global Promise`
|
|
|
|
);
|
|
|
|
return require('bluebird');
|
|
|
|
},
|
|
|
|
},
|
2018-06-18 21:36:02 +02:00
|
|
|
});
|
2016-05-19 11:42:08 +03:00
|
|
|
|
2016-03-02 17:07:05 +01:00
|
|
|
// Run a "raw" query, though we can't do anything with it other than put
|
|
|
|
// it in a query statement.
|
2016-09-12 18:01:47 -04:00
|
|
|
Knex.raw = (sql, bindings) => {
|
2018-07-09 08:10:34 -04:00
|
|
|
console.warn(
|
|
|
|
'global Knex.raw is deprecated, use knex.raw (chain off an initialized knex object)'
|
|
|
|
);
|
|
|
|
return new Raw().set(sql, bindings);
|
2018-06-18 21:36:02 +02:00
|
|
|
};
|