Added index in car catalog images + improve code readability
MODIFY
src/storage/mongodb/CarStorage.ts
src/storage/mongodb/CarStorage.ts
@@ -464,6 +464,7 @@ export default class CarStorage {
});
// Project
DatabaseUtils.projectFields(aggregation, projectFields);
+ // Execute
const carMakersMDB = await global.database.getCollection<any>(Constants.DEFAULT_TENANT, 'carcatalogs')
.aggregate(aggregation, {
allowDiskUse: true

MODIFY
src/storage/mongodb/MongoDBStorage.ts
src/storage/mongodb/MongoDBStorage.ts
@@ -121,7 +121,6 @@ export default class MongoDBStorage {
]);
// Car Catalogs
await this.handleIndexesInCollection(tenantID, 'carcatalogimages', [
- { fields: { carID: 1 } }
]);
// Transactions
await this.handleIndexesInCollection(tenantID, 'transactions', [
@@ -250,7 +249,20 @@ export default class MongoDBStorage {
action: ServerAction.MONGO_DB
});
}
+ // Handle Default Tenant
+ await this.handleCheckDefaultTenant();
+ // Handle Tenants
+ await this.handleCheckTenants();
+ }
+
+ private async handleCheckDefaultTenant() {
+ // Database creation Lock
+ const databaseLock = LockingManager.createExclusiveLock(Constants.DEFAULT_TENANT, LockEntity.DATABASE, 'check-database');
+ if (await LockingManager.acquire(databaseLock)) {
+ try {
// Check only collections with indexes
+ // Locks
+ await this.handleIndexesInCollection(Constants.DEFAULT_TENANT, 'locks', []);
// Tenants
await this.handleIndexesInCollection(Constants.DEFAULT_TENANT, 'tenants', [
{ fields: { subdomain: 1 }, options: { unique: true } },
@@ -259,6 +271,10 @@ export default class MongoDBStorage {
await this.handleIndexesInCollection(Constants.DEFAULT_TENANT, 'users', [
{ fields: { email: 1 }, options: { unique: true } }
]);
+ // Car Catalogs
+ await this.handleIndexesInCollection(Constants.DEFAULT_TENANT, 'carcatalogimages', [
+ { fields: { carID: 1 } }
+ ]);
// Logs
await this.handleIndexesInCollection(Constants.DEFAULT_TENANT, 'logs', [
{ fields: { timestamp: 1 } },
@@ -269,32 +285,28 @@ export default class MongoDBStorage {
{ fields: { host: 1, timestamp: 1 } },
{ fields: { message: 'text', source: 'text', host: 'text', action: 'text' } },
]);
- // Locks
- await this.handleIndexesInCollection(Constants.DEFAULT_TENANT, 'locks', [
- ]);
- // Get all the collections
- const collections = await this.db.listCollections().toArray();
- for (const collection of collections) {
- if (collection.name === 'migrations') {
- await this.db.collection(collection.name).rename(DatabaseUtils.getCollectionName(Constants.DEFAULT_TENANT, collection.name), { dropTarget: true });
+ } finally {
+ // Release the database creation Lock
+ await LockingManager.release(databaseLock);
}
- if (collection.name === DatabaseUtils.getCollectionName(Constants.DEFAULT_TENANT, 'runningmigrations')) {
- await this.db.collection(collection.name).drop();
}
}
+
+ private async handleCheckTenants() {
+ // Get all the Tenants
const tenantsMDB = await this.db.collection(DatabaseUtils.getCollectionName(Constants.DEFAULT_TENANT, 'tenants'))
.find({}).toArray();
const tenantIds = tenantsMDB.map((t): string => t._id.toString());
for (const tenantId of tenantIds) {
// Database creation Lock
- const createDatabaseLock = LockingManager.createExclusiveLock(tenantId, LockEntity.DATABASE, 'check-database');
- if (await LockingManager.acquire(createDatabaseLock)) {
+ const databaseLock = LockingManager.createExclusiveLock(tenantId, LockEntity.DATABASE, 'check-database');
+ if (await LockingManager.acquire(databaseLock)) {
try {
// Create tenant collections
await this.checkAndCreateTenantDatabase(tenantId);
} finally {
// Release the database creation Lock
- await LockingManager.release(createDatabaseLock);
+ await LockingManager.release(databaseLock);
}
}
}

Added index in car catalog images + improve code readability