|
|
@ -1,6 +1,7 @@ |
|
|
|
import fs from 'fs'; |
|
|
|
import fs from 'fs'; |
|
|
|
import parseDbUrl from 'parse-database-url'; |
|
|
|
import parseDbUrl from 'parse-database-url'; |
|
|
|
import { URL } from 'url'; |
|
|
|
import { URL } from 'url'; |
|
|
|
|
|
|
|
import { promisify } from 'util'; |
|
|
|
|
|
|
|
|
|
|
|
import { |
|
|
|
import { |
|
|
|
AuthConfig, |
|
|
|
AuthConfig, |
|
|
@ -18,6 +19,8 @@ const { |
|
|
|
animals, |
|
|
|
animals, |
|
|
|
} = require('unique-names-generator'); |
|
|
|
} = require('unique-names-generator'); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const readFileAsync = promisify(fs.readFile); |
|
|
|
|
|
|
|
|
|
|
|
const driverClientMapping = { |
|
|
|
const driverClientMapping = { |
|
|
|
mysql: 'mysql2', |
|
|
|
mysql: 'mysql2', |
|
|
|
mariadb: 'mysql2', |
|
|
|
mariadb: 'mysql2', |
|
|
@ -89,8 +92,8 @@ const knownQueryParams = [ |
|
|
|
]; |
|
|
|
]; |
|
|
|
|
|
|
|
|
|
|
|
export default class NcConfigFactory implements NcConfig { |
|
|
|
export default class NcConfigFactory implements NcConfig { |
|
|
|
public static make(): NcConfig { |
|
|
|
public static async make(): Promise<NcConfig> { |
|
|
|
this.jdbcToXcUrl(); |
|
|
|
await this.jdbcToXcUrl(); |
|
|
|
|
|
|
|
|
|
|
|
const ncConfig = new NcConfigFactory(); |
|
|
|
const ncConfig = new NcConfigFactory(); |
|
|
|
|
|
|
|
|
|
|
@ -115,7 +118,7 @@ export default class NcConfigFactory implements NcConfig { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (process.env.NC_DB) { |
|
|
|
if (process.env.NC_DB) { |
|
|
|
ncConfig.meta.db = this.metaUrlToDbConfig(process.env.NC_DB); |
|
|
|
ncConfig.meta.db = await this.metaUrlToDbConfig(process.env.NC_DB); |
|
|
|
} else if (process.env.NC_DB_JSON) { |
|
|
|
} else if (process.env.NC_DB_JSON) { |
|
|
|
ncConfig.meta.db = JSON.parse(process.env.NC_DB_JSON); |
|
|
|
ncConfig.meta.db = JSON.parse(process.env.NC_DB_JSON); |
|
|
|
} else if (process.env.NC_DB_JSON_FILE) { |
|
|
|
} else if (process.env.NC_DB_JSON_FILE) { |
|
|
@ -125,7 +128,7 @@ export default class NcConfigFactory implements NcConfig { |
|
|
|
throw new Error(`NC_DB_JSON_FILE not found: ${filePath}`); |
|
|
|
throw new Error(`NC_DB_JSON_FILE not found: ${filePath}`); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const fileContent = fs.readFileSync(filePath, { encoding: 'utf8' }); |
|
|
|
const fileContent = await readFileAsync(filePath, { encoding: 'utf8' }); |
|
|
|
ncConfig.meta.db = JSON.parse(fileContent); |
|
|
|
ncConfig.meta.db = JSON.parse(fileContent); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -291,7 +294,7 @@ export default class NcConfigFactory implements NcConfig { |
|
|
|
.replace(/[ -]/g, '_'); |
|
|
|
.replace(/[ -]/g, '_'); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static metaUrlToDbConfig(urlString) { |
|
|
|
static async metaUrlToDbConfig(urlString) { |
|
|
|
const url = new URL(urlString); |
|
|
|
const url = new URL(urlString); |
|
|
|
|
|
|
|
|
|
|
|
let dbConfig; |
|
|
|
let dbConfig; |
|
|
@ -378,29 +381,29 @@ export default class NcConfigFactory implements NcConfig { |
|
|
|
typeof dbConfig?.connection?.ssl === 'object' |
|
|
|
typeof dbConfig?.connection?.ssl === 'object' |
|
|
|
) { |
|
|
|
) { |
|
|
|
if (dbConfig.connection.ssl.caFilePath && !dbConfig.connection.ssl.ca) { |
|
|
|
if (dbConfig.connection.ssl.caFilePath && !dbConfig.connection.ssl.ca) { |
|
|
|
dbConfig.connection.ssl.ca = fs |
|
|
|
dbConfig.connection.ssl.ca = await readFileAsync( |
|
|
|
.readFileSync(dbConfig.connection.ssl.caFilePath) |
|
|
|
dbConfig.connection.ssl.caFilePath |
|
|
|
.toString(); |
|
|
|
).toString(); |
|
|
|
} |
|
|
|
} |
|
|
|
if (dbConfig.connection.ssl.keyFilePath && !dbConfig.connection.ssl.key) { |
|
|
|
if (dbConfig.connection.ssl.keyFilePath && !dbConfig.connection.ssl.key) { |
|
|
|
dbConfig.connection.ssl.key = fs |
|
|
|
dbConfig.connection.ssl.key = await readFileAsync( |
|
|
|
.readFileSync(dbConfig.connection.ssl.keyFilePath) |
|
|
|
dbConfig.connection.ssl.keyFilePath |
|
|
|
.toString(); |
|
|
|
).toString(); |
|
|
|
} |
|
|
|
} |
|
|
|
if ( |
|
|
|
if ( |
|
|
|
dbConfig.connection.ssl.certFilePath && |
|
|
|
dbConfig.connection.ssl.certFilePath && |
|
|
|
!dbConfig.connection.ssl.cert |
|
|
|
!dbConfig.connection.ssl.cert |
|
|
|
) { |
|
|
|
) { |
|
|
|
dbConfig.connection.ssl.cert = fs |
|
|
|
dbConfig.connection.ssl.cert = await readFileAsync( |
|
|
|
.readFileSync(dbConfig.connection.ssl.certFilePath) |
|
|
|
dbConfig.connection.ssl.certFilePath |
|
|
|
.toString(); |
|
|
|
).toString(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return dbConfig; |
|
|
|
return dbConfig; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static makeProjectConfigFromUrl(url, type?: string): NcConfig { |
|
|
|
public static async makeProjectConfigFromUrl(url, type?: string): Promise<NcConfig> { |
|
|
|
const config = new NcConfigFactory(); |
|
|
|
const config = new NcConfigFactory(); |
|
|
|
const dbConfig = this.urlToDbConfig(url, '', config, type); |
|
|
|
const dbConfig = this.urlToDbConfig(url, '', config, type); |
|
|
|
// config.envs[process.env.NODE_ENV || 'dev'].db.push(dbConfig);
|
|
|
|
// config.envs[process.env.NODE_ENV || 'dev'].db.push(dbConfig);
|
|
|
@ -430,7 +433,7 @@ export default class NcConfigFactory implements NcConfig { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (process.env.NC_DB) { |
|
|
|
if (process.env.NC_DB) { |
|
|
|
config.meta.db = this.metaUrlToDbConfig(process.env.NC_DB); |
|
|
|
config.meta.db = await this.metaUrlToDbConfig(process.env.NC_DB); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (process.env.NC_TRY) { |
|
|
|
if (process.env.NC_TRY) { |
|
|
@ -482,10 +485,10 @@ export default class NcConfigFactory implements NcConfig { |
|
|
|
return config; |
|
|
|
return config; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static makeProjectConfigFromConnection( |
|
|
|
public static async makeProjectConfigFromConnection( |
|
|
|
dbConnectionConfig: any, |
|
|
|
dbConnectionConfig: any, |
|
|
|
type?: string |
|
|
|
type?: string |
|
|
|
): NcConfig { |
|
|
|
): Promise<NcConfig> { |
|
|
|
const config = new NcConfigFactory(); |
|
|
|
const config = new NcConfigFactory(); |
|
|
|
let dbConfig = dbConnectionConfig; |
|
|
|
let dbConfig = dbConnectionConfig; |
|
|
|
|
|
|
|
|
|
|
@ -545,7 +548,7 @@ export default class NcConfigFactory implements NcConfig { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (process.env.NC_DB) { |
|
|
|
if (process.env.NC_DB) { |
|
|
|
config.meta.db = this.metaUrlToDbConfig(process.env.NC_DB); |
|
|
|
config.meta.db = await this.metaUrlToDbConfig(process.env.NC_DB); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (process.env.NC_TRY) { |
|
|
|
if (process.env.NC_TRY) { |
|
|
@ -637,9 +640,9 @@ export default class NcConfigFactory implements NcConfig { |
|
|
|
this.envs = { _noco: { db: [] } }; |
|
|
|
this.envs = { _noco: { db: [] } }; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static jdbcToXcUrl() { |
|
|
|
public static async jdbcToXcUrl() { |
|
|
|
if (process.env.NC_DATABASE_URL_FILE || process.env.DATABASE_URL_FILE) { |
|
|
|
if (process.env.NC_DATABASE_URL_FILE || process.env.DATABASE_URL_FILE) { |
|
|
|
const database_url = fs.readFileSync( |
|
|
|
const database_url = await readFileAsync( |
|
|
|
process.env.NC_DATABASE_URL_FILE || process.env.DATABASE_URL_FILE, |
|
|
|
process.env.NC_DATABASE_URL_FILE || process.env.DATABASE_URL_FILE, |
|
|
|
'utf-8' |
|
|
|
'utf-8' |
|
|
|
); |
|
|
|
); |
|
|
|