From d732622e19833b2851bc83eb7397dbc3369c502c Mon Sep 17 00:00:00 2001 From: Guilherme Gazzo Date: Fri, 8 Dec 2023 14:35:33 -0300 Subject: [PATCH 1/3] .. --- apps/meteor/server/models/raw/BaseRaw.ts | 14 +++++++++----- apps/meteor/server/startup/migrations/v304.ts | 1 + packages/model-typings/src/models/IBaseModel.ts | 2 ++ 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/apps/meteor/server/models/raw/BaseRaw.ts b/apps/meteor/server/models/raw/BaseRaw.ts index bfa5a81ddcf03..131c213d6b775 100644 --- a/apps/meteor/server/models/raw/BaseRaw.ts +++ b/apps/meteor/server/models/raw/BaseRaw.ts @@ -66,14 +66,20 @@ export abstract class BaseRaw< * @param trash Trash collection instance * @param options Model options */ - constructor(private db: Db, protected name: string, protected trash?: Collection, options?: ModelOptions) { + constructor(private db: Db, protected name: string, protected trash?: Collection, private options?: ModelOptions) { this.collectionName = options?.collectionNameResolver ? options.collectionNameResolver(name) : getCollectionName(name); this.col = this.db.collection(this.collectionName, options?.collection || {}); + void this.createIndexes(); + + this.preventSetUpdatedAt = options?.preventSetUpdatedAt ?? false; + } + + public async createIndexes() { const indexes = this.modelIndexes(); - if (options?._updatedAtIndexOptions) { - indexes?.push({ ...options._updatedAtIndexOptions, key: { _updatedAt: 1 } }); + if (this.options?._updatedAtIndexOptions) { + indexes?.push({ ...this.options._updatedAtIndexOptions, key: { _updatedAt: 1 } }); } if (indexes?.length) { @@ -81,8 +87,6 @@ export abstract class BaseRaw< console.warn(`Some indexes for collection '${this.collectionName}' could not be created:\n\t${e.message}`); }); } - - this.preventSetUpdatedAt = options?.preventSetUpdatedAt ?? false; } protected modelIndexes(): IndexDescription[] | undefined { diff --git a/apps/meteor/server/startup/migrations/v304.ts b/apps/meteor/server/startup/migrations/v304.ts index e5d6484446f05..db9aa44b4ee05 100644 --- a/apps/meteor/server/startup/migrations/v304.ts +++ b/apps/meteor/server/startup/migrations/v304.ts @@ -7,5 +7,6 @@ addMigration({ name: 'Drop wrong index from analytics collection', async up() { await Analytics.col.dropIndex('room._id_1_date_1'); + await Analytics.createIndexes(); }, }); diff --git a/packages/model-typings/src/models/IBaseModel.ts b/packages/model-typings/src/models/IBaseModel.ts index e0ae05cba0c5e..6f71c06d21b1e 100644 --- a/packages/model-typings/src/models/IBaseModel.ts +++ b/packages/model-typings/src/models/IBaseModel.ts @@ -45,6 +45,8 @@ export interface IBaseModel< > { col: Collection; + createIndexes(): Promise; + getCollectionName(): string; findOneAndUpdate(query: Filter, update: UpdateFilter | T, options?: FindOneAndUpdateOptions): Promise>; From 655f9330a62a036e4f0752c2bc6c383bf05690cc Mon Sep 17 00:00:00 2001 From: Guilherme Gazzo Date: Fri, 8 Dec 2023 15:11:34 -0300 Subject: [PATCH 2/3] .. --- apps/meteor/server/models/raw/BaseRaw.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/apps/meteor/server/models/raw/BaseRaw.ts b/apps/meteor/server/models/raw/BaseRaw.ts index 131c213d6b775..ce3e0f224e1e6 100644 --- a/apps/meteor/server/models/raw/BaseRaw.ts +++ b/apps/meteor/server/models/raw/BaseRaw.ts @@ -71,7 +71,9 @@ export abstract class BaseRaw< this.col = this.db.collection(this.collectionName, options?.collection || {}); - void this.createIndexes(); + void this.createIndexes().catch((e) => { + console.warn(`Some indexes for collection '${this.collectionName}' could not be created:\n\t${e.message}`); + }); this.preventSetUpdatedAt = options?.preventSetUpdatedAt ?? false; } @@ -83,9 +85,7 @@ export abstract class BaseRaw< } if (indexes?.length) { - this.col.createIndexes(indexes).catch((e) => { - console.warn(`Some indexes for collection '${this.collectionName}' could not be created:\n\t${e.message}`); - }); + return this.col.createIndexes(indexes); } } From 9ed75a6310145a824b483bbe0936cce8f32d78b5 Mon Sep 17 00:00:00 2001 From: Diego Sampaio Date: Fri, 8 Dec 2023 16:08:57 -0300 Subject: [PATCH 3/3] fix BaseModel interface --- packages/model-typings/src/models/IBaseModel.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/model-typings/src/models/IBaseModel.ts b/packages/model-typings/src/models/IBaseModel.ts index 6f71c06d21b1e..36f34a0085082 100644 --- a/packages/model-typings/src/models/IBaseModel.ts +++ b/packages/model-typings/src/models/IBaseModel.ts @@ -45,7 +45,7 @@ export interface IBaseModel< > { col: Collection; - createIndexes(): Promise; + createIndexes(): Promise; getCollectionName(): string;