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