Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: create orm for specific cases #32948

Merged
merged 12 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from 10 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
7 changes: 6 additions & 1 deletion apps/meteor/server/models/raw/BaseRaw.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { RocketChatRecordDeleted } from '@rocket.chat/core-typings';
import type { IBaseModel, DefaultFields, ResultFields, FindPaginated, InsertionModel } from '@rocket.chat/model-typings';
import { getCollectionName } from '@rocket.chat/models';
import type { Updater } from '@rocket.chat/models';
import { getCollectionName, UpdaterImpl } from '@rocket.chat/models';
import { ObjectId } from 'mongodb';
import type {
BulkWriteOptions,
Expand Down Expand Up @@ -109,6 +110,10 @@ export abstract class BaseRaw<
return this.collectionName;
}

protected getUpdater(): Updater<T> {
return new UpdaterImpl<T>(this.col as unknown as IBaseModel<T>);
}

private doNotMixInclusionAndExclusionFields(options: FindOptions<T> = {}): FindOptions<T> {
const optionsDef = this.ensureDefaultFields(options);
if (optionsDef?.projection === undefined) {
Expand Down
1 change: 1 addition & 0 deletions packages/model-typings/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,4 @@ export * from './models/IAuditLogModel';
export * from './models/ICronHistoryModel';
export * from './models/IMigrationsModel';
export * from './models/IModerationReportsModel';
export * from './updater';
36 changes: 36 additions & 0 deletions packages/model-typings/src/updater.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* eslint-disable @typescript-eslint/naming-convention */
import type { Join, NestedPaths, PropertyType, ArrayElement, NestedPathsOfType, Filter } from 'mongodb';

export interface Updater<T extends { _id: string }> {
set<P extends SetProps<T>, K extends keyof P>(key: K, value: P[K]): Updater<T>;
unset<K extends keyof UnsetProps<T>>(key: K): Updater<T>;
inc<K extends keyof IncProps<T>>(key: K, value: number): Updater<T>;
addToSet<K extends keyof AddToSetProps<T>>(key: K, value: AddToSetProps<T>[K]): Updater<T>;
persist(query: Filter<T>): Promise<void>;
hasChanges(): boolean;
}

export type SetProps<TSchema extends { _id: string }> = Readonly<
{
[Property in Join<NestedPaths<TSchema, []>, '.'>]: PropertyType<TSchema, Property>;
} & {
[Property in `${NestedPathsOfType<TSchema, any[]>}.$${`[${string}]` | ''}`]: ArrayElement<
PropertyType<TSchema, Property extends `${infer Key}.$${string}` ? Key : never>
>;
} & {
[Property in `${NestedPathsOfType<TSchema, Record<string, any>[]>}.$${`[${string}]` | ''}.${string}`]: any;
}
>;

type GetType<T, K> = {
[Key in keyof T]: K extends T[Key] ? T[Key] : never;
};

type OmitNever<T> = { [K in keyof T as T[K] extends never ? never : K]: T[K] };

// only allow optional properties
export type UnsetProps<TSchema extends { _id: string }> = OmitNever<GetType<SetProps<TSchema>, undefined>>;

export type IncProps<TSchema extends { _id: string }> = OmitNever<GetType<SetProps<TSchema>, number>>;

export type AddToSetProps<TSchema extends { _id: string }> = OmitNever<GetType<SetProps<TSchema>, any[]>>;
2 changes: 1 addition & 1 deletion packages/models/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"@swc/jest": "^0.2.29",
"@types/jest": "~29.5.12",
"eslint": "~8.45.0",
"jest": "~29.7.0",
"jest": "^29.7.0",
"ts-jest": "~29.1.1",
"typescript": "~5.3.3"
},
Expand Down
1 change: 1 addition & 0 deletions packages/models/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export function getCollectionName(name: string): string {
}

export { registerModel } from './proxify';
export { type Updater, UpdaterImpl } from './updater';

export const Apps = proxify<IAppsModel>('IAppsModel');
export const AppsTokens = proxify<IAppsTokensModel>('IAppsTokensModel');
Expand Down
192 changes: 192 additions & 0 deletions packages/models/src/updater.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
import { UpdaterImpl } from './updater';

test('updater typings', () => {
const updater = new UpdaterImpl<{
_id: string;
t: 'l';
a: {
b: string;
};
c?: number;

d?: {
e: string;
};
e: string[];
}>({} as any);

updater.addToSet('e', 'a');

// @ts-expect-error

Check warning on line 20 in packages/models/src/updater.spec.ts

View workflow job for this annotation

GitHub Actions / 🔎 Code Check / Code Lint

Include a description after the "@ts-expect-error" directive to explain why the @ts-expect-error is necessary. The description must be 3 characters or longer
updater.addToSet('e', 1);
// @ts-expect-error

Check warning on line 22 in packages/models/src/updater.spec.ts

View workflow job for this annotation

GitHub Actions / 🔎 Code Check / Code Lint

Include a description after the "@ts-expect-error" directive to explain why the @ts-expect-error is necessary. The description must be 3 characters or longer
updater.addToSet('a', 'b');

// @ts-expect-error

Check warning on line 25 in packages/models/src/updater.spec.ts

View workflow job for this annotation

GitHub Actions / 🔎 Code Check / Code Lint

Include a description after the "@ts-expect-error" directive to explain why the @ts-expect-error is necessary. The description must be 3 characters or longer
updater.set('njame', 1);
// @ts-expect-error

Check warning on line 27 in packages/models/src/updater.spec.ts

View workflow job for this annotation

GitHub Actions / 🔎 Code Check / Code Lint

Include a description after the "@ts-expect-error" directive to explain why the @ts-expect-error is necessary. The description must be 3 characters or longer
updater.set('ttes', 1);
// @ts-expect-error

Check warning on line 29 in packages/models/src/updater.spec.ts

View workflow job for this annotation

GitHub Actions / 🔎 Code Check / Code Lint

Include a description after the "@ts-expect-error" directive to explain why the @ts-expect-error is necessary. The description must be 3 characters or longer
updater.set('t', 'a');
updater.set('t', 'l');
// @ts-expect-error

Check warning on line 32 in packages/models/src/updater.spec.ts

View workflow job for this annotation

GitHub Actions / 🔎 Code Check / Code Lint

Include a description after the "@ts-expect-error" directive to explain why the @ts-expect-error is necessary. The description must be 3 characters or longer
updater.set('a', 'b');
// @ts-expect-error

Check warning on line 34 in packages/models/src/updater.spec.ts

View workflow job for this annotation

GitHub Actions / 🔎 Code Check / Code Lint

Include a description after the "@ts-expect-error" directive to explain why the @ts-expect-error is necessary. The description must be 3 characters or longer
updater.set('c', 'b');
updater.set('c', 1);

updater.set('a', {
b: 'set',
});
updater.set('a.b', 'test');

// @ts-expect-error

Check warning on line 43 in packages/models/src/updater.spec.ts

View workflow job for this annotation

GitHub Actions / 🔎 Code Check / Code Lint

Include a description after the "@ts-expect-error" directive to explain why the @ts-expect-error is necessary. The description must be 3 characters or longer
updater.unset('a');

updater.unset('c');

updater.unset('d');

updater.unset('d.e');
// @ts-expect-error

Check warning on line 51 in packages/models/src/updater.spec.ts

View workflow job for this annotation

GitHub Actions / 🔎 Code Check / Code Lint

Include a description after the "@ts-expect-error" directive to explain why the @ts-expect-error is necessary. The description must be 3 characters or longer
updater.inc('d', 1);
updater.inc('c', 1);
});

test('updater $set operations', async () => {
const updateOne = jest.fn();

const updater = new UpdaterImpl<{
_id: string;
t: 'l';
a: {
b: string;
};
c?: number;
}>({
updateOne,
} as any);

updater.set('a', {
b: 'set',
});

await updater.persist({
_id: 'test',
});

expect(updateOne).toBeCalledWith(
{
_id: 'test',
},
{ $set: { a: { b: 'set' } } },
);
});

test('updater $unset operations', async () => {
const updateOne = jest.fn();

const updater = new UpdaterImpl<{
_id: string;
t: 'l';
a: {
b: string;
};
c?: number;
}>({
updateOne,
} as any);

updater.unset('c');

await updater.persist({
_id: 'test',
});

expect(updateOne).toBeCalledWith(
{
_id: 'test',
},
{ $unset: { c: 1 } },
);
});

test('updater inc multiple operations', async () => {
const updateOne = jest.fn();

const updater = new UpdaterImpl<{
_id: string;
t: 'l';
a: {
b: string;
};
c?: number;
}>({
updateOne,
} as any);

updater.inc('c', 1);
updater.inc('c', 1);

await updater.persist({
_id: 'test',
});

expect(updateOne).toBeCalledWith(
{
_id: 'test',
},
{ $inc: { c: 2 } },
);
});

test('it should add items to array', async () => {
const updateOne = jest.fn();
const updater = new UpdaterImpl<{
_id: string;
a: string[];
}>({
updateOne,
} as any);

updater.addToSet('a', 'b');
updater.addToSet('a', 'c');

await updater.persist({
_id: 'test',
});

expect(updateOne).toBeCalledWith(
{
_id: 'test',
},
{ $addToSet: { $each: { a: ['b', 'c'] } } },
);
});

test('it should persist only once', async () => {
const updateOne = jest.fn();

const updater = new UpdaterImpl<{
_id: string;
t: 'l';
a: {
b: string;
};
c?: number;
}>({
updateOne,
} as any);

updater.set('a', {
b: 'set',
});

await updater.persist({
_id: 'test',
});

expect(updateOne).toBeCalledTimes(1);

expect(() => updater.persist({ _id: 'test' })).rejects.toThrow();
});
103 changes: 103 additions & 0 deletions packages/models/src/updater.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/* eslint-disable @typescript-eslint/naming-convention */
import type { IBaseModel, Updater, SetProps, UnsetProps, IncProps, AddToSetProps } from '@rocket.chat/model-typings';
import type { UpdateFilter, Filter } from 'mongodb';

type ArrayElementType<T> = T extends (infer E)[] ? E : T;

type Keys<T extends { _id: string }> = keyof SetProps<T>;

export class UpdaterImpl<T extends { _id: string }> implements Updater<T> {
private _set: Map<Keys<T>, any> | undefined;

private _unset: Set<keyof UnsetProps<T>> | undefined;

private _inc: Map<keyof IncProps<T>, number> | undefined;

private _addToSet: Map<keyof AddToSetProps<T>, any[]> | undefined;

private dirty = false;

constructor(private model: IBaseModel<T>) {}

set<P extends SetProps<T>, K extends keyof P>(key: K, value: P[K]) {
this._set = this._set ?? new Map<Keys<T>, any>();
this._set.set(key as Keys<T>, value);
return this;
}

unset<K extends keyof UnsetProps<T>>(key: K): Updater<T> {
this._unset = this._unset ?? new Set<keyof UnsetProps<T>>();
this._unset.add(key);
return this;
}

test1<K extends UpdateFilter<T>>(key: K) {
console.log(key);
}

ggazzo marked this conversation as resolved.
Show resolved Hide resolved
inc<K extends keyof IncProps<T>>(key: K, value: number): Updater<T> {
this._inc = this._inc ?? new Map<keyof IncProps<T>, number>();

const prev = this._inc.get(key) ?? 0;
this._inc.set(key, prev + value);
return this;
}

addToSet<K extends keyof AddToSetProps<T>>(key: K, value: ArrayElementType<AddToSetProps<T>[K]>): Updater<T> {
this._addToSet = this._addToSet ?? new Map<keyof AddToSetProps<T>, any[]>();

const prev = this._addToSet.get(key) ?? [];
this._addToSet.set(key, [...prev, value]);
return this;
}

async persist(query: Filter<T>): Promise<void> {
if (this.dirty) {
throw new Error('Updater is not dirty');
}

if ((process.env.NODE_ENV === 'development' || process.env.TEST_MODE) && !this.hasChanges()) {
throw new Error('Nothing to update');
}

this.dirty = true;

try {
await this.model.updateOne(query, {
...(this._set && { $set: Object.fromEntries(this._set) }),
...(this._unset && { $unset: Object.fromEntries([...this._unset.values()].map((k) => [k, 1])) }),
...(this._inc && { $inc: Object.fromEntries(this._inc) }),
...(this._addToSet && { $addToSet: { $each: Object.fromEntries(this._addToSet) } }),
ggazzo marked this conversation as resolved.
Show resolved Hide resolved
} as unknown as UpdateFilter<T>);
} catch (error) {
console.error(
'Failed to update',
JSON.stringify(query),
JSON.stringify(
{
...(this._set && { $set: Object.fromEntries(this._set) }),
...(this._unset && { $unset: Object.fromEntries([...this._unset.values()].map((k) => [k, 1])) }),
...(this._inc && { $inc: Object.fromEntries(this._inc) }),
...(this._addToSet && { $addToSet: { $each: Object.fromEntries(this._addToSet) } }),
},
null,
2,
),
);
throw error;
}
}

hasChanges() {
const update = {
...(this._set && { $set: Object.fromEntries(this._set) }),
...(this._unset && { $unset: Object.fromEntries([...this._unset.values()].map((k) => [k, 1])) }),
...(this._inc && { $inc: Object.fromEntries(this._inc) }),
...(this._addToSet && { $addToSet: { $each: Object.fromEntries(this._addToSet) } }),
} as unknown as UpdateFilter<T>;

return Object.keys(update).length > 0;
}
}

export { Updater };
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9774,7 +9774,7 @@ __metadata:
"@swc/jest": ^0.2.29
"@types/jest": ~29.5.12
eslint: ~8.45.0
jest: ~29.7.0
jest: ^29.7.0
ts-jest: ~29.1.1
typescript: ~5.3.3
languageName: unknown
Expand Down Expand Up @@ -28453,7 +28453,7 @@ __metadata:
languageName: node
linkType: hard

"jest@npm:~29.7.0":
"jest@npm:^29.7.0, jest@npm:~29.7.0":
version: 29.7.0
resolution: "jest@npm:29.7.0"
dependencies:
Expand Down
Loading