knex/src/index.js

68 lines
1.9 KiB
JavaScript
Raw Normal View History

2016-03-02 17:07:05 +01:00
import Raw from './raw';
import { warn } from './helpers';
import Client from './client';
2016-03-02 17:07:05 +01:00
import makeClient from './util/make-client';
import makeKnex from './util/make-knex';
import parseConnection from './util/parse-connection';
2016-03-02 17:07:05 +01:00
import { assign } from 'lodash'
2016-03-02 17:07:05 +01:00
// The client names we'll allow in the `{name: lib}` pairing.
const aliases = {
2016-09-12 18:45:35 -04:00
'mariadb' : 'maria',
'mariasql' : 'maria',
'pg' : 'postgres',
'postgresql' : 'postgres',
'sqlite' : 'sqlite3'
};
export default function Knex(config) {
2016-03-02 17:07:05 +01:00
if (typeof config === 'string') {
return new Knex(assign(parseConnection(config), arguments[2]))
}
let Dialect;
2016-03-02 17:07:05 +01:00
if (arguments.length === 0 || (!config.client && !config.dialect)) {
Dialect = makeClient(Client)
2016-05-19 13:20:45 +03:00
} else if (typeof config.client === 'function' && config.client.prototype instanceof Client) {
2016-05-19 08:49:07 +03:00
Dialect = makeClient(config.client)
2016-03-02 17:07:05 +01:00
} else {
const clientName = config.client || config.dialect
Dialect = makeClient(require(`./dialects/${aliases[clientName] || clientName}/index.js`))
2016-03-02 17:07:05 +01:00
}
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
2016-09-12 18:11:56 -04:00
Object.defineProperty(Knex, 'VERSION', {
get() {
warn(
'Knex.VERSION is deprecated, you can get the module version' +
"by running require('knex/package').version"
)
return '0.12.0'
}
})
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.
Knex.raw = (sql, bindings) => {
warn('global Knex.raw is deprecated, use knex.raw (chain off an initialized knex object)')
return new Raw().set(sql, bindings)
2016-03-02 17:07:05 +01:00
}
// Bluebird
Knex.Promise = require('bluebird')
2016-03-02 17:07:05 +01:00
// Doing this ensures Browserify works. Still need to figure out
// the best way to do some of this.
if (process.browser) {
require('./dialects/websql/index.js')
}