Skip to content

Commit

Permalink
chore: move common files to core-services (#33341)
Browse files Browse the repository at this point in the history
  • Loading branch information
sampaiodiego authored Sep 24, 2024
1 parent 1f89f78 commit a4fcd99
Show file tree
Hide file tree
Showing 10 changed files with 20 additions and 60 deletions.
8 changes: 2 additions & 6 deletions _templates/service/new/service.ejs.t
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
---
to: ee/apps/<%= name %>/src/service.ts
---
import { api } from '@rocket.chat/core-services';
import { api, getConnection, getTrashCollection } from '@rocket.chat/core-services';
import { broker } from '@rocket.chat/network-broker';
import type { Document } from 'mongodb';
import polka from 'polka';

import { Collections, getCollection, getConnection } from '../../../../apps/meteor/ee/server/services/mongo';
import { registerServiceModels } from '../../../../apps/meteor/ee/server/lib/registerServiceModels';

const PORT = process.env.PORT || <%= h.random() %>;

(async () => {
const db = await getConnection();

const trash = await getCollection<Document>(Collections.Trash);

registerServiceModels(db, trash);
registerServiceModels(db, await getTrashCollection());

api.setBroker(broker);

Expand Down
8 changes: 2 additions & 6 deletions ee/apps/account-service/src/service.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
import { api } from '@rocket.chat/core-services';
import { api, getConnection, getTrashCollection } from '@rocket.chat/core-services';
import { broker } from '@rocket.chat/network-broker';
import type { Document } from 'mongodb';
import polka from 'polka';

import { registerServiceModels } from '../../../../apps/meteor/ee/server/lib/registerServiceModels';
import { Collections, getCollection, getConnection } from '../../../../apps/meteor/ee/server/services/mongo';

const PORT = process.env.PORT || 3033;

(async () => {
const db = await getConnection();

const trash = await getCollection<Document>(Collections.Trash);

registerServiceModels(db, trash);
registerServiceModels(db, await getTrashCollection());

api.setBroker(broker);

Expand Down
8 changes: 2 additions & 6 deletions ee/apps/authorization-service/src/service.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
import { api } from '@rocket.chat/core-services';
import { api, getConnection, getTrashCollection } from '@rocket.chat/core-services';
import { broker } from '@rocket.chat/network-broker';
import type { Document } from 'mongodb';
import polka from 'polka';

import { registerServiceModels } from '../../../../apps/meteor/ee/server/lib/registerServiceModels';
import { Collections, getCollection, getConnection } from '../../../../apps/meteor/ee/server/services/mongo';

const PORT = process.env.PORT || 3034;

(async () => {
const db = await getConnection();

const trash = await getCollection<Document>(Collections.Trash);

registerServiceModels(db, trash);
registerServiceModels(db, await getTrashCollection());

api.setBroker(broker);

Expand Down
8 changes: 2 additions & 6 deletions ee/apps/ddp-streamer/src/service.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import { api } from '@rocket.chat/core-services';
import { api, getConnection, getTrashCollection } from '@rocket.chat/core-services';
import { broker } from '@rocket.chat/network-broker';
import type { Document } from 'mongodb';

import { registerServiceModels } from '../../../../apps/meteor/ee/server/lib/registerServiceModels';
import { Collections, getCollection, getConnection } from '../../../../apps/meteor/ee/server/services/mongo';

(async () => {
const db = await getConnection();

const trash = await getCollection<Document>(Collections.Trash);

registerServiceModels(db, trash);
registerServiceModels(db, await getTrashCollection());

api.setBroker(broker);

Expand Down
8 changes: 2 additions & 6 deletions ee/apps/omnichannel-transcript/src/service.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
import { api } from '@rocket.chat/core-services';
import { api, getConnection, getTrashCollection } from '@rocket.chat/core-services';
import { Logger } from '@rocket.chat/logger';
import { broker } from '@rocket.chat/network-broker';
import type { Document } from 'mongodb';
import polka from 'polka';

import { registerServiceModels } from '../../../../apps/meteor/ee/server/lib/registerServiceModels';
import { Collections, getCollection, getConnection } from '../../../../apps/meteor/ee/server/services/mongo';

const PORT = process.env.PORT || 3036;

(async () => {
const db = await getConnection();

const trash = await getCollection<Document>(Collections.Trash);

registerServiceModels(db, trash);
registerServiceModels(db, await getTrashCollection());

api.setBroker(broker);

Expand Down
8 changes: 2 additions & 6 deletions ee/apps/presence-service/src/service.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
import { api } from '@rocket.chat/core-services';
import { api, getConnection, getTrashCollection } from '@rocket.chat/core-services';
import { broker } from '@rocket.chat/network-broker';
import type { Document } from 'mongodb';
import polka from 'polka';

import { registerServiceModels } from '../../../../apps/meteor/ee/server/lib/registerServiceModels';
import { Collections, getCollection, getConnection } from '../../../../apps/meteor/ee/server/services/mongo';

const PORT = process.env.PORT || 3031;

(async () => {
const db = await getConnection();

const trash = await getCollection<Document>(Collections.Trash);

registerServiceModels(db, trash);
registerServiceModels(db, await getTrashCollection());

api.setBroker(broker);

Expand Down
8 changes: 2 additions & 6 deletions ee/apps/queue-worker/src/service.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
import { api } from '@rocket.chat/core-services';
import { api, getConnection, getTrashCollection } from '@rocket.chat/core-services';
import { Logger } from '@rocket.chat/logger';
import { broker } from '@rocket.chat/network-broker';
import type { Document } from 'mongodb';
import polka from 'polka';

import { registerServiceModels } from '../../../../apps/meteor/ee/server/lib/registerServiceModels';
import { Collections, getCollection, getConnection } from '../../../../apps/meteor/ee/server/services/mongo';

const PORT = process.env.PORT || 3038;

(async () => {
const db = await getConnection();

const trash = await getCollection<Document>(Collections.Trash);

registerServiceModels(db, trash);
registerServiceModels(db, await getTrashCollection());

api.setBroker(broker);

Expand Down
8 changes: 2 additions & 6 deletions ee/apps/stream-hub-service/src/service.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { api } from '@rocket.chat/core-services';
import { api, getConnection, getTrashCollection } from '@rocket.chat/core-services';
import { Logger } from '@rocket.chat/logger';
import { broker } from '@rocket.chat/network-broker';
import type { Document } from 'mongodb';
import polka from 'polka';

import { registerServiceModels } from '../../../../apps/meteor/ee/server/lib/registerServiceModels';
import { Collections, getCollection, getConnection } from '../../../../apps/meteor/ee/server/services/mongo';
import { DatabaseWatcher } from '../../../../apps/meteor/server/database/DatabaseWatcher';
import { StreamHub } from './StreamHub';

Expand All @@ -14,9 +12,7 @@ const PORT = process.env.PORT || 3035;
(async () => {
const db = await getConnection();

const trash = await getCollection<Document>(Collections.Trash);

registerServiceModels(db, trash);
registerServiceModels(db, await getTrashCollection());

api.setBroker(broker);

Expand Down
2 changes: 2 additions & 0 deletions packages/core-services/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ export {
AnalyticsOverviewDataResult,
} from './types/IOmnichannelAnalyticsService';

export { getConnection, getTrashCollection } from './lib/mongo';

export {
AutoUpdateRecord,
FindVoipRoomsParams,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,6 @@ const { MONGO_URL = 'mongodb://localhost:27017/rocketchat' } = process.env;

const name = /^mongodb:\/\/.*?(?::[0-9]+)?\/([^?]*)/.exec(MONGO_URL)?.[1];

export enum Collections {
Subscriptions = 'rocketchat_subscription',
UserSession = 'usersSessions',
User = 'users',
Trash = 'rocketchat__trash',
Messages = 'rocketchat_message',
Rooms = 'rocketchat_room',
Settings = 'rocketchat_settings',
}

function connectDb(options?: MongoClientOptions): Promise<MongoClient> {
const client = new MongoClient(MONGO_URL, options);

Expand Down Expand Up @@ -44,9 +34,9 @@ export const getConnection = ((): ((options?: MongoClientOptions) => Promise<Db>
};
})();

export async function getCollection<T extends Document>(name: Collections): Promise<Collection<T>> {
export async function getTrashCollection<T extends Document>(): Promise<Collection<T>> {
if (!db) {
db = await getConnection();
}
return db.collection<T>(name);
return db.collection<T>('rocketchat__trash');
}

0 comments on commit a4fcd99

Please sign in to comment.