Skip to content

Commit

Permalink
fix(typeorm): fixed left join by default
Browse files Browse the repository at this point in the history
Fixed using left join by default. Added "required" param to the join options

fix #31, #98
  • Loading branch information
michaelyali committed Oct 2, 2019
1 parent f54ea53 commit 3307348
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 19 deletions.
3 changes: 2 additions & 1 deletion integration/crud-typeorm/seeds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ export class Seeds1544303473346 implements MigrationInterface {
('[email protected]', false, 2, 17, NULL, NULL),
('[email protected]', false, 2, 18, NULL, NULL),
('[email protected]', false, 2, 19, NULL, NULL),
('[email protected]', false, 2, 20, NULL, NULL);
('[email protected]', false, 2, 20, NULL, NULL),
('[email protected]', false, 2, NULL, NULL, NULL);
`);
}

Expand Down
16 changes: 2 additions & 14 deletions packages/crud-typeorm/src/typeorm-crud.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,6 @@ export class TypeOrmCrudService<T> extends CrudService<T> {
...hash,
[curr.propertyName]: {
name: curr.propertyName,
type: this.getJoinType(curr.relationType),
columns: curr.inverseEntityMetadata.columns.map((col) => col.propertyName),
referencedColumn: (curr.joinColumns.length
? curr.joinColumns[0]
Expand All @@ -407,17 +406,6 @@ export class TypeOrmCrudService<T> extends CrudService<T> {
);
}

private getJoinType(relationType: string): string {
switch (relationType) {
case 'many-to-one':
case 'one-to-one':
return 'innerJoin';

default:
return 'leftJoin';
}
}

private async getOneOrFail(req: CrudRequest): Promise<T> {
const { parsed, options } = req;
const builder = await this.createBuilder(parsed, options);
Expand Down Expand Up @@ -539,7 +527,6 @@ export class TypeOrmCrudService<T> extends CrudService<T> {

this.entityRelationsHash[cond.field] = {
name: curr.propertyName,
type: this.getJoinType(curr.relationType),
columns: curr.inverseEntityMetadata.columns.map((col) => col.propertyName),
referencedColumn: (curr.joinColumns.length
? /* istanbul ignore next */ curr.joinColumns[0]
Expand Down Expand Up @@ -572,8 +559,9 @@ export class TypeOrmCrudService<T> extends CrudService<T> {
].map((col) => `${relation.name}.${col}`);

const relationPath = relation.nestedRelation || `${this.alias}.${relation.name}`;
const relationType = options.required ? 'innerJoin' : 'leftJoin';

builder[relation.type](relationPath, relation.name);
builder[relationType](relationPath, relation.name);
builder.addSelect(select);
}

Expand Down
59 changes: 55 additions & 4 deletions packages/crud-typeorm/test/0.basic-crud.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,49 @@ describe('#crud-typeorm', () => {
constructor(public service: UsersService) {}
}

@Crud({
model: { type: User },
query: {
join: {
profile: {
eager: true,
required: true,
},
},
},
})
@Controller('/users2')
class UsersController2 {
constructor(public service: UsersService) {}
}

@Crud({
model: { type: User },
query: {
join: {
profile: {
eager: true,
},
},
},
})
@Controller('/users3')
class UsersController3 {
constructor(public service: UsersService) {}
}

beforeAll(async () => {
const fixture = await Test.createTestingModule({
imports: [
TypeOrmModule.forRoot({ ...withCache, logging: false }),
TypeOrmModule.forFeature([Company, Project, User, UserProfile]),
],
controllers: [CompaniesController, UsersController],
controllers: [
CompaniesController,
UsersController,
UsersController2,
UsersController3,
],
providers: [
{ provide: APP_FILTER, useClass: HttpExceptionFilter },
CompaniesService,
Expand Down Expand Up @@ -328,7 +364,7 @@ describe('#crud-typeorm', () => {
it('should return updated entity, 2', (done) => {
const dto = { isActive: false, companyId: 5 };
return request(server)
.patch('/companies/1/users/21')
.patch('/companies/1/users/22')
.send(dto)
.end((_, res) => {
expect(res.status).toBe(200);
Expand Down Expand Up @@ -375,14 +411,29 @@ describe('#crud-typeorm', () => {
});
it('should return deleted entity', (done) => {
return request(server)
.delete('/companies/1/users/21')
.delete('/companies/1/users/22')
.end((_, res) => {
expect(res.status).toBe(200);
expect(res.body.id).toBe(21);
expect(res.body.id).toBe(22);
expect(res.body.companyId).toBe(1);
done();
});
});
});

describe('join options: required', () => {
const users2 = () => request(server).get('/users2/21');
const users3 = () => request(server).get('/users3/21');

it('should return status 404', async () => {
await users2().expect(404);
});

it('should return status 200', async () => {
const res = await users3().expect(200);
expect(res.body.id).toBe(21);
expect(res.body.profile).toBe(null);
});
});
});
});
1 change: 1 addition & 0 deletions packages/crud/src/interfaces/query-options.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ export interface JoinOption {
exclude?: QueryFields;
persist?: QueryFields;
eager?: boolean;
required?: boolean;
}

0 comments on commit 3307348

Please sign in to comment.