Skip to content

Commit

Permalink
feat(repository): hasOne relation patch and delete operations
Browse files Browse the repository at this point in the history
implement patch and delete operations in hasOne relation

"fix loopbackio#2233"
  • Loading branch information
Raphael Dai committed Mar 12, 2019
1 parent a3e77e8 commit f780525
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,17 +116,13 @@ describe('hasOne relation', () => {
});

it('can PATCH hasOne instances', async () => {
// existingCustomerId = 11;
const address = await controller.createCustomerAddress(existingCustomerId, {
street: '1 Amedee Bonnet',
zipcode: '69740',
city: 'Genas',
province: 'Rhone',
});

// const persisted = await addressRepo.findById(address.zipcode);
// expect(persisted.toObject()).to.deepEqual(address.toObject());

const patchObject = {city: 'Lyon-Genas'};
const arePatched = await controller.patchCustomerAddress(
existingCustomerId,
Expand All @@ -135,10 +131,10 @@ describe('hasOne relation', () => {

expect(arePatched).to.deepEqual({count: 1});
const patchedData = await addressRepo.findById(address.zipcode);
expect(patchedData.city).to.containEql(address.city);
expect(patchedData.city).to.equal('Lyon-Genas');
});

it('can throws error when query tries to change the foreignKey', async () => {
it('throws an error when PATCH tries to change the foreignKey', async () => {
try {
await expect(
controller.patchCustomerAddress(existingCustomerId, {
Expand All @@ -149,17 +145,13 @@ describe('hasOne relation', () => {
});

it('can DELETE hasOne relation instances', async () => {
// existingCustomerId = 12;
const address = await controller.createCustomerAddress(existingCustomerId, {
await controller.createCustomerAddress(existingCustomerId, {
street: '1 Amedee Bonnet',
zipcode: '69740',
city: 'Genas',
province: 'Rhone',
});

const newData = await addressRepo.findById(address.zipcode);
expect(newData.city).to.containEql(address.city);

const areDeleted = await controller.deleteCustomerAddress(
existingCustomerId,
);
Expand Down
10 changes: 0 additions & 10 deletions packages/repository/src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,16 +156,6 @@ export class ModelDefinition {
);
return idProps;
}

/**
* Returns the primary key(s) of a given model.
* Implemented by Raphael Drai for the purpose of PUT operations
* in relations models.
*
*/
// idNames(): string[] {
// return this.idProperties();
// }
}

function asJSON(value: any): any {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ export interface HasOneRepository<Target extends Entity> {
): Promise<Target>;

/**
* Delete multiple target model instances
* 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>;

/**
* Patch related target model instance
* Patch the related target model instance
* @param dataObject The target model fields and their new values to patch
* @param options
* @returns A promise which resolves the patched target model instances
Expand Down Expand Up @@ -119,7 +119,7 @@ export class DefaultHasOneRepository<
): Promise<Count> {
const targetRepository = await this.getTargetRepository();
return await targetRepository.updateAll(
dataObject,
constrainDataObject(dataObject, this.constraint),
constrainWhere({}, this.constraint as Where<TargetEntity>),
);
}
Expand Down

0 comments on commit f780525

Please sign in to comment.