From f6e473747d16e7662949096cecf638448c41a983 Mon Sep 17 00:00:00 2001 From: Diego Sampaio Date: Thu, 7 Apr 2022 15:42:45 -0300 Subject: [PATCH 1/2] Convert indexes property to a method --- app/models/server/raw/Analytics.ts | 6 +++-- app/models/server/raw/Avatars.ts | 12 +++++---- app/models/server/raw/BaseRaw.ts | 11 +++++--- app/models/server/raw/CredentialTokens.ts | 6 +++-- app/models/server/raw/CustomSounds.ts | 6 +++-- app/models/server/raw/CustomUserStatus.ts | 6 +++-- app/models/server/raw/EmailInbox.ts | 6 +++-- app/models/server/raw/EmailMessageHistory.ts | 6 +++-- app/models/server/raw/EmojiCustom.ts | 6 +++-- app/models/server/raw/ExportOperations.ts | 6 +++-- app/models/server/raw/FederationServers.ts | 6 +++-- app/models/server/raw/IntegrationHistory.ts | 6 +++-- app/models/server/raw/Integrations.ts | 6 +++-- app/models/server/raw/LivechatTrigger.ts | 6 +++-- app/models/server/raw/NotificationQueue.ts | 18 +++++++------ app/models/server/raw/OEmbedCache.ts | 6 +++-- app/models/server/raw/PbxEvents.ts | 6 +++-- app/models/server/raw/ReadReceipts.ts | 6 +++-- app/models/server/raw/ServerEvents.ts | 6 +++-- app/models/server/raw/Sessions.ts | 27 ++++++++++---------- app/models/server/raw/Statistics.ts | 6 +++-- app/models/server/raw/Uploads.ts | 6 +++-- app/models/server/raw/UserDataFiles.ts | 6 +++-- 23 files changed, 114 insertions(+), 68 deletions(-) diff --git a/app/models/server/raw/Analytics.ts b/app/models/server/raw/Analytics.ts index e78f219d2650..23a2b637d2f9 100644 --- a/app/models/server/raw/Analytics.ts +++ b/app/models/server/raw/Analytics.ts @@ -1,14 +1,16 @@ import { Random } from 'meteor/random'; import { AggregationCursor, Cursor, SortOptionObject, UpdateWriteOpResult } from 'mongodb'; -import { BaseRaw, IndexSpecification } from './BaseRaw'; +import { BaseRaw } from './BaseRaw'; import { IAnalytic } from '../../../../definition/IAnalytic'; import { IRoom } from '../../../../definition/IRoom'; type T = IAnalytic; export class AnalyticsRaw extends BaseRaw { - protected indexes: IndexSpecification[] = [{ key: { date: 1 } }, { key: { 'room._id': 1, 'date': 1 }, unique: true }]; + protected modelIndexes() { + return [{ key: { date: 1 } }, { key: { 'room._id': 1, 'date': 1 }, unique: true }]; + } saveMessageSent({ room, date }: { room: IRoom; date: IAnalytic['date'] }): Promise { return this.updateMany( diff --git a/app/models/server/raw/Avatars.ts b/app/models/server/raw/Avatars.ts index 33628d6eb66e..f092c34b8e67 100644 --- a/app/models/server/raw/Avatars.ts +++ b/app/models/server/raw/Avatars.ts @@ -1,13 +1,15 @@ import { DeleteWriteOpResultObject, UpdateWriteOpResult } from 'mongodb'; -import { BaseRaw, IndexSpecification } from './BaseRaw'; +import { BaseRaw } from './BaseRaw'; import { IAvatar as T } from '../../../../definition/IAvatar'; export class AvatarsRaw extends BaseRaw { - protected indexes: IndexSpecification[] = [ - { key: { name: 1 }, sparse: true }, - { key: { rid: 1 }, sparse: true }, - ]; + protected modelIndexes() { + return [ + { key: { name: 1 }, sparse: true }, + { key: { rid: 1 }, sparse: true }, + ]; + } insertAvatarFileInit(name: string, userId: string, store: string, file: { name: string }, extra: object): Promise { const fileData = { diff --git a/app/models/server/raw/BaseRaw.ts b/app/models/server/raw/BaseRaw.ts index c4066eea7abc..90c0fcfc63df 100644 --- a/app/models/server/raw/BaseRaw.ts +++ b/app/models/server/raw/BaseRaw.ts @@ -73,8 +73,6 @@ const warnFields = export class BaseRaw = undefined> implements IBaseRaw { public readonly defaultFields: C; - protected indexes?: IndexSpecification[]; - protected name: string; private preventSetUpdatedAt: boolean; @@ -85,13 +83,18 @@ export class BaseRaw = undefined> implements IBase this.name = this.col.collectionName.replace(baseName, ''); this.trash = trash as unknown as Collection>; - if (this.indexes?.length) { - this.col.createIndexes(this.indexes); + const indexes = this.modelIndexes(); + if (indexes?.length) { + this.col.createIndexes(indexes); } this.preventSetUpdatedAt = options?.preventSetUpdatedAt ?? false; } + protected modelIndexes(): IndexSpecification[] | void { + // noop + } + private doNotMixInclusionAndExclusionFields(options: FindOneOptions = {}): FindOneOptions { const optionsDef = this.ensureDefaultFields(options); if (optionsDef?.projection === undefined) { diff --git a/app/models/server/raw/CredentialTokens.ts b/app/models/server/raw/CredentialTokens.ts index 930d00527165..9288eca4dac1 100644 --- a/app/models/server/raw/CredentialTokens.ts +++ b/app/models/server/raw/CredentialTokens.ts @@ -1,8 +1,10 @@ -import { BaseRaw, IndexSpecification } from './BaseRaw'; +import { BaseRaw } from './BaseRaw'; import { ICredentialToken as T } from '../../../../definition/ICredentialToken'; export class CredentialTokensRaw extends BaseRaw { - protected indexes: IndexSpecification[] = [{ key: { expireAt: 1 }, sparse: true, expireAfterSeconds: 0 }]; + protected modelIndexes() { + return [{ key: { expireAt: 1 }, sparse: true, expireAfterSeconds: 0 }]; + } async create(_id: string, userInfo: T['userInfo']): Promise { const validForMilliseconds = 60000; // Valid for 60 seconds diff --git a/app/models/server/raw/CustomSounds.ts b/app/models/server/raw/CustomSounds.ts index 5e7dd4572ac9..9dc83064da1f 100644 --- a/app/models/server/raw/CustomSounds.ts +++ b/app/models/server/raw/CustomSounds.ts @@ -1,10 +1,12 @@ import { Cursor, FindOneOptions, InsertOneWriteOpResult, UpdateWriteOpResult, WithId, WithoutProjection } from 'mongodb'; -import { BaseRaw, IndexSpecification } from './BaseRaw'; +import { BaseRaw } from './BaseRaw'; import { ICustomSound as T } from '../../../../definition/ICustomSound'; export class CustomSoundsRaw extends BaseRaw { - protected indexes: IndexSpecification[] = [{ key: { name: 1 } }]; + protected modelIndexes() { + return [{ key: { name: 1 } }]; + } // find findByName(name: string, options: WithoutProjection>): Cursor { diff --git a/app/models/server/raw/CustomUserStatus.ts b/app/models/server/raw/CustomUserStatus.ts index d4ed3321183d..8d342d67bbe0 100644 --- a/app/models/server/raw/CustomUserStatus.ts +++ b/app/models/server/raw/CustomUserStatus.ts @@ -1,10 +1,12 @@ import { Cursor, FindOneOptions, InsertOneWriteOpResult, UpdateWriteOpResult, WithId, WithoutProjection } from 'mongodb'; -import { BaseRaw, IndexSpecification } from './BaseRaw'; +import { BaseRaw } from './BaseRaw'; import { ICustomUserStatus as T } from '../../../../definition/ICustomUserStatus'; export class CustomUserStatusRaw extends BaseRaw { - protected indexes: IndexSpecification[] = [{ key: { name: 1 } }]; + protected modelIndexes() { + return [{ key: { name: 1 } }]; + } // find one by name async findOneByName(name: string, options: WithoutProjection>): Promise { diff --git a/app/models/server/raw/EmailInbox.ts b/app/models/server/raw/EmailInbox.ts index 38889a1d9072..dacb35b385ac 100644 --- a/app/models/server/raw/EmailInbox.ts +++ b/app/models/server/raw/EmailInbox.ts @@ -1,6 +1,8 @@ -import { BaseRaw, IndexSpecification } from './BaseRaw'; +import { BaseRaw } from './BaseRaw'; import { IEmailInbox } from '../../../../definition/IEmailInbox'; export class EmailInboxRaw extends BaseRaw { - protected indexes: IndexSpecification[] = [{ key: { email: 1 }, unique: true }]; + protected modelIndexes() { + return [{ key: { email: 1 }, unique: true }]; + } } diff --git a/app/models/server/raw/EmailMessageHistory.ts b/app/models/server/raw/EmailMessageHistory.ts index df97023387ea..df1315dc5525 100644 --- a/app/models/server/raw/EmailMessageHistory.ts +++ b/app/models/server/raw/EmailMessageHistory.ts @@ -1,10 +1,12 @@ -import { IndexSpecification, InsertOneWriteOpResult, WithId } from 'mongodb'; +import { InsertOneWriteOpResult, WithId } from 'mongodb'; import { BaseRaw } from './BaseRaw'; import { IEmailMessageHistory as T } from '../../../../definition/IEmailMessageHistory'; export class EmailMessageHistoryRaw extends BaseRaw { - protected indexes: IndexSpecification[] = [{ key: { createdAt: 1 }, expireAfterSeconds: 60 * 60 * 24 }]; + protected modelIndexes() { + return [{ key: { createdAt: 1 }, expireAfterSeconds: 60 * 60 * 24 }]; + } async create({ _id, email }: T): Promise>> { return this.insertOne({ diff --git a/app/models/server/raw/EmojiCustom.ts b/app/models/server/raw/EmojiCustom.ts index 17d45215a5ed..538be1ae3fef 100644 --- a/app/models/server/raw/EmojiCustom.ts +++ b/app/models/server/raw/EmojiCustom.ts @@ -1,10 +1,12 @@ import { Cursor, FindOneOptions, InsertOneWriteOpResult, UpdateWriteOpResult, WithId, WithoutProjection } from 'mongodb'; -import { BaseRaw, IndexSpecification } from './BaseRaw'; +import { BaseRaw } from './BaseRaw'; import { IEmojiCustom as T } from '../../../../definition/IEmojiCustom'; export class EmojiCustomRaw extends BaseRaw { - protected indexes: IndexSpecification[] = [{ key: { name: 1 } }, { key: { aliases: 1 } }, { key: { extension: 1 } }]; + protected modelIndexes() { + return [{ key: { name: 1 } }, { key: { aliases: 1 } }, { key: { extension: 1 } }]; + } // find findByNameOrAlias(emojiName: string, options: WithoutProjection>): Cursor { diff --git a/app/models/server/raw/ExportOperations.ts b/app/models/server/raw/ExportOperations.ts index b29f54be4a3b..a1e8e95dd7c2 100644 --- a/app/models/server/raw/ExportOperations.ts +++ b/app/models/server/raw/ExportOperations.ts @@ -1,12 +1,14 @@ import { Cursor, UpdateWriteOpResult } from 'mongodb'; -import { BaseRaw, IndexSpecification } from './BaseRaw'; +import { BaseRaw } from './BaseRaw'; import { IExportOperation } from '../../../../definition/IExportOperation'; type T = IExportOperation; export class ExportOperationsRaw extends BaseRaw { - protected indexes: IndexSpecification[] = [{ key: { userId: 1 } }, { key: { status: 1 } }]; + protected modelIndexes() { + return [{ key: { userId: 1 } }, { key: { status: 1 } }]; + } findOnePending(): Promise { const query = { diff --git a/app/models/server/raw/FederationServers.ts b/app/models/server/raw/FederationServers.ts index 5118aa8d2fcf..d660784d2441 100644 --- a/app/models/server/raw/FederationServers.ts +++ b/app/models/server/raw/FederationServers.ts @@ -2,10 +2,12 @@ import { UpdateWriteOpResult } from 'mongodb'; import { Users } from './index'; import { IFederationServer } from '../../../../definition/Federation'; -import { BaseRaw, IndexSpecification } from './BaseRaw'; +import { BaseRaw } from './BaseRaw'; export class FederationServersRaw extends BaseRaw { - protected indexes: IndexSpecification[] = [{ key: { domain: 1 } }]; + protected modelIndexes() { + return [{ key: { domain: 1 } }]; + } saveDomain(domain: string): Promise { return this.updateOne( diff --git a/app/models/server/raw/IntegrationHistory.ts b/app/models/server/raw/IntegrationHistory.ts index ef2f2b365c8b..41e763e91a78 100644 --- a/app/models/server/raw/IntegrationHistory.ts +++ b/app/models/server/raw/IntegrationHistory.ts @@ -1,8 +1,10 @@ -import { BaseRaw, IndexSpecification } from './BaseRaw'; +import { BaseRaw } from './BaseRaw'; import { IIntegrationHistory } from '../../../../definition/IIntegrationHistory'; export class IntegrationHistoryRaw extends BaseRaw { - protected indexes: IndexSpecification[] = [{ key: { 'integration._id': 1, 'integration._createdBy._id': 1 } }]; + protected modelIndexes() { + return [{ key: { 'integration._id': 1, 'integration._createdBy._id': 1 } }]; + } removeByIntegrationId(integrationId: string): ReturnType['deleteMany']> { return this.deleteMany({ 'integration._id': integrationId }); diff --git a/app/models/server/raw/Integrations.ts b/app/models/server/raw/Integrations.ts index c1c7de6b07bd..e7b4f1cc8df7 100644 --- a/app/models/server/raw/Integrations.ts +++ b/app/models/server/raw/Integrations.ts @@ -1,8 +1,10 @@ -import { BaseRaw, IndexSpecification } from './BaseRaw'; +import { BaseRaw } from './BaseRaw'; import { IIntegration } from '../../../../definition/IIntegration'; export class IntegrationsRaw extends BaseRaw { - protected indexes: IndexSpecification[] = [{ key: { type: 1 } }]; + protected modelIndexes() { + return [{ key: { type: 1 } }]; + } findOneByUrl(url: string): Promise { return this.findOne({ url }); diff --git a/app/models/server/raw/LivechatTrigger.ts b/app/models/server/raw/LivechatTrigger.ts index e8311c5faef0..a582a06778d3 100644 --- a/app/models/server/raw/LivechatTrigger.ts +++ b/app/models/server/raw/LivechatTrigger.ts @@ -1,10 +1,12 @@ import { Cursor, UpdateWriteOpResult } from 'mongodb'; -import { BaseRaw, IndexSpecification } from './BaseRaw'; +import { BaseRaw } from './BaseRaw'; import { ILivechatTrigger } from '../../../../definition/ILivechatTrigger'; export class LivechatTriggerRaw extends BaseRaw { - protected indexes: IndexSpecification[] = [{ key: { enabled: 1 } }]; + protected modelIndexes() { + return [{ key: { enabled: 1 } }]; + } findEnabled(): Cursor { return this.find({ enabled: true }); diff --git a/app/models/server/raw/NotificationQueue.ts b/app/models/server/raw/NotificationQueue.ts index 23b820ab2393..c98f115e066b 100644 --- a/app/models/server/raw/NotificationQueue.ts +++ b/app/models/server/raw/NotificationQueue.ts @@ -1,16 +1,18 @@ import { UpdateWriteOpResult } from 'mongodb'; -import { BaseRaw, IndexSpecification } from './BaseRaw'; +import { BaseRaw } from './BaseRaw'; import { INotification } from '../../../../definition/INotification'; export class NotificationQueueRaw extends BaseRaw { - protected indexes: IndexSpecification[] = [ - { key: { uid: 1 } }, - { key: { ts: 1 }, expireAfterSeconds: 2 * 60 * 60 }, - { key: { schedule: 1 }, sparse: true }, - { key: { sending: 1 }, sparse: true }, - { key: { error: 1 }, sparse: true }, - ]; + protected modelIndexes() { + return [ + { key: { uid: 1 } }, + { key: { ts: 1 }, expireAfterSeconds: 2 * 60 * 60 }, + { key: { schedule: 1 }, sparse: true }, + { key: { sending: 1 }, sparse: true }, + { key: { error: 1 }, sparse: true }, + ]; + } unsetSendingById(_id: string): Promise { return this.updateOne( diff --git a/app/models/server/raw/OEmbedCache.ts b/app/models/server/raw/OEmbedCache.ts index e7a4ff2f301d..dc46ab0f2335 100644 --- a/app/models/server/raw/OEmbedCache.ts +++ b/app/models/server/raw/OEmbedCache.ts @@ -1,12 +1,14 @@ import { DeleteWriteOpResultObject } from 'mongodb'; -import { BaseRaw, IndexSpecification } from './BaseRaw'; +import { BaseRaw } from './BaseRaw'; import { IOEmbedCache } from '../../../../definition/IOEmbedCache'; type T = IOEmbedCache; export class OEmbedCacheRaw extends BaseRaw { - protected indexes: IndexSpecification[] = [{ key: { updatedAt: 1 } }]; + protected modelIndexes() { + return [{ key: { updatedAt: 1 } }]; + } async createWithIdAndData(_id: string, data: any): Promise { const record = { diff --git a/app/models/server/raw/PbxEvents.ts b/app/models/server/raw/PbxEvents.ts index 19ca711db9ed..56570bc0cf18 100644 --- a/app/models/server/raw/PbxEvents.ts +++ b/app/models/server/raw/PbxEvents.ts @@ -1,10 +1,12 @@ import { Cursor } from 'mongodb'; -import { BaseRaw, IndexSpecification } from './BaseRaw'; +import { BaseRaw } from './BaseRaw'; import { IPbxEvent } from '../../../../definition/IPbxEvent'; export class PbxEventsRaw extends BaseRaw { - protected indexes: IndexSpecification[] = [{ key: { uniqueId: 1 }, unique: true }]; + protected modelIndexes() { + return [{ key: { uniqueId: 1 }, unique: true }]; + } findByEvents(callUniqueId: string, events: string[]): Cursor { return this.find( diff --git a/app/models/server/raw/ReadReceipts.ts b/app/models/server/raw/ReadReceipts.ts index 768cb7c53263..1be190713da5 100644 --- a/app/models/server/raw/ReadReceipts.ts +++ b/app/models/server/raw/ReadReceipts.ts @@ -1,10 +1,12 @@ import { Cursor } from 'mongodb'; -import { BaseRaw, IndexSpecification } from './BaseRaw'; +import { BaseRaw } from './BaseRaw'; import { ReadReceipt } from '../../../../definition/ReadReceipt'; export class ReadReceiptsRaw extends BaseRaw { - protected indexes: IndexSpecification[] = [{ key: { roomId: 1, userId: 1, messageId: 1 }, unique: true }, { key: { messageId: 1 } }]; + protected modelIndexes() { + return [{ key: { roomId: 1, userId: 1, messageId: 1 }, unique: true }, { key: { messageId: 1 } }]; + } findByMessageId(messageId: string): Cursor { return this.find({ messageId }); diff --git a/app/models/server/raw/ServerEvents.ts b/app/models/server/raw/ServerEvents.ts index e40a5e0d5627..ef9731eb86b6 100644 --- a/app/models/server/raw/ServerEvents.ts +++ b/app/models/server/raw/ServerEvents.ts @@ -1,8 +1,10 @@ -import { BaseRaw, IndexSpecification } from './BaseRaw'; +import { BaseRaw } from './BaseRaw'; import { IServerEvent, IServerEventType } from '../../../../definition/IServerEvent'; export class ServerEventsRaw extends BaseRaw { - protected indexes: IndexSpecification[] = [{ key: { t: 1, ip: 1, ts: -1 } }, { key: { 't': 1, 'u.username': 1, 'ts': -1 } }]; + protected modelIndexes() { + return [{ key: { t: 1, ip: 1, ts: -1 } }, { key: { 't': 1, 'u.username': 1, 'ts': -1 } }]; + } async findLastFailedAttemptByIp(ip: string): Promise { return this.findOne( diff --git a/app/models/server/raw/Sessions.ts b/app/models/server/raw/Sessions.ts index 3b7be065d635..726e9407d3f7 100644 --- a/app/models/server/raw/Sessions.ts +++ b/app/models/server/raw/Sessions.ts @@ -3,7 +3,6 @@ import { BulkWriteOperation, BulkWriteOpResultObject, Collection, - IndexSpecification, UpdateWriteOpResult, FilterQuery, Cursor, @@ -738,18 +737,6 @@ export const aggregates = { }; export class SessionsRaw extends BaseRaw { - protected indexes: IndexSpecification[] = [ - { key: { instanceId: 1, sessionId: 1, year: 1, month: 1, day: 1 } }, - { key: { instanceId: 1, sessionId: 1, userId: 1 } }, - { key: { instanceId: 1, sessionId: 1 } }, - { key: { sessionId: 1 } }, - { key: { userId: 1 } }, - { key: { year: 1, month: 1, day: 1, type: 1 } }, - { key: { type: 1 } }, - { key: { ip: 1, loginAt: 1 } }, - { key: { _computedAt: 1 }, expireAfterSeconds: 60 * 60 * 24 * 45 }, - ]; - private secondaryCollection: Collection; constructor(public readonly col: Collection, public readonly colSecondary: Collection, trash?: Collection) { @@ -758,6 +745,20 @@ export class SessionsRaw extends BaseRaw { this.secondaryCollection = colSecondary; } + protected modelIndexes() { + return [ + { key: { instanceId: 1, sessionId: 1, year: 1, month: 1, day: 1 } }, + { key: { instanceId: 1, sessionId: 1, userId: 1 } }, + { key: { instanceId: 1, sessionId: 1 } }, + { key: { sessionId: 1 } }, + { key: { userId: 1 } }, + { key: { year: 1, month: 1, day: 1, type: 1 } }, + { key: { type: 1 } }, + { key: { ip: 1, loginAt: 1 } }, + { key: { _computedAt: 1 }, expireAfterSeconds: 60 * 60 * 24 * 45 }, + ]; + } + async getActiveUsersBetweenDates({ start, end }: DestructuredRange): Promise { return this.col .aggregate([ diff --git a/app/models/server/raw/Statistics.ts b/app/models/server/raw/Statistics.ts index 424cb3175efc..d332a7b652f2 100644 --- a/app/models/server/raw/Statistics.ts +++ b/app/models/server/raw/Statistics.ts @@ -1,8 +1,10 @@ -import { BaseRaw, IndexSpecification } from './BaseRaw'; +import { BaseRaw } from './BaseRaw'; import { IStatistic } from '../../../../definition/IStatistic'; export class StatisticsRaw extends BaseRaw { - protected indexes: IndexSpecification[] = [{ key: { createdAt: -1 } }]; + protected modelIndexes() { + return [{ key: { createdAt: -1 } }]; + } async findLast(): Promise { const options = { diff --git a/app/models/server/raw/Uploads.ts b/app/models/server/raw/Uploads.ts index 4bd0572bddd8..c8b9c0f23860 100644 --- a/app/models/server/raw/Uploads.ts +++ b/app/models/server/raw/Uploads.ts @@ -13,7 +13,7 @@ import { WriteOpResult, } from 'mongodb'; -import { BaseRaw, IndexSpecification, InsertionModel } from './BaseRaw'; +import { BaseRaw, InsertionModel } from './BaseRaw'; import { IUpload as T } from '../../../../definition/IUpload'; const fillTypeGroup = (fileData: Partial): void => { @@ -25,7 +25,9 @@ const fillTypeGroup = (fileData: Partial): void => { }; export class UploadsRaw extends BaseRaw { - protected indexes: IndexSpecification[] = [{ key: { rid: 1 } }, { key: { uploadedAt: 1 } }, { key: { typeGroup: 1 } }]; + protected modelIndexes() { + return [{ key: { rid: 1 } }, { key: { uploadedAt: 1 } }, { key: { typeGroup: 1 } }]; + } findNotHiddenFilesOfRoom(roomId: string, searchText: string, fileType: string, limit: number): Cursor { const fileQuery = { diff --git a/app/models/server/raw/UserDataFiles.ts b/app/models/server/raw/UserDataFiles.ts index 948456d6f77c..b29cc7d704ee 100644 --- a/app/models/server/raw/UserDataFiles.ts +++ b/app/models/server/raw/UserDataFiles.ts @@ -1,10 +1,12 @@ import { FindOneOptions, InsertOneWriteOpResult, WithId, WithoutProjection } from 'mongodb'; -import { BaseRaw, IndexSpecification } from './BaseRaw'; +import { BaseRaw } from './BaseRaw'; import { IUserDataFile as T } from '../../../../definition/IUserDataFile'; export class UserDataFilesRaw extends BaseRaw { - protected indexes: IndexSpecification[] = [{ key: { userId: 1 } }]; + protected modelIndexes() { + return [{ key: { userId: 1 } }]; + } findLastFileByUser(userId: string, options: WithoutProjection> = {}): Promise { const query = { From 196655a0ffcce6d6d7b9e8a4f4ad2defe45b5603 Mon Sep 17 00:00:00 2001 From: Diego Sampaio Date: Thu, 7 Apr 2022 15:52:27 -0300 Subject: [PATCH 2/2] Convert other indexes to same pattern --- app/models/server/raw/Banners.ts | 10 +++------- app/models/server/raw/BannersDismiss.ts | 8 +++----- app/models/server/raw/Nps.ts | 8 +++----- app/models/server/raw/NpsVote.ts | 8 +++----- app/models/server/raw/Team.ts | 20 +++----------------- app/models/server/raw/TeamMember.ts | 18 ++++++++++-------- app/models/server/raw/WebdavAccounts.ts | 8 +++----- 7 files changed, 28 insertions(+), 52 deletions(-) diff --git a/app/models/server/raw/Banners.ts b/app/models/server/raw/Banners.ts index c0285527d78d..6b1db0a94c38 100644 --- a/app/models/server/raw/Banners.ts +++ b/app/models/server/raw/Banners.ts @@ -1,16 +1,12 @@ -import { Collection, Cursor, FindOneOptions, UpdateWriteOpResult, WithoutProjection, InsertOneWriteOpResult } from 'mongodb'; +import { Cursor, FindOneOptions, UpdateWriteOpResult, WithoutProjection, InsertOneWriteOpResult } from 'mongodb'; import { BannerPlatform, IBanner } from '../../../../definition/IBanner'; import { BaseRaw } from './BaseRaw'; type T = IBanner; export class BannersRaw extends BaseRaw { - constructor(public readonly col: Collection, trash?: Collection) { - super(col, trash); - - this.col.createIndexes([{ key: { platform: 1, startAt: 1, expireAt: 1 } }]); - - this.col.createIndexes([{ key: { platform: 1, startAt: 1, expireAt: 1, active: 1 } }]); + protected modelIndexes() { + return [{ key: { platform: 1, startAt: 1, expireAt: 1 } }, { key: { platform: 1, startAt: 1, expireAt: 1, active: 1 } }]; } create(doc: IBanner): Promise> { diff --git a/app/models/server/raw/BannersDismiss.ts b/app/models/server/raw/BannersDismiss.ts index 61325e5423f3..007a7e155143 100644 --- a/app/models/server/raw/BannersDismiss.ts +++ b/app/models/server/raw/BannersDismiss.ts @@ -1,13 +1,11 @@ -import { Collection, Cursor, FindOneOptions, WithoutProjection } from 'mongodb'; +import { Cursor, FindOneOptions, WithoutProjection } from 'mongodb'; import { IBannerDismiss } from '../../../../definition/IBanner'; import { BaseRaw } from './BaseRaw'; export class BannersDismissRaw extends BaseRaw { - constructor(public readonly col: Collection, trash?: Collection) { - super(col, trash); - - this.col.createIndexes([{ key: { userId: 1, bannerId: 1 } }]); + modelIndexes() { + return [{ key: { userId: 1, bannerId: 1 } }]; } findByUserIdAndBannerId(userId: string, bannerIds: string[]): Cursor; diff --git a/app/models/server/raw/Nps.ts b/app/models/server/raw/Nps.ts index daf123b14d1c..513103057228 100644 --- a/app/models/server/raw/Nps.ts +++ b/app/models/server/raw/Nps.ts @@ -1,14 +1,12 @@ -import { UpdateWriteOpResult, Collection } from 'mongodb'; +import { UpdateWriteOpResult } from 'mongodb'; import { BaseRaw } from './BaseRaw'; import { INps, NPSStatus } from '../../../../definition/INps'; type T = INps; export class NpsRaw extends BaseRaw { - constructor(public readonly col: Collection, trash?: Collection) { - super(col, trash); - - this.col.createIndexes([{ key: { status: 1, expireAt: 1 } }]); + modelIndexes() { + return [{ key: { status: 1, expireAt: 1 } }]; } // get expired surveys still in progress diff --git a/app/models/server/raw/NpsVote.ts b/app/models/server/raw/NpsVote.ts index 5aa9c56bda88..19a6842894c0 100644 --- a/app/models/server/raw/NpsVote.ts +++ b/app/models/server/raw/NpsVote.ts @@ -1,14 +1,12 @@ -import { ObjectId, Collection, Cursor, FindOneOptions, UpdateWriteOpResult, WithoutProjection } from 'mongodb'; +import { ObjectId, Cursor, FindOneOptions, UpdateWriteOpResult, WithoutProjection } from 'mongodb'; import { INpsVote, INpsVoteStatus } from '../../../../definition/INps'; import { BaseRaw } from './BaseRaw'; type T = INpsVote; export class NpsVoteRaw extends BaseRaw { - constructor(public readonly col: Collection, trash?: Collection) { - super(col, trash); - - this.col.createIndexes([{ key: { npsId: 1, status: 1, sentAt: 1 } }, { key: { npsId: 1, identifier: 1 } }]); + modelIndexes() { + return [{ key: { npsId: 1, status: 1, sentAt: 1 } }, { key: { npsId: 1, identifier: 1 } }]; } findNotSentByNpsId(npsId: string, options?: WithoutProjection>): Cursor { diff --git a/app/models/server/raw/Team.ts b/app/models/server/raw/Team.ts index 15beb82c9f9d..d4037ac8001c 100644 --- a/app/models/server/raw/Team.ts +++ b/app/models/server/raw/Team.ts @@ -1,25 +1,11 @@ -import { - Collection, - WithoutProjection, - FindOneOptions, - Cursor, - UpdateWriteOpResult, - DeleteWriteOpResultObject, - FilterQuery, -} from 'mongodb'; +import { WithoutProjection, FindOneOptions, Cursor, UpdateWriteOpResult, DeleteWriteOpResultObject, FilterQuery } from 'mongodb'; import { BaseRaw } from './BaseRaw'; import { ITeam, TEAM_TYPE } from '../../../../definition/ITeam'; export class TeamRaw extends BaseRaw { - constructor(public readonly col: Collection, trash?: Collection) { - super(col, trash); - - this.col.createIndex({ name: 1 }, { unique: true }); - - // this.col.createIndexes([ - // { key: { status: 1, expireAt: 1 } }, - // ]); + protected modelIndexes() { + return [{ key: { name: 1 }, unique: true }]; } findByNames(names: Array): Cursor; diff --git a/app/models/server/raw/TeamMember.ts b/app/models/server/raw/TeamMember.ts index 16a5c1a76baa..d6d4b07a8afc 100644 --- a/app/models/server/raw/TeamMember.ts +++ b/app/models/server/raw/TeamMember.ts @@ -1,5 +1,4 @@ import { - Collection, WithoutProjection, FindOneOptions, Cursor, @@ -15,13 +14,16 @@ import type { IUser, IRole } from '../../../../definition/IUser'; type T = ITeamMember; export class TeamMemberRaw extends BaseRaw { - constructor(public readonly col: Collection, trash?: Collection) { - super(col, trash); - - this.col.createIndexes([{ key: { teamId: 1 } }]); - - // teamId => userId should be unique - this.col.createIndex({ teamId: 1, userId: 1 }, { unique: true }); + protected modelIndexes() { + return [ + { + key: { teamId: 1 }, + }, + { + key: { teamId: 1, userId: 1 }, + unique: true, + }, + ]; } findByUserId(userId: string): Cursor; diff --git a/app/models/server/raw/WebdavAccounts.ts b/app/models/server/raw/WebdavAccounts.ts index 9c3d0a8b0fdd..9bda5ee83e93 100644 --- a/app/models/server/raw/WebdavAccounts.ts +++ b/app/models/server/raw/WebdavAccounts.ts @@ -1,7 +1,7 @@ /** * Webdav Accounts model */ -import type { Collection, FindOneOptions, Cursor, DeleteWriteOpResultObject } from 'mongodb'; +import type { FindOneOptions, Cursor, DeleteWriteOpResultObject } from 'mongodb'; import { BaseRaw } from './BaseRaw'; import { IWebdavAccount } from '../../../../definition/IWebdavAccount'; @@ -9,10 +9,8 @@ import { IWebdavAccount } from '../../../../definition/IWebdavAccount'; type T = IWebdavAccount; export class WebdavAccountsRaw extends BaseRaw { - constructor(public readonly col: Collection, trash?: Collection) { - super(col, trash); - - this.col.createIndex({ userId: 1 }); + protected modelIndexes() { + return [{ key: { userId: 1 } }]; } findOneByIdAndUserId(_id: string, userId: string, options: FindOneOptions): Promise {