Skip to content

Commit

Permalink
feat: allow default populate by id
Browse files Browse the repository at this point in the history
  • Loading branch information
etienne-bechara committed Apr 8, 2021
1 parent 40cdf07 commit 3a7ed30
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 1 deletion.
1 change: 1 addition & 0 deletions source/orm/orm.interface/orm.service.options.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export interface OrmServiceOptions<T> {
uniqueKey?: string[];
populate?: (keyof T)[] | string[];
populateById?: (keyof T)[] | string[];
}
2 changes: 2 additions & 0 deletions source/orm/orm.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,12 @@ export abstract class OrmService<Entity> {

/**
* Reads a single entity by its ID.
* Obeys a custom default populate different than default.
* @param id
* @param options
*/
public async readById(id: string, options: OrmReadOptions<Entity> = { }): Promise<Entity> {
options.populate = options.populate || this.serviceOptions.populateById;
const entity = await this.read(id, options);

if (!entity[0]) {
Expand Down
3 changes: 2 additions & 1 deletion source/test/company/company.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export class CompanyService extends OrmService<CompanyEntity> {
) {
super(companyRepository, {
uniqueKey: [ 'name' ],
populate: [ 'headquarter', 'branches', 'employees' ],
populate: [ 'employees' ],
populateById: [ 'headquarter', 'branches', 'employees' ],
});
}

Expand Down

0 comments on commit 3a7ed30

Please sign in to comment.