-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathknexfile-external.ts
More file actions
58 lines (50 loc) · 1.58 KB
/
knexfile-external.ts
File metadata and controls
58 lines (50 loc) · 1.58 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
import type { Knex } from "knex";
import dotenv from "dotenv";
import { types } from "pg";
dotenv.config({ path: ".env.wasabi" });
types.setTypeParser(types.builtins.DATE, (val) => val);
types.setTypeParser(types.builtins.INT8, "text", parseInt);
types.setTypeParser(types.builtins.NUMERIC, Number);
interface Configs {
[key: string]: Knex.Config;
}
// Function to create a dynamic Knex configuration for a given database
const createKnexConfig = (dbName: string): Knex.Config => ({
client: "postgresql",
connection: {
host: process.env.DATA_DB_HOST,
user: process.env.DATA_DB_USER,
password: process.env.DATA_DB_PASSWORD,
database: dbName,
port: parseInt(process.env.DATA_DB_PORT || "", 10) || 5432,
},
pool: {
min: 0,
max: 10,
},
});
// Get an array of database names from the DATABASE_NAMES environment variable
const databaseNames = (process.env.DATA_DB_NAMES || "").split(",");
// Generate dynamic configurations for each database name
const externalConfigs: Configs = databaseNames.reduce(
(configs: Configs, dbName: string) => {
configs[dbName] = createKnexConfig(dbName);
return configs;
},
{}
);
externalConfigs[process.env.DAPP_ANALYTICS_DB_NAME] = {
client: "postgresql",
connection: {
host: process.env.DAPP_ANALYTICS_DB_HOST,
user: process.env.DAPP_ANALYTICS_DB_USER,
password: process.env.DAPP_ANALYTICS_DB_PASSWORD,
database: process.env.DAPP_ANALYTICS_DB_NAME,
port: parseInt(process.env.DAPP_ANALYTICS_DB_PORT || "", 10) || 5432,
},
pool: {
min: 0,
max: 10,
},
};
export default externalConfigs;