Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 32 additions & 18 deletions packages/models/src/models/BaseRaw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -50,8 +51,7 @@ export abstract class BaseRaw<
T extends { _id: string },
C extends DefaultFields<T> = undefined,
TDeleted extends RocketChatRecordDeleted<T> = RocketChatRecordDeleted<T>,
> implements IBaseModel<T, C, TDeleted>
{
> implements IBaseModel<T, C, TDeleted> {
protected defaultFields: C | undefined;

public readonly col: Collection<T>;
Expand Down Expand Up @@ -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<TDeleted>, { $set: trash } as UpdateFilter<TDeleted>, {
upsert: true,
});
await this.trash?.updateOne(
{ _id } as Filter<TDeleted>,
{ $set: trash } as UpdateFilter<TDeleted>,
{
upsert: true,
session: options?.session,
},
);
}

if (options) {
Expand All @@ -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;
}
Expand All @@ -352,14 +357,19 @@ export abstract class BaseRaw<
__collection__: this.name,
} as unknown as TDeleted;

await this.trash?.updateOne({ _id } as Filter<TDeleted>, { $set: trash } as UpdateFilter<TDeleted>, {
upsert: true,
});
await this.trash?.updateOne(
{ _id } as Filter<TDeleted>,
{ $set: trash } as UpdateFilter<TDeleted>,
{
upsert: true,
session: options?.session,
},
);

try {
await this.col.deleteOne({ _id } as Filter<T>);
await this.col.deleteOne({ _id } as Filter<T>, { session: options?.session });
} catch (e) {
await this.trash?.deleteOne({ _id } as Filter<TDeleted>);
await this.trash?.deleteOne({ _id } as Filter<TDeleted>, { session: options?.session });
throw e;
}

Expand Down Expand Up @@ -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<TDeleted>, { $set: trash } as UpdateFilter<TDeleted>, {
upsert: true,
session: options?.session,
});
await this.trash?.updateOne(
{ _id } as Filter<TDeleted>,
{ $set: trash } as UpdateFilter<TDeleted>,
{
upsert: true,
session: options?.session,
},
);

void options?.onTrash?.(doc);
}
Expand Down