Add cleanup of initialize mongodb, ensure indexes are created for compound searching
This commit is contained in:
@@ -13,4 +13,6 @@ const ImdbEntriesSchema: Schema = new Schema({
|
||||
TitleType: { type: String, default: "" },
|
||||
});
|
||||
|
||||
ImdbEntriesSchema.index({ PrimaryTitle: 'text', TitleType: 1, StartYear: 1 }, { background: true });
|
||||
|
||||
export const ImdbEntryModel = mongoose.model<IImdbEntry>('ImdbEntry', ImdbEntriesSchema, 'imdb-entries');
|
||||
@@ -1,17 +1,28 @@
|
||||
import {TorrentType} from "@enums/torrent_types";
|
||||
import {ILoggingService} from "@interfaces/logging_service";
|
||||
import {IMongoMetadataQuery} from "@mongo/interfaces/mongo_metadata_query";
|
||||
import {IMongoRepository} from "@mongo/interfaces/mongo_repository";
|
||||
import {ImdbEntryModel} from "@mongo/models/imdb_entries_model";
|
||||
import {configurationService} from '@services/configuration_service';
|
||||
import {injectable} from "inversify";
|
||||
import {IocTypes} from "@setup/ioc_types";
|
||||
import {inject, injectable} from "inversify";
|
||||
import mongoose from 'mongoose';
|
||||
|
||||
@injectable()
|
||||
export class MongoRepository implements IMongoRepository {
|
||||
@inject(IocTypes.ILoggingService) private logger: ILoggingService;
|
||||
private db: typeof mongoose = mongoose;
|
||||
|
||||
|
||||
async connect() : Promise<void> {
|
||||
await this.db.connect(configurationService.cacheConfig.MONGO_URI, {directConnection: true});
|
||||
try {
|
||||
await this.db.connect(configurationService.cacheConfig.MONGO_URI, {directConnection: true});
|
||||
this.logger.info('Successfully connected to mongo db');
|
||||
}
|
||||
catch (error) {
|
||||
this.logger.debug('Failed to connect to mongo db', error);
|
||||
this.logger.error('Failed to connect to mongo db');
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
async getImdbId(title: string, category: string, year?: string | number) : Promise<string | null> {
|
||||
@@ -35,7 +46,12 @@ export class MongoRepository implements IMongoRepository {
|
||||
query.StartYear = year.toString();
|
||||
}
|
||||
|
||||
const result = await ImdbEntryModel.findOne(query);
|
||||
return result ? result._id : null;
|
||||
try {
|
||||
const result = await ImdbEntryModel.findOne(query, '_id').maxTimeMS(30000);
|
||||
return result ? result._id : null;
|
||||
} catch (error) {
|
||||
this.logger.error('Query exceeded the 30 seconds time limit', error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,9 @@
|
||||
import "reflect-metadata"; // required
|
||||
import {TorrentType} from "@enums/torrent_types";
|
||||
import {ILoggingService} from "@interfaces/logging_service";
|
||||
import {MongoRepository} from "@mongo/mongo_repository";
|
||||
import {Container} from "inversify";
|
||||
import {IocTypes} from "@setup/ioc_types";
|
||||
import {Container, inject} from "inversify";
|
||||
|
||||
jest.mock('@services/configuration_service', () => {
|
||||
return {
|
||||
@@ -20,13 +22,24 @@ jest.mock('@services/configuration_service', () => {
|
||||
}
|
||||
});
|
||||
|
||||
jest.mock('@services/logging_service', () => {
|
||||
return {
|
||||
error: jest.fn(),
|
||||
info: jest.fn(),
|
||||
debug: jest.fn(),
|
||||
}
|
||||
})
|
||||
|
||||
xdescribe('MongoRepository Tests - Manual Tests against real cluster. Skipped by default.', () => {
|
||||
let mongoRepository: MongoRepository;
|
||||
let mongoRepository: MongoRepository,
|
||||
mockLogger: ILoggingService;
|
||||
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
process.env.LOG_LEVEL = 'debug';
|
||||
mockLogger = jest.requireMock<ILoggingService>('@services/logging_service');
|
||||
const container = new Container();
|
||||
container.bind<ILoggingService>(IocTypes.ILoggingService).toConstantValue(mockLogger);
|
||||
container.bind<MongoRepository>(MongoRepository).toSelf();
|
||||
mongoRepository = container.get(MongoRepository);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user