diff --git a/packages/models/src/models/BaseRaw.ts b/packages/models/src/models/BaseRaw.ts index 633e9fad3f3bc..2a0be9761b9a4 100644 --- a/packages/models/src/models/BaseRaw.ts +++ b/packages/models/src/models/BaseRaw.ts @@ -29,15 +29,16 @@ import type { ClientSession, } from 'mongodb'; -import { getCollectionName, UpdaterImpl } from '..'; +import { UpdaterImpl } from '../updater'; +const getCollectionName = (name: string): string => `rocketchat_${name}`; import type { Updater } from '../updater'; import { setUpdatedAt } from './setUpdatedAt'; const warnFields = process.env.NODE_ENV !== 'production' || process.env.SHOW_WARNINGS === 'true' ? (...rest: any): void => { - console.warn(...rest, new Error().stack); - } + console.warn(...rest, new Error().stack); + } : new Function(); type ModelOptions = { @@ -50,8 +51,7 @@ export abstract class BaseRaw< T extends { _id: string }, C extends DefaultFields = undefined, TDeleted extends RocketChatRecordDeleted = RocketChatRecordDeleted, -> implements IBaseModel -{ +> implements IBaseModel { protected defaultFields: C | undefined; public readonly col: Collection; @@ -324,9 +324,14 @@ export abstract class BaseRaw< } as unknown as TDeleted; // since the operation is not atomic, we need to make sure that the record is not already deleted/inserted - await this.trash?.updateOne({ _id } as Filter, { $set: trash } as UpdateFilter, { - upsert: true, - }); + await this.trash?.updateOne( + { _id } as Filter, + { $set: trash } as UpdateFilter, + { + upsert: true, + session: options?.session, + }, + ); } if (options) { @@ -340,7 +345,7 @@ export abstract class BaseRaw< return this.col.findOneAndDelete(filter, options || {}); } - const doc = await this.col.findOne(filter); + const doc = await this.col.findOne(filter, { session: options?.session }); if (!doc) { return null; } @@ -352,14 +357,19 @@ export abstract class BaseRaw< __collection__: this.name, } as unknown as TDeleted; - await this.trash?.updateOne({ _id } as Filter, { $set: trash } as UpdateFilter, { - upsert: true, - }); + await this.trash?.updateOne( + { _id } as Filter, + { $set: trash } as UpdateFilter, + { + upsert: true, + session: options?.session, + }, + ); try { - await this.col.deleteOne({ _id } as Filter); + await this.col.deleteOne({ _id } as Filter, { session: options?.session }); } catch (e) { - await this.trash?.deleteOne({ _id } as Filter); + await this.trash?.deleteOne({ _id } as Filter, { session: options?.session }); throw e; } @@ -389,10 +399,14 @@ export abstract class BaseRaw< ids.push(_id as T['_id']); // since the operation is not atomic, we need to make sure that the record is not already deleted/inserted - await this.trash?.updateOne({ _id } as Filter, { $set: trash } as UpdateFilter, { - upsert: true, - session: options?.session, - }); + await this.trash?.updateOne( + { _id } as Filter, + { $set: trash } as UpdateFilter, + { + upsert: true, + session: options?.session, + }, + ); void options?.onTrash?.(doc); }