cleanup
This commit is contained in:
parent
c353ddbe01
commit
ce9e2df67c
310 changed files with 2550 additions and 36861 deletions
|
|
@ -189,6 +189,8 @@ export class ConfigManager<T = Record<string, unknown>> {
|
|||
case 'production':
|
||||
case 'prod':
|
||||
return 'production';
|
||||
case 'qa':
|
||||
return 'qa';
|
||||
case 'test':
|
||||
return 'test';
|
||||
case 'development':
|
||||
|
|
|
|||
|
|
@ -133,13 +133,43 @@ export class EnvLoader implements ConfigLoader {
|
|||
|
||||
private shouldPreserveStringForKey(key: string): boolean {
|
||||
// Keys that should preserve string values even if they look like numbers
|
||||
const preserveStringKeys = ['QM_WEBMASTER_ID', 'IB_MARKET_DATA_TYPE'];
|
||||
const preserveStringKeys = [
|
||||
'QM_WEBMASTER_ID',
|
||||
'IB_MARKET_DATA_TYPE',
|
||||
'MONGODB_URI',
|
||||
'MONGODB_READ_URI',
|
||||
'MONGODB_PASSWORD',
|
||||
'POSTGRES_PASSWORD',
|
||||
'DRAGONFLY_PASSWORD',
|
||||
'QUESTDB_PASSWORD',
|
||||
];
|
||||
return preserveStringKeys.includes(key);
|
||||
}
|
||||
|
||||
private getProviderMapping(envKey: string): { path: string[] } | null {
|
||||
// Provider-specific and special environment variable mappings
|
||||
const providerMappings: Record<string, string[]> = {
|
||||
// Database mappings
|
||||
MONGODB_URI: ['database', 'mongodb', 'uri'],
|
||||
MONGODB_READ_URI: ['database', 'mongodb', 'readUri'],
|
||||
MONGODB_READ_ONLY: ['database', 'mongodb', 'readOnly'],
|
||||
MONGODB_DATABASE: ['database', 'mongodb', 'database'],
|
||||
MONGODB_HOST: ['database', 'mongodb', 'host'],
|
||||
MONGODB_PORT: ['database', 'mongodb', 'port'],
|
||||
MONGODB_USERNAME: ['database', 'mongodb', 'user'],
|
||||
MONGODB_PASSWORD: ['database', 'mongodb', 'password'],
|
||||
POSTGRES_HOST: ['database', 'postgres', 'host'],
|
||||
POSTGRES_PORT: ['database', 'postgres', 'port'],
|
||||
POSTGRES_DATABASE: ['database', 'postgres', 'database'],
|
||||
POSTGRES_USERNAME: ['database', 'postgres', 'user'],
|
||||
POSTGRES_PASSWORD: ['database', 'postgres', 'password'],
|
||||
POSTGRES_SSL: ['database', 'postgres', 'ssl'],
|
||||
DRAGONFLY_HOST: ['database', 'dragonfly', 'host'],
|
||||
DRAGONFLY_PORT: ['database', 'dragonfly', 'port'],
|
||||
DRAGONFLY_PASSWORD: ['database', 'dragonfly', 'password'],
|
||||
QUESTDB_HOST: ['database', 'questdb', 'host'],
|
||||
QUESTDB_PORT: ['database', 'questdb', 'httpPort'],
|
||||
|
||||
// WebShare provider mappings
|
||||
WEBSHARE_API_KEY: ['webshare', 'apiKey'],
|
||||
WEBSHARE_API_URL: ['webshare', 'apiUrl'],
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import { z } from 'zod';
|
||||
|
||||
export const environmentSchema = z.enum(['development', 'test', 'production']);
|
||||
export const environmentSchema = z.enum(['development', 'qa', 'test', 'production']);
|
||||
|
||||
export const baseConfigSchema = z.object({
|
||||
environment: environmentSchema.optional(),
|
||||
|
|
|
|||
|
|
@ -37,6 +37,8 @@ export const questdbConfigSchema = z.object({
|
|||
export const mongodbConfigSchema = z.object({
|
||||
enabled: z.boolean().default(true),
|
||||
uri: z.string().url(), // URI is required and contains all connection info
|
||||
readUri: z.string().url().optional(), // Optional read-only URI (e.g. dev data-pipeline reading QA MongoDB)
|
||||
readOnly: z.boolean().default(false).optional(),
|
||||
database: z.string(), // Database name for reference
|
||||
poolSize: z.number().min(1).max(100).default(10),
|
||||
// Optional fields for cases where URI parsing might fail
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import { z } from 'zod';
|
||||
|
||||
export type Environment = 'development' | 'test' | 'production';
|
||||
export type Environment = 'development' | 'qa' | 'test' | 'production';
|
||||
|
||||
export interface ConfigLoader {
|
||||
load(): Record<string, unknown>;
|
||||
|
|
|
|||
|
|
@ -28,6 +28,7 @@ describe('Config Schemas', () => {
|
|||
describe('environmentSchema', () => {
|
||||
it('should accept valid environments', () => {
|
||||
expect(environmentSchema.parse('development')).toBe('development');
|
||||
expect(environmentSchema.parse('qa')).toBe('qa');
|
||||
expect(environmentSchema.parse('test')).toBe('test');
|
||||
expect(environmentSchema.parse('production')).toBe('production');
|
||||
});
|
||||
|
|
|
|||
|
|
@ -14,6 +14,16 @@ import { HandlerScanner } from '../scanner';
|
|||
import { ServiceLifecycleManager } from '../utils/lifecycle';
|
||||
import type { ContainerBuildOptions, ServiceDefinitions } from './types';
|
||||
|
||||
function extractMongoDatabaseFromUri(uri?: string): string | undefined {
|
||||
if (!uri) return undefined;
|
||||
try {
|
||||
const db = new URL(uri).pathname.replace(/^\/+/, '').split('/')[0];
|
||||
return db || undefined;
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
function getInfluxPort(config: { influxPort?: number; ilpPort?: number }): number {
|
||||
return config.influxPort ?? config.ilpPort ?? 9009;
|
||||
}
|
||||
|
|
@ -133,6 +143,16 @@ export class ServiceContainerBuilder {
|
|||
uri: 'mongodb://localhost:27017',
|
||||
database: '',
|
||||
}),
|
||||
...(this.options.mongoReadOnly
|
||||
? {
|
||||
uri: config.mongodb?.readUri ?? config.mongodb?.uri ?? 'mongodb://localhost:27017',
|
||||
database:
|
||||
extractMongoDatabaseFromUri(config.mongodb?.readUri) ??
|
||||
config.mongodb?.database ??
|
||||
'',
|
||||
readOnly: true,
|
||||
}
|
||||
: {}),
|
||||
enabled: this.options.enableMongoDB !== false && (config.mongodb?.enabled ?? true),
|
||||
},
|
||||
postgres: {
|
||||
|
|
|
|||
|
|
@ -42,6 +42,7 @@ export type ServiceCradle = ServiceDefinitions;
|
|||
export interface ServiceContainerOptions {
|
||||
enableQuestDB?: boolean;
|
||||
enableMongoDB?: boolean;
|
||||
mongoReadOnly?: boolean;
|
||||
enablePostgres?: boolean;
|
||||
enableCache?: boolean;
|
||||
enableQueue?: boolean;
|
||||
|
|
|
|||
|
|
@ -24,6 +24,7 @@ export function registerDatabaseServices(
|
|||
password: config.mongodb.password,
|
||||
authSource: config.mongodb.authSource,
|
||||
uri: config.mongodb.uri,
|
||||
readOnly: config.mongodb.readOnly ?? false,
|
||||
poolSettings: {
|
||||
maxPoolSize: config.mongodb.poolSize,
|
||||
minPoolSize: 1,
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ export interface QueuePoolConfig extends ConnectionPoolConfig {
|
|||
|
||||
export interface ConnectionFactoryConfig {
|
||||
service: string;
|
||||
environment: 'development' | 'production' | 'test';
|
||||
environment: 'development' | 'qa' | 'production' | 'test';
|
||||
pools?: {
|
||||
mongodb?: Partial<MongoDBPoolConfig>;
|
||||
postgres?: Partial<PostgreSQLPoolConfig>;
|
||||
|
|
|
|||
|
|
@ -80,7 +80,7 @@ export class MongoDBClient {
|
|||
});
|
||||
|
||||
await this.client.connect();
|
||||
await this.client.db(this.defaultDatabase).admin().ping();
|
||||
await this.client.db(this.defaultDatabase).command({ ping: 1 });
|
||||
|
||||
// Set default database from config
|
||||
this.db = this.client.db(this.defaultDatabase);
|
||||
|
|
@ -201,6 +201,41 @@ export class MongoDBClient {
|
|||
return this.client.db(dbName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether this client is locked to read-only mode
|
||||
*/
|
||||
isReadOnly(): boolean {
|
||||
return this.config.readOnly === true;
|
||||
}
|
||||
|
||||
private assertWritable(operation: string): void {
|
||||
if (this.config.readOnly) {
|
||||
throw new Error(
|
||||
`MongoDB client is configured in read-only mode; write operation "${operation}" is forbidden`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private assertReadOnlyPipeline(pipeline: Document[]): void {
|
||||
if (!this.config.readOnly) {
|
||||
return;
|
||||
}
|
||||
const checkStage = (value: unknown): void => {
|
||||
if (!value || typeof value !== 'object') return;
|
||||
for (const [key, child] of Object.entries(value)) {
|
||||
if (key === '$out' || key === '$merge') {
|
||||
throw new Error(
|
||||
`MongoDB client is configured in read-only mode; aggregation stage "${key}" is forbidden`
|
||||
);
|
||||
}
|
||||
checkStage(child);
|
||||
}
|
||||
};
|
||||
for (const stage of pipeline) {
|
||||
checkStage(stage);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Batch upsert documents for high-performance operations
|
||||
* Supports single or multiple unique keys for matching
|
||||
|
|
@ -216,6 +251,7 @@ export class MongoDBClient {
|
|||
database?: string; // Optional database override
|
||||
} = {}
|
||||
): Promise<{ insertedCount: number; updatedCount: number; errors: unknown[] }> {
|
||||
this.assertWritable(`batchUpsert(${collectionName})`);
|
||||
if (!this.client) {
|
||||
throw new Error('MongoDB client not connected');
|
||||
}
|
||||
|
|
@ -367,6 +403,7 @@ export class MongoDBClient {
|
|||
Partial<Pick<T, 'created_at' | 'updated_at'>>,
|
||||
database?: string
|
||||
): Promise<T> {
|
||||
this.assertWritable(`insertOne(${collectionName})`);
|
||||
const collection = this.getCollection<T>(collectionName, database);
|
||||
|
||||
const now = new Date();
|
||||
|
|
@ -389,6 +426,7 @@ export class MongoDBClient {
|
|||
options?: BulkWriteOptions,
|
||||
dbName?: string
|
||||
) {
|
||||
this.assertWritable(`insertMany(${collectionName})`);
|
||||
const collection = this.getCollection(collectionName, dbName);
|
||||
const now = new Date();
|
||||
|
||||
|
|
@ -446,6 +484,7 @@ export class MongoDBClient {
|
|||
options?: UpdateOptions,
|
||||
dbName?: string
|
||||
) {
|
||||
this.assertWritable(`updateOne(${collectionName})`);
|
||||
const collection = this.getCollection(collectionName, dbName);
|
||||
|
||||
// Add updated_at timestamp
|
||||
|
|
@ -474,6 +513,7 @@ export class MongoDBClient {
|
|||
options?: UpdateOptions,
|
||||
dbName?: string
|
||||
) {
|
||||
this.assertWritable(`updateMany(${collectionName})`);
|
||||
const collection = this.getCollection(collectionName, dbName);
|
||||
|
||||
// Add updated_at timestamp
|
||||
|
|
@ -501,6 +541,7 @@ export class MongoDBClient {
|
|||
options?: DeleteOptions,
|
||||
dbName?: string
|
||||
) {
|
||||
this.assertWritable(`deleteOne(${collectionName})`);
|
||||
const collection = this.getCollection(collectionName, dbName);
|
||||
const result = await collection.deleteOne(filter, options);
|
||||
return {
|
||||
|
|
@ -517,6 +558,7 @@ export class MongoDBClient {
|
|||
options?: DeleteOptions,
|
||||
dbName?: string
|
||||
) {
|
||||
this.assertWritable(`deleteMany(${collectionName})`);
|
||||
const collection = this.getCollection(collectionName, dbName);
|
||||
const result = await collection.deleteMany(filter, options);
|
||||
return {
|
||||
|
|
@ -546,6 +588,7 @@ export class MongoDBClient {
|
|||
options?: AggregateOptions,
|
||||
dbName?: string
|
||||
): Promise<T[]> {
|
||||
this.assertReadOnlyPipeline(pipeline);
|
||||
const collection = this.getCollection(collectionName, dbName);
|
||||
const cursor = collection.aggregate(pipeline, options);
|
||||
return (await cursor.toArray()) as T[];
|
||||
|
|
@ -560,6 +603,7 @@ export class MongoDBClient {
|
|||
options?: CreateIndexesOptions,
|
||||
dbName?: string
|
||||
): Promise<string> {
|
||||
this.assertWritable(`createIndex(${collectionName})`);
|
||||
const collection = this.getCollection(collectionName, dbName);
|
||||
return await collection.createIndex(indexSpec, options);
|
||||
}
|
||||
|
|
@ -573,6 +617,7 @@ export class MongoDBClient {
|
|||
options?: DropIndexesOptions,
|
||||
dbName?: string
|
||||
): Promise<void> {
|
||||
this.assertWritable(`dropIndex(${collectionName})`);
|
||||
const collection = this.getCollection(collectionName, dbName);
|
||||
await collection.dropIndex(indexName, options);
|
||||
}
|
||||
|
|
@ -601,6 +646,7 @@ export class MongoDBClient {
|
|||
options?: CreateCollectionOptions,
|
||||
dbName?: string
|
||||
): Promise<void> {
|
||||
this.assertWritable(`createCollection(${collectionName})`);
|
||||
const db = this.getDatabase(dbName);
|
||||
await db.createCollection(collectionName, options);
|
||||
}
|
||||
|
|
@ -609,6 +655,7 @@ export class MongoDBClient {
|
|||
* Drop a collection
|
||||
*/
|
||||
async dropCollection(collectionName: string, dbName?: string): Promise<void> {
|
||||
this.assertWritable(`dropCollection(${collectionName})`);
|
||||
const db = this.getDatabase(dbName);
|
||||
await db.dropCollection(collectionName);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ export interface MongoDBClientConfig {
|
|||
password?: string;
|
||||
authSource?: string;
|
||||
uri?: string;
|
||||
readOnly?: boolean;
|
||||
poolSettings?: {
|
||||
maxPoolSize: number;
|
||||
minPoolSize: number;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue