This commit is contained in:
Yanlong Wang 2025-04-11 10:50:32 +08:00
parent 7f59f42ad7
commit 24c1618f4e
No known key found for this signature in database
GPG Key ID: C0A623C0BADF9F37
2 changed files with 86 additions and 1 deletions

85
src/services/mongodb.ts Normal file
View File

@ -0,0 +1,85 @@
import _ from 'lodash';
import { singleton, container } from 'tsyringe';
import {
MongoClientOptions, ObjectId,
} from 'mongodb';
import { AbstractMongoDB, AbstractMongoCollection, AbstractMongoCappedCollection } from 'civkit/abstract/mongo';
import { LoggerInterface } from 'civkit/logger';
import globalLogger, { GlobalLogger } from '../services/logger';
import { Finalizer } from './finalizer';
import { InjectProperty } from './registry';
import { SecretExposer } from '../shared/services/secrets';
@singleton()
export class MongoDB extends AbstractMongoDB {
options?: MongoClientOptions;
url!: string;
logger = this.globalLogger.child({ service: this.constructor.name });
@InjectProperty() protected config!: SecretExposer;
constructor(
protected globalLogger: GlobalLogger
) {
super(...arguments);
this.dependsOn(this.config);
}
override async init() {
await this.dependencyReady();
if (!this.options) {
this.options = {
minPoolSize: 2,
maxPoolSize: 10,
};
}
if (!this.url) {
this.url = this.config.MONGO_URI;
}
await super.init();
this.emit('ready');
}
@Finalizer()
override async standDown() {
if (this.serviceStatus !== 'ready') {
return;
}
await this.client.close(true);
super.standDown();
}
}
export abstract class MongoCollection<T extends object, P = ObjectId> extends AbstractMongoCollection<T, P> {
@InjectProperty()
mongo!: MongoDB;
logger: LoggerInterface = globalLogger.child({ service: this.constructor.name });
constructor(...whatever: any[]) {
super(...whatever);
this.dependsOn(globalLogger);
}
}
export abstract class MongoCappedCollection<T extends object, P = ObjectId> extends
AbstractMongoCappedCollection<T, P> {
logger: LoggerInterface = globalLogger.child({ service: this.constructor.name });
@InjectProperty()
mongo!: MongoDB;
}
export const mongoClient = container.resolve(MongoDB);
export default mongoClient;

@ -1 +1 @@
Subproject commit 95c88c0da1058144a86b736fde6bec8a861c2800 Subproject commit 4552b18a12f249a8413ba73700bbe0816e9fde0d