import { DyFM_Log, DyFM_String } from '@futdevpro/fsm-dynamo'; import { ConnectOptions } from 'mongoose'; import { DyNTS_global_settings } from '../../_collections/global-settings.const'; /** * This will hold and set the basic settings of an application */ export class DyNTS_App_Params { /** * name of the application */ readonly name: string; /** * title will be shown on the start of the application */ readonly title?: string; /** * version of the application * you should set the version, * probably, you should set from the package.json as follows: * import { version } from '../package.json'; */ readonly version: string; /** * name of your MongoDB table * by default, its: `mongodb://localhost:27017/${this.dbName}` */ readonly dbUri?: string; /** * name of your MongoDB table */ readonly dbName?: string; /** * options for the MongoDB connection */ readonly dbOptions?: ConnectOptions; readonly openHost?: string; readonly secureHost?: string; /** * specify the maximum * length of the queue of pending connections. * The actual length will be determined by the OS through sysctl. * */ readonly expressBacklog?: number; /** * name of the system, by default, its: this.name.replace(' ', '-') */ readonly systemName?: string; /** * short code name of the system */ readonly systemShortCodeName?: string; //readonly dontUseEnvInSystemShortCodeName?: boolean; constructor( set: DyNTS_App_Params ) { this.name = set.name; this.title = set.title ?? ''; this.version = set.version; if (DyNTS_global_settings.systemVersion === 'unknown-version') { DyNTS_global_settings.systemVersion = this.version; } if (set.dbName || set.dbUri || set.dbOptions) { this.dbName = set.dbName; if (!set.dbUri) { if (DyNTS_global_settings.env_settings.mongoUri) { if (set.dbName) { this.dbUri = `${DyNTS_global_settings.env_settings.mongoUri}/${set.dbName}`; } else { this.dbUri = DyNTS_global_settings.env_settings.mongoUri; } } else { this.dbUri = `mongodb://0.0.0.0:27017/${set.dbName}`; } } else { this.dbUri = set.dbUri; } if (this.dbUri.split('//').length > 2) { DyFM_Log.warn( 'DyNTS_App_Params.constructor: ' + `dbUri should not contain more than one "//" in "${this.dbUri}".` ); } this.dbOptions = set.dbOptions ?? { directConnection: true, }; } this.systemName = set.systemName ?? this.name.replace(' ', '-'); DyNTS_global_settings.systemName ??= this.systemName; let sysShortCodeName: string; sysShortCodeName = set.systemShortCodeName ?? DyFM_String.anyNameToShortCode(this.systemName); //if (!set.dontUseEnvInSystemShortCodeName) { sysShortCodeName += '_' + DyNTS_global_settings.env_settings.environment.toUpperCase(); //} this.systemShortCodeName = sysShortCodeName; DyNTS_global_settings.systemShortCodeName ??= this.systemShortCodeName; this.openHost = set.openHost ?? '0.0.0.0'; this.secureHost = set.secureHost ?? '0.0.0.0'; this.expressBacklog = set.expressBacklog ?? 0; if (!this.dbName) { DyFM_Log.error( 'DyNTS_App_Params.constructor: ' + `dbName is not set in "${this.name}" model.` + 'For future compatibility.' ); } if (RegExp(/[A-Z]/).exec(this.dbName)) { DyFM_Log.warn( 'DyNTS_App_Params.constructor: ' + `dbName should be in snake_case in "${this.dbName}" model.` + 'For future compatibility.' ); } /* if (this.dbName?.includes('-')) { DyFM_Log.warn( 'DyNTS_App_Params.constructor: ' + `dbName should not contain "-" in "${this.dbName}" model.` + 'For future compatibility.' ); } */ } }