Skip to content

Commit

Permalink
feat: add init interface
Browse files Browse the repository at this point in the history
  • Loading branch information
gtoselli authored and lucagiove committed Jan 10, 2024
1 parent 18ad06a commit cf803b5
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 7 deletions.
3 changes: 3 additions & 0 deletions src/init.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface IInit {
init(): Promise<void> | void;
}
5 changes: 3 additions & 2 deletions src/repo/mongo-aggregate-repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ISerializer } from './serializer.interface';
import { merge } from 'lodash';
import { DuplicatedIdError, OptimisticLockError, RepoHookError } from '../errors';
import { ILogger } from './logger';
import { IInit } from '../init.interface';

export interface IAggregateRepo<A> {
// TODO add id as a generic type
Expand All @@ -20,7 +21,7 @@ type WithOptionalVersion<T> = T & { __version?: number };
// TODO probably we should create a dedicated interface whit like DocumentWithIdAndTimestamps
const MONGODB_UNIQUE_INDEX_CONSTRAINT_ERROR = 11000;

export class MongoAggregateRepo<A, AM extends DocumentWithId> implements IAggregateRepo<A> {
export class MongoAggregateRepo<A, AM extends DocumentWithId> implements IAggregateRepo<A>, IInit {
protected collection: Collection<AM>;

constructor(
Expand All @@ -33,7 +34,7 @@ export class MongoAggregateRepo<A, AM extends DocumentWithId> implements IAggreg
this.collection = this.mongoClient.db().collection(this.collectionName);
}

async onModuleInit() {
async init() {
await this.collection.createIndex({ id: 1 }, { unique: true });
}

Expand Down
5 changes: 3 additions & 2 deletions src/repo/mongo-query-repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { CreateIndexesOptions, Document, IndexSpecification, MongoClient } from
import { isEmpty } from 'lodash';
import { LoggedMongoCollection } from './logged-mongo-collection';
import { ILogger } from './logger';
import { IInit } from '../init.interface';

export abstract class MongoQueryRepo<RM extends Document> {
export abstract class MongoQueryRepo<RM extends Document> implements IInit {
protected readonly collection: LoggedMongoCollection<RM>;
protected abstract readonly indexes: { indexSpec: IndexSpecification; options?: CreateIndexesOptions }[];

Expand All @@ -15,7 +16,7 @@ export abstract class MongoQueryRepo<RM extends Document> {
this.collection = new LoggedMongoCollection(mongoClient.db().collection(this.collectionName), this.logger);
}

async onModuleInit() {
async init() {
await this.collection.createIndex({ id: 1 }, { unique: true });
if (!isEmpty(this.indexes)) {
for (const { indexSpec, options } of this.indexes) {
Expand Down
2 changes: 1 addition & 1 deletion src/repo/tests/mongo-aggregate-repo.integration-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('MongoAggregateRepo MongoDB Integration', () => {
mongoClient,
'collectionName',
);
await aggregateRepo.onModuleInit();
await aggregateRepo.init();
});

afterEach(async () => {
Expand Down
4 changes: 2 additions & 2 deletions src/repo/tests/mongo-aggregate-repo.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ describe('MongoAggregateRepo', () => {
});
});

describe('onModuleInit', () => {
describe('init', () => {
it('should call createIndex to create an index for id field', async () => {
await mongoAggregateRepo.onModuleInit();
await mongoAggregateRepo.init();
expect(collectionMock.createIndex).toHaveBeenCalledWith({ id: 1 }, { unique: true });
});
});
Expand Down

0 comments on commit cf803b5

Please sign in to comment.