forked from ToolJet/ToolJet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase-config-utils.ts
More file actions
143 lines (126 loc) · 5.04 KB
/
database-config-utils.ts
File metadata and controls
143 lines (126 loc) · 5.04 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import * as Joi from 'joi';
import * as path from 'path';
import * as fs from 'fs';
import * as dotenv from 'dotenv';
import { isEmpty } from 'lodash';
const url = require('url');
const querystring = require('querystring');
export function filePathForEnvVars(env: string | undefined): string {
if (env === 'test') {
return path.resolve(process.cwd(), '../.env.test');
} else {
return path.resolve(process.cwd(), '../.env');
}
}
export function getEnvVars() {
let data: any = process.env;
const envVarsFilePath = filePathForEnvVars(process.env.NODE_ENV);
if (fs.existsSync(envVarsFilePath)) {
data = { ...data, ...dotenv.parse(fs.readFileSync(envVarsFilePath)) };
}
data = {
...data,
...((data.DATABASE_URL || data.TOOLJET_DB_URL) && buildDbConfigFromDatabaseURL(data)),
};
return data;
}
function buildDbConfigFromDatabaseURL(data): any {
const config = buildDbConfigFromUrl(data.DATABASE_URL);
const TJDBconfig = buildDbConfigFromUrl(data.TOOLJET_DB_URL);
const { value: dbConfig, error } = validateDatabaseConfig({
DATABASE_URL: data.DATBASE_URL,
PG_HOST: config?.host || data.PG_HOST,
PG_PORT: config?.port || data.PG_PORT,
PG_PASS: config?.password || data.PG_PASS,
PG_USER: config?.user || data.PG_USER,
PG_DB: config?.database || data.PG_DB,
PG_DB_OWNER: data.PG_DB_OWNER,
TOOLJET_DB: TJDBconfig?.database || data.TOOLJET_DB,
TOOLJET_DB_OWNER: data.TOOLJET_DB_OWNER,
TOOLJET_DB_HOST: TJDBconfig?.host || data.TOOLJET_DB_HOST,
TOOLJET_DB_PORT: TJDBconfig?.port || data.TOOLJET_DB_PORT,
TOOLJET_DB_PASS: TJDBconfig?.password || data.TOOLJET_DB_PASS,
TOOLJET_DB_USER: TJDBconfig?.user || data.TOOLJET_DB_USER,
SAMPLE_DB: data.SAMPLE_DB || 'sample_db',
SAMPLE_PG_DB_HOST: config?.host || data.PG_HOST || data.SAMPLE_PG_DB_HOST,
SAMPLE_PG_DB_PORT: config?.port || data.PG_PORT || data.SAMPLE_PG_DB_PORT,
SAMPLE_PG_DB_USER: config?.user || data.PG_USER || data.SAMPLE_PG_DB_USER,
SAMPLE_PG_DB_PASS: config?.password || data.PG_PASS || data.SAMPLE_PG_DB_PASS,
});
if (error) {
throw new Error(`Config validation error: ${error.message}`);
}
return removeEmptyKeys(dbConfig);
}
function buildDbConfigFromUrl(dbURL): any {
let config: any;
if (dbURL) {
const parsedUrl = url.parse(dbURL, false, true);
config = querystring.parse(parsedUrl.query);
config.driver = parsedUrl.protocol.replace(/:$/, '');
if (parsedUrl.auth) {
const userPassword = parsedUrl.auth.split(':', 2);
config.user = userPassword[0];
if (userPassword.length > 1) config.password = userPassword[1];
if (parsedUrl.pathname) config.database = parsedUrl.pathname.replace(/^\//, '').replace(/\/$/, '');
if (parsedUrl.hostname) config.host = parsedUrl.hostname;
if (parsedUrl.port) config.port = parsedUrl.port;
}
}
return config;
}
function removeEmptyKeys(obj) {
return Object.entries(obj)
.filter(([_, v]) => !isEmpty(v))
.reduce((acc, [k, v]) => ({ ...acc, [k]: v }), {});
}
function validateDatabaseConfig(dbConfig: any): Joi.ValidationResult {
const envVarsSchema = Joi.object()
.keys({
PG_HOST: Joi.string().default('localhost'),
PG_PORT: Joi.number().positive().default(5432),
PG_PASS: Joi.string().default(''),
PG_USER: Joi.string().required(),
PG_DB: Joi.string().default('tooljet_production'),
PG_DB_OWNER: Joi.string().default('true'),
TOOLJET_DB_HOST: Joi.string().default('localhost'),
TOOLJET_DB_PORT: Joi.number().positive().default(5432),
TOOLJET_DB_PASS: Joi.string().default(''),
TOOLJET_DB_USER: Joi.string().required(),
TOOLJET_DB: Joi.string().default('tooljet_db'),
TOOLJET_DB_OWNER: Joi.string().default('true'),
...{
SAMPLE_PG_DB_HOST: Joi.string().default('localhost'),
SAMPLE_PG_DB_PORT: Joi.number().positive().default(5432),
SAMPLE_PG_DB_PASS: Joi.string().default(''),
SAMPLE_PG_DB_USER: Joi.string().required(),
SAMPLE_DB: Joi.string().default('sample_db'),
},
})
.unknown();
return envVarsSchema.validate(dbConfig);
}
export function buildAndValidateDatabaseConfig(): Joi.ValidationResult {
const config: any = getEnvVars();
const dbConfig = {
PG_HOST: config.PG_HOST,
PG_PORT: config.PG_PORT,
PG_PASS: config.PG_PASS,
PG_USER: config.PG_USER,
PG_DB: config.PG_DB,
PG_DB_OWNER: config.PG_DB_OWNER,
TOOLJET_DB: config.TOOLJET_DB,
TOOLJET_DB_HOST: config.TOOLJET_DB_HOST,
TOOLJET_DB_PORT: config.TOOLJET_DB_PORT,
TOOLJET_DB_PASS: config.TOOLJET_DB_PASS,
TOOLJET_DB_USER: config.TOOLJET_DB_USER,
TOOLJET_DB_OWNER: config.TOOLJET_DB_OWNER,
ENABLE_SAMPLE_PG_DB: config.ENABLE_SAMPLE_PG_DB,
SAMPLE_DB: config.SAMPLE_DB || 'sample_db',
SAMPLE_PG_DB_HOST: config.SAMPLE_PG_DB_HOST || config.PG_HOST,
SAMPLE_PG_DB_PORT: config.SAMPLE_PG_DB_PORT || config.PG_PORT,
SAMPLE_PG_DB_USER: config.SAMPLE_PG_DB_USER || config.PG_USER,
SAMPLE_PG_DB_PASS: config.SAMPLE_PG_DB_PASS || config.PG_PASS,
};
return validateDatabaseConfig(dbConfig);
}