1 import { ModulesSdkTypes } from "@medusajs/types"
2 import { MedusaError } from "../common"
3
4 function getEnv(key: string, moduleName: string): string {
5 const value =
6 process.env[`${moduleName.toUpperCase()}_${key}`] ??
7 process.env[`MEDUSA_${key}`] ??
8 process.env[`${key}`]
9 return value ?? ""
10 }
11
12 function isModuleServiceInitializeOptions(
13 obj: unknown
14 ): obj is ModulesSdkTypes.ModuleServiceInitializeOptions {
15 return !!(obj as any)?.database
16 }
17
18 function getDefaultDriverOptions(clientUrl: string) {
19 const localOptions = {
20 connection: {
21 ssl: false,
22 },
23 }
24
25 const remoteOptions = {
26 connection: {
27 ssl: {
28 rejectUnauthorized: false,
29 },
30 },
31 }
32
33 if (clientUrl) {
34 return clientUrl.match(
35 /localhost|127\.0\.0\.1|ssl_mode=(disable|false)|sslmode=(disable)/i
36 )
37 ? localOptions
38 : remoteOptions
39 }
40
41 return process.env.NODE_ENV?.match(/prod/i)
42 ? remoteOptions
43 : process.env.NODE_ENV?.match(/dev/i)
44 ? localOptions
45 : {}
46 }
47
48 function getDatabaseUrl(
49 config: ModulesSdkTypes.ModuleServiceInitializeOptions
50 ): string {
51 const { clientUrl, host, port, user, password, database } = config.database!
52
53 if (host) {
54 return `postgres://${user}:${password}@${host}:${port}/${database}`
55 }
56
57 return clientUrl!
58 }
59
60 /**
61 * Load the config for the database connection. The options can be retrieved
62 * e.g through PRODUCT_* (e.g PRODUCT_DATABASE_URL) or * (e.g DATABASE_URL) environment variables or the options object.
63 * @param options
64 * @param moduleName
65 */
66 export function loadDatabaseConfig(
67 moduleName: string,
68 options?: ModulesSdkTypes.ModuleServiceInitializeOptions,
69 silent: boolean = false
70 ): Pick<
71 ModulesSdkTypes.ModuleServiceInitializeOptions["database"],
72 "clientUrl" | "schema" | "driverOptions" | "debug"
73 > {
74 const clientUrl =
75 options?.database?.clientUrl ?? getEnv("DATABASE_URL", moduleName)
76
77 const poolEnvConfig = getEnv("DATABASE_POOL", moduleName)
78 const database = {
79 clientUrl,
80 schema: getEnv("DATABASE_SCHEMA", moduleName) ?? "public",
81 driverOptions: JSON.parse(
82 getEnv("DATABASE_DRIVER_OPTIONS", moduleName) ||
83 JSON.stringify(getDefaultDriverOptions(clientUrl))
84 ),
85 pool: poolEnvConfig ? JSON.parse(poolEnvConfig) : undefined,
86 debug: false,
87 connection: undefined,
88 }
89
90 if (isModuleServiceInitializeOptions(options)) {
91 database.clientUrl = getDatabaseUrl({
92 database: { ...options.database, clientUrl },
93 })
94 database.schema = options.database!.schema ?? database.schema
95 database.driverOptions =
96 options.database!.driverOptions ??
97 getDefaultDriverOptions(database.clientUrl)
98 database.pool = options.database!.pool ?? database.pool
99 database.debug = options.database!.debug ?? database.debug
100 database.connection = options.database!.connection
101 }
102
103 if (!database.clientUrl && !silent && !database.connection) {
104 throw new MedusaError(
105 MedusaError.Types.INVALID_ARGUMENT,
106 "No database clientUrl provided. Please provide the clientUrl through the [MODULE]_DATABASE_URL, MEDUSA_DATABASE_URL or DATABASE_URL environment variable or the options object in the initialize function."
107 )
108 }
109
110 /**
111 * Remove the ssl_mode parameter since it is not supported by
112 * the database
113 * driver but rather an internal parameter used by us.
114 */
115 database.clientUrl = database.clientUrl?.replace(
116 /(\?|&)ssl_mode=[^&]*(&|$)/gi,
117 (match, prefix, suffix) => {
118 if (prefix === "?" && suffix === "&") return "?"
119 if (prefix === "?" && suffix === "") return ""
120 if (prefix === "&") return suffix
121 return ""
122 }
123 )
124 return database
125 }