feat(database): support ssl config of database (#2620)

* feat: support ssl config of database

* chore: .env.example
This commit is contained in:
ChengLei Shao 2023-09-11 10:55:47 +08:00 committed by GitHub
parent fec17d5661
commit cc46d72c53
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 1 deletions

View File

@ -36,6 +36,12 @@ DB_TABLE_PREFIX=
# DB_LOGGING=on # DB_LOGGING=on
# DB_UNDERSCORED=false # DB_UNDERSCORED=false
#== SSL CONFIG ==#
# DB_DIALECT_OPTIONS_SSL_CA=
# DB_DIALECT_OPTIONS_SSL_KEY=
# DB_DIALECT_OPTIONS_SSL_CERT=
# DB_DIALECT_OPTIONS_SSL_REJECT_UNAUTHORIZED=true
################# CACHE ################# ################# CACHE #################
# default is memory cache, when develop mode,code's change will be clear memory cache, so can use 'cache-manager-fs-hash' # default is memory cache, when develop mode,code's change will be clear memory cache, so can use 'cache-manager-fs-hash'
# CACHE_CONFIG={"storePackage":"cache-manager-fs-hash","ttl":86400,"max":1000} # CACHE_CONFIG={"storePackage":"cache-manager-fs-hash","ttl":86400,"max":1000}

View File

@ -1,6 +1,26 @@
import { IDatabaseOptions } from '@nocobase/database'; import { IDatabaseOptions } from '@nocobase/database';
export default { function getEnvValue(key, defaultValue?) {
return process.env[key] || defaultValue;
}
function extractSSLOptionsFromEnv() {
const sslOptions = {};
const ca = getEnvValue('DB_DIALECT_OPTIONS_SSL_CA');
const key = getEnvValue('DB_DIALECT_OPTIONS_SSL_KEY');
const cert = getEnvValue('DB_DIALECT_OPTIONS_SSL_CERT');
const rejectUnauthorized = getEnvValue('DB_DIALECT_OPTIONS_SSL_REJECT_UNAUTHORIZED');
if (ca) sslOptions['ca'] = ca;
if (key) sslOptions['key'] = key;
if (cert) sslOptions['cert'] = cert;
if (rejectUnauthorized) sslOptions['rejectUnauthorized'] = rejectUnauthorized === 'true';
return sslOptions;
}
const databaseOptions = {
logging: process.env.DB_LOGGING == 'on' ? customLogger : false, logging: process.env.DB_LOGGING == 'on' ? customLogger : false,
dialect: process.env.DB_DIALECT as any, dialect: process.env.DB_DIALECT as any,
storage: process.env.DB_STORAGE, storage: process.env.DB_STORAGE,
@ -15,6 +35,15 @@ export default {
underscored: process.env.DB_UNDERSCORED === 'true', underscored: process.env.DB_UNDERSCORED === 'true',
} as IDatabaseOptions; } as IDatabaseOptions;
const sslOptions = extractSSLOptionsFromEnv();
if (Object.keys(sslOptions).length) {
databaseOptions.dialectOptions = databaseOptions.dialectOptions || {};
databaseOptions.dialectOptions['ssl'] = sslOptions;
}
export default databaseOptions;
function customLogger(queryString, queryObject) { function customLogger(queryString, queryObject) {
console.log(queryString); // outputs a string console.log(queryString); // outputs a string
if (queryObject?.bind) { if (queryObject?.bind) {