Skip to content

Commit

Permalink
fix(repositories): add lean() to find operations
Browse files Browse the repository at this point in the history
without the lean() rest operations on returned object do not work as expected (lean returns the object without mongoose properties)
  • Loading branch information
AlmogVC committed Dec 21, 2019
1 parent 3f30a66 commit c248702
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
12 changes: 10 additions & 2 deletions src/service/service.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import ServiceModel from './service.model';

export default class ServiceRepository {
public static getOne(serviceFilter: Partial<IService>) {
return ServiceModel.findOne(serviceFilter).exec();
return ServiceModel.findOne(serviceFilter)
.lean()
.exec();
}

public static create(service: IService) {
Expand All @@ -15,6 +17,12 @@ export default class ServiceRepository {
delete updatedService.createdAt;
delete updatedService.id;

return ServiceModel.findOneAndUpdate({ name }, updatedService, { new: true });
return ServiceModel.findOneAndUpdate({ name }, updatedService, { new: true }).lean();
}

public static getAll() {
return ServiceModel.find({})
.lean()
.exec();
}
}
16 changes: 14 additions & 2 deletions src/serviceHost/serviceHost.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,22 @@ export default class ServiceHostRepository {
}

public static updateLastAliveDate(service: string, hostname: string) {
return ServiceHostModel.findOneAndUpdate({ service, hostname }, { lastAliveDate: new Date() }, { new: true });
return ServiceHostModel.findOneAndUpdate(
{ service, hostname },
{ lastAliveDate: new Date() },
{ new: true },
).lean();
}

public static getOne(service: string, hostname: string) {
return ServiceHostModel.findOne({ service, hostname });
return ServiceHostModel.findOne({ service, hostname })
.lean()
.exec();
}

public static getMany(serviceHostFiler: Partial<IServiceHost>) {
return ServiceHostModel.find(serviceHostFiler)
.lean()
.exec();
}
}

0 comments on commit c248702

Please sign in to comment.