Skip to content

Commit

Permalink
feat(repository): hasone relation delete and patch operations
Browse files Browse the repository at this point in the history
implemented new feauture delete and patch operations in hasone relation

"fix #2233"
  • Loading branch information
Raphael Dai committed Mar 17, 2019
1 parent f780525 commit 7c5163d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ describe('hasOne relation', () => {
const areDeleted = await controller.deleteCustomerAddress(
existingCustomerId,
);
expect(areDeleted.count).to.equal(1);
expect(areDeleted).to.deepEqual({count: 1});

await expect(
controller.findCustomerAddress(existingCustomerId),
Expand Down
27 changes: 11 additions & 16 deletions packages/repository/src/relations/has-one/has-one.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {Getter} from '@loopback/context';
import {DataObject, Options, Count} from '../../common-types';
import {Entity} from '../../model';
import {Filter, Where} from '../../query';
import { Getter } from '@loopback/context';
import { DataObject, Options, Count } from '../../common-types';
import { Entity } from '../../model';
import { Filter, Where } from '../../query';
import {
constrainDataObject,
constrainFilter,
EntityCrudRepository,
constrainWhere,
} from '../../repositories';
import {EntityNotFoundError} from '../../errors';
import { EntityNotFoundError } from '../../errors';

/**
* CRUD operations for a target repository of a HasMany relation
Expand Down Expand Up @@ -43,11 +43,10 @@ export interface HasOneRepository<Target extends Entity> {

/**
* Delete the related target model instance
* @param where Instances within the where scope are deleted
* @param options
* @returns A promise which resolves the deleted target model instances
*/
delete(where?: Where<Target>, options?: Options): Promise<Count>;
delete(options?: Options): Promise<Count>;

/**
* Patch the related target model instance
Expand All @@ -62,7 +61,7 @@ export class DefaultHasOneRepository<
TargetEntity extends Entity,
TargetID,
TargetRepository extends EntityCrudRepository<TargetEntity, TargetID>
> implements HasOneRepository<TargetEntity> {
> implements HasOneRepository<TargetEntity> {
/**
* Constructor of DefaultHasOneEntityCrudRepository
* @param getTargetRepository the getter of the related target model repository instance
Expand All @@ -72,7 +71,7 @@ export class DefaultHasOneRepository<
constructor(
public getTargetRepository: Getter<TargetRepository>,
public constraint: DataObject<TargetEntity>,
) {}
) { }

async create(
targetModelData: DataObject<TargetEntity>,
Expand All @@ -94,7 +93,7 @@ export class DefaultHasOneRepository<
): Promise<TargetEntity> {
const targetRepository = await this.getTargetRepository();
const found = await targetRepository.find(
Object.assign({limit: 1}, constrainFilter(filter, this.constraint)),
Object.assign({ limit: 1 }, constrainFilter(filter, this.constraint)),
options,
);
if (found.length < 1) {
Expand All @@ -104,13 +103,9 @@ export class DefaultHasOneRepository<
}
return found[0];
}

async delete(where?: Where<TargetEntity>, options?: Options): Promise<Count> {
async delete(options?: Options): Promise<Count> {
const targetRepository = await this.getTargetRepository();
return targetRepository.deleteAll(
constrainWhere(where, this.constraint as Where<TargetEntity>),
options,
);
return targetRepository.deleteAll(options);
}

async patch(
Expand Down

0 comments on commit 7c5163d

Please sign in to comment.