/
DriverUtils.ts
99 lines (88 loc) · 3.65 KB
/
DriverUtils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { Driver } from "./Driver";
import { hash } from "../util/StringUtils";
/**
* Common driver utility functions.
*/
export class DriverUtils {
// -------------------------------------------------------------------------
// Public Static Methods
// -------------------------------------------------------------------------
/**
* Normalizes and builds a new driver options.
* Extracts settings from connection url and sets to a new options object.
*/
static buildDriverOptions(options: any, buildOptions?: { useSid: boolean }): any {
if (options.url) {
const parsedUrl = this.parseConnectionUrl(options.url);
let urlDriverOptions: any = {
type: parsedUrl.type,
host: parsedUrl.host,
username: parsedUrl.username,
password: parsedUrl.password,
port: parsedUrl.port,
database: parsedUrl.database
};
if (buildOptions && buildOptions.useSid) {
urlDriverOptions.sid = parsedUrl.database;
}
return Object.assign({}, urlDriverOptions, options);
}
return Object.assign({}, options);
}
/**
* Builds column alias from given alias name and column name.
*
* If alias length is greater than the limit (if any) allowed by the current
* driver, replaces it with a hashed string.
*
* @param driver Current `Driver`.
* @param alias Alias part.
* @param column Name of the column to be concatened to `alias`.
*
* @return An alias allowing to select/transform the target `column`.
*/
static buildColumnAlias({ maxAliasLength }: Driver, alias: string, column: string): string {
const columnAliasName = alias + "_" + column;
if (maxAliasLength && maxAliasLength > 0 && columnAliasName.length > maxAliasLength) {
return hash(columnAliasName, { length: maxAliasLength });
}
return columnAliasName;
}
// -------------------------------------------------------------------------
// Private Static Methods
// -------------------------------------------------------------------------
/**
* Extracts connection data from the connection url.
*/
private static parseConnectionUrl(url: string) {
const type = url.split(":")[0];
const firstSlashes = url.indexOf("//");
const preBase = url.substr(firstSlashes + 2);
const secondSlash = preBase.indexOf("/");
const base = (secondSlash !== -1) ? preBase.substr(0, secondSlash) : preBase;
let afterBase = (secondSlash !== -1) ? preBase.substr(secondSlash + 1) : undefined;
// remove mongodb query params
if (afterBase && afterBase.indexOf("?") !== -1) {
afterBase = afterBase.substr(0, afterBase.indexOf("?"));
}
const lastAtSign = base.lastIndexOf("@");
const usernameAndPassword = base.substr(0, lastAtSign);
const hostAndPort = base.substr(lastAtSign + 1);
let username = usernameAndPassword;
let password = "";
const firstColon = usernameAndPassword.indexOf(":");
if (firstColon !== -1) {
username = usernameAndPassword.substr(0, firstColon);
password = usernameAndPassword.substr(firstColon + 1);
}
const [host, port] = hostAndPort.split(":");
return {
type: type,
host: host,
username: decodeURIComponent(username),
password: decodeURIComponent(password),
port: port ? parseInt(port) : undefined,
database: afterBase || undefined
};
}
}