Total Complexity | 4 |
Complexity/F | 0 |
Lines of Code | 67 |
Function Count | 0 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | import fs from 'fs' |
||
2 | import Mariadb from '../services/mariadb' |
||
3 | import Mysql from '../services/mysql' |
||
4 | import Mysql57 from '../services/mysql57' |
||
5 | import Mysql80 from '../services/mysql80' |
||
6 | |||
7 | const supportedDatabases: string[] = [ |
||
8 | (new Mysql80).service, |
||
9 | (new Mysql57).service, |
||
10 | (new Mariadb).service |
||
11 | ] |
||
12 | |||
13 | /** |
||
14 | * Get the Database object by the name of the service. |
||
15 | * @param databaseType |
||
16 | */ |
||
17 | const getDatabaseByName = (databaseType: string): Mysql => { |
||
18 | let database: Mysql |
||
19 | |||
20 | switch (databaseType) { |
||
21 | case (new Mysql80).service: |
||
22 | database = new Mysql80() |
||
23 | break |
||
24 | case (new Mysql57).service: |
||
25 | database = new Mysql57() |
||
26 | break |
||
27 | case (new Mariadb).service: |
||
28 | database = new Mariadb() |
||
29 | break |
||
30 | default: |
||
31 | throw Error('Invalid database type version: ' + databaseType) |
||
32 | } |
||
33 | |||
34 | return database |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * Get the currently linked Mysql binary. |
||
39 | */ |
||
40 | const getLinkedDatabase = async (): Promise<Mysql> => { |
||
41 | const mysqlLink = await fs.lstatSync('/usr/local/bin/mysql') |
||
42 | |||
43 | if (!mysqlLink.isSymbolicLink()) { |
||
44 | throw Error('Mysql executable is not found.') |
||
45 | } |
||
46 | |||
47 | const mysqlBinary = await fs.realpathSync('/usr/local/bin/mysql') |
||
48 | |||
49 | let linkedDatabase: Mysql | undefined |
||
50 | |||
51 | supportedDatabases.forEach((versionName) => { |
||
52 | if (mysqlBinary.includes(versionName)) { |
||
53 | linkedDatabase = getDatabaseByName(versionName) |
||
54 | } |
||
55 | }) |
||
56 | |||
57 | if (linkedDatabase) { |
||
58 | return linkedDatabase |
||
59 | } else { |
||
60 | throw Error('Unable to determine linked database') |
||
61 | } |
||
62 | } |
||
63 | |||
64 | export { |
||
65 | getDatabaseByName, |
||
66 | getLinkedDatabase |
||
67 | } |