1
import {Driver} from "./Driver";
2 5
import { hash } from "../util/StringUtils";
3

4
/**
5
 * Common driver utility functions.
6
 */
7 5
export class DriverUtils {
8

9
    // -------------------------------------------------------------------------
10
    // Public Static Methods
11
    // -------------------------------------------------------------------------
12

13
    /**
14
     * Normalizes and builds a new driver options.
15
     * Extracts settings from connection url and sets to a new options object.
16
     */
17 5
    static buildDriverOptions(options: any, buildOptions?: { useSid: boolean }): any {
18 5
        if (options.url) {
19 5
            const urlDriverOptions = this.parseConnectionUrl(options.url) as { [key: string]: any };
20

21 5
            if (buildOptions && buildOptions.useSid && urlDriverOptions.database) {
22 0
                urlDriverOptions.sid = urlDriverOptions.database;
23
            }
24

25 5
            for (const key of Object.keys(urlDriverOptions)) {
26 5
                if (typeof urlDriverOptions[key] === "undefined") {
27 3
                    delete urlDriverOptions[key];
28
                }
29
            }
30

31 5
            return Object.assign({}, options, urlDriverOptions);
32
        }
33 5
        return Object.assign({}, options);
34
    }
35

36
    /**
37
     * Builds column alias from given alias name and column name.
38
     *
39
     * If alias length is greater than the limit (if any) allowed by the current
40
     * driver, replaces it with a hashed string.
41
     *
42
     * @param driver Current `Driver`.
43
     * @param alias Alias part.
44
     * @param column Name of the column to be concatened to `alias`.
45
     *
46
     * @return An alias allowing to select/transform the target `column`.
47
     */
48 5
    static buildColumnAlias({ maxAliasLength }: Driver, alias: string, column: string): string {
49 5
        const columnAliasName = alias + "_" + column;
50

51 5
        if (maxAliasLength && maxAliasLength > 0 && columnAliasName.length > maxAliasLength) {
52 4
            return hash(columnAliasName, { length: maxAliasLength });
53
        }
54

55 5
        return columnAliasName;
56
    }
57

58
    // -------------------------------------------------------------------------
59
    // Private Static Methods
60
    // -------------------------------------------------------------------------
61

62
    /**
63
     * Extracts connection data from the connection url.
64
     */
65 5
    private static parseConnectionUrl(url: string) {
66 5
        const type = url.split(":")[0];
67 5
        const firstSlashes = url.indexOf("//");
68 5
        const preBase = url.substr(firstSlashes + 2);
69 5
        const secondSlash = preBase.indexOf("/");
70 5
        const base = (secondSlash !== -1) ? preBase.substr(0, secondSlash) : preBase;
71 5
        let afterBase = (secondSlash !== -1) ? preBase.substr(secondSlash + 1) : undefined;
72
        // remove mongodb query params
73 5
        if (afterBase && afterBase.indexOf("?") !== -1) {
74 5
            afterBase = afterBase.substr(0, afterBase.indexOf("?"));
75
        }
76

77 5
        const lastAtSign = base.lastIndexOf("@");
78 5
        const usernameAndPassword = base.substr(0, lastAtSign);
79 5
        const hostAndPort = base.substr(lastAtSign + 1);
80

81 5
        let username = usernameAndPassword;
82 5
        let password = "";
83 5
        const firstColon = usernameAndPassword.indexOf(":");
84 5
        if (firstColon !== -1) {
85 5
            username = usernameAndPassword.substr(0, firstColon);
86 5
            password = usernameAndPassword.substr(firstColon + 1);
87
        }
88 5
        const [host, port] = hostAndPort.split(":");
89

90 5
        return {
91
            type: type,
92
            host: host,
93
            username: decodeURIComponent(username),
94
            password: decodeURIComponent(password),
95 5
            port: port ? parseInt(port) : undefined,
96 5
            database: afterBase || undefined
97
        };
98
    }
99 5
}

Read our documentation on viewing source code .

Loading