1 | const Raw = require('./raw');
|
2 | const Client = require('./client');
|
3 | const QueryBuilder = require('./query/builder');
|
4 | const QueryInterface = require('./query/methods');
|
5 |
|
6 | const makeKnex = require('./util/make-knex');
|
7 | const parseConnection = require('./util/parse-connection');
|
8 | const fakeClient = require('./util/fake-client');
|
9 | const { SUPPORTED_CLIENTS } = require('./constants');
|
10 | const { resolveClientNameWithAliases } = require('./helpers');
|
11 |
|
12 | function Knex(config) {
|
13 |
|
14 | if (typeof config === 'string') {
|
15 | const parsedConfig = Object.assign(parseConnection(config), arguments[2]);
|
16 | return new Knex(parsedConfig);
|
17 | }
|
18 |
|
19 | let Dialect;
|
20 |
|
21 | if (arguments.length === 0 || (!config.client && !config.dialect)) {
|
22 | Dialect = Client;
|
23 | }
|
24 |
|
25 |
|
26 | else if (
|
27 | typeof config.client === 'function' &&
|
28 | config.client.prototype instanceof Client
|
29 | ) {
|
30 | Dialect = config.client;
|
31 | }
|
32 |
|
33 |
|
34 | else {
|
35 | const clientName = config.client || config.dialect;
|
36 | if (!SUPPORTED_CLIENTS.includes(clientName)) {
|
37 | throw new Error(
|
38 | `knex: Unknown configuration option 'client' value ${clientName}. Note that it is case-sensitive, check documentation for supported values.`
|
39 | );
|
40 | }
|
41 |
|
42 | const resolvedClientName = resolveClientNameWithAliases(clientName);
|
43 | Dialect = require(`./dialects/${resolvedClientName}/index.js`);
|
44 | }
|
45 |
|
46 |
|
47 | if (typeof config.connection === 'string') {
|
48 | config = Object.assign({}, config, {
|
49 | connection: parseConnection(config.connection).connection,
|
50 | });
|
51 | }
|
52 | const newKnex = makeKnex(new Dialect(config));
|
53 | if (config.userParams) {
|
54 | newKnex.userParams = config.userParams;
|
55 | }
|
56 | return newKnex;
|
57 | }
|
58 |
|
59 |
|
60 | Knex.Client = Client;
|
61 | Knex.QueryBuilder = {
|
62 | extend: function(methodName, fn) {
|
63 | QueryBuilder.extend(methodName, fn);
|
64 | QueryInterface.push(methodName);
|
65 | },
|
66 | };
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 | Knex.raw = (sql, bindings) => {
|
73 | console.warn(
|
74 | 'global Knex.raw is deprecated, use knex.raw (chain off an initialized knex object)'
|
75 | );
|
76 | return new Raw(fakeClient).set(sql, bindings);
|
77 | };
|
78 |
|
79 | module.exports = Knex;
|