Skip to content

Commit

Permalink
Server: Allow filtering users with replication
Browse files Browse the repository at this point in the history
  • Loading branch information
laurent22 committed May 12, 2024
1 parent d49b2ec commit 13116fe
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
30 changes: 30 additions & 0 deletions packages/server/src/models/BaseModel.test.ts
@@ -0,0 +1,30 @@
import { beforeAllDb, afterAllTests, beforeEachDb, models } from '../utils/testing/testUtils';

describe('BaseModel', () => {

beforeAll(async () => {
await beforeAllDb('BaseModel');
});

afterAll(async () => {
await afterAllTests();
});

beforeEach(async () => {
await beforeEachDb();
});

test('should check if a user has replication', async () => {
// cSpell:disable
const itemModel = models().item();
itemModel.usersWithReplication_ = ['A', 'B', 'EYE1m66mGmA01sDDDDKE19'];
expect(itemModel.isUserWithReplication('AAAAAAAAAAAA')).toBe(true);
expect(itemModel.isUserWithReplication('AAAAAAAAAAAAEEEEEEE')).toBe(true);
expect(itemModel.isUserWithReplication('bbbbbbbb')).toBe(false);
expect(itemModel.isUserWithReplication('BBBBBBBBBB')).toBe(true);
expect(itemModel.isUserWithReplication('')).toBe(false);
expect(itemModel.isUserWithReplication('EYE1m66mGmA01sDDDDKE19')).toBe(true);
// cSpell:enable
});

});
13 changes: 12 additions & 1 deletion packages/server/src/models/BaseModel.ts
Expand Up @@ -117,8 +117,19 @@ export default abstract class BaseModel<T> {
return this.db_;
}

public isUserWithReplication(userId: Uuid) {
if (!userId) return false;

for (const id of this.usersWithReplication_) {
if (id === userId) return true;
if (id.length < 22 && userId.startsWith(id)) return true;
}

return false;
}

public dbSlave(userId: Uuid = ''): DbConnection {
if (userId && this.usersWithReplication_.includes(userId)) {
if (this.isUserWithReplication(userId)) {
logger.info(`Using slave database for user: ${userId}`);
return this.dbSlave_;
}
Expand Down

0 comments on commit 13116fe

Please sign in to comment.