Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(loadrelationids): @Crud decorator accepts loadRelationIds query … #813

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion packages/crud-typeorm/src/typeorm-crud.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,11 @@ export class TypeOrmCrudService<T> extends CrudService<T> {
// search
this.setSearchCondition(builder, parsed.search);

// set loadRelationIds
if (options.query.loadRelationIds) {
builder.loadAllRelationIds(options.query.loadRelationIds);
}

// set joins
const joinOptions = options.query.join || {};
const allowedJoins = objKeys(joinOptions);
Expand Down Expand Up @@ -810,7 +815,7 @@ export class TypeOrmCrudService<T> extends CrudService<T> {

protected getFieldWithAlias(field: string, sort = false) {
/* istanbul ignore next */
const i = ['mysql','mariadb'].includes(this.dbName) ? '`' : '"';
const i = ['mysql', 'mariadb'].includes(this.dbName) ? '`' : '"';
const cols = field.split('.');

switch (cols.length) {
Expand Down
12 changes: 12 additions & 0 deletions packages/crud-typeorm/test/__fixture__/user.license.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';

import { TypeOrmCrudService } from '../../../crud-typeorm/src/typeorm-crud.service';
import { UserLicense } from '../../../../integration/crud-typeorm/users-licenses';

@Injectable()
export class UserLicenseService extends TypeOrmCrudService<UserLicense> {
constructor(@InjectRepository(UserLicense) repo) {
super(repo);
}
}
33 changes: 32 additions & 1 deletion packages/crud-typeorm/test/b.query-params.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ import { Project } from '../../../integration/crud-typeorm/projects';
import { User } from '../../../integration/crud-typeorm/users';
import { UserProfile } from '../../../integration/crud-typeorm/users-profiles';
import { Note } from '../../../integration/crud-typeorm/notes';
import { UserLicense } from '../../../integration/crud-typeorm/users-licenses';
import { HttpExceptionFilter } from '../../../integration/shared/https-exception.filter';
import { Crud } from '../../crud/src/decorators';
import { CompaniesService } from './__fixture__/companies.service';
import { ProjectsService } from './__fixture__/projects.service';
import { UsersService, UsersService2 } from './__fixture__/users.service';
import { NotesService } from './__fixture__/notes.service';
import { UserLicenseService } from './__fixture__/user.license.service';

// tslint:disable:max-classes-per-file
describe('#crud-typeorm', () => {
Expand Down Expand Up @@ -163,11 +165,25 @@ describe('#crud-typeorm', () => {
constructor(public service: NotesService) {}
}

@Crud({
model: { type: UserLicense },
query: {
loadRelationIds: {
relations: ['license'],
disableMixedMap: true,
},
},
})
@Controller('user.licenses')
class UserLicenseController {
constructor(public service: UserLicenseService) {}
}

beforeAll(async () => {
const fixture = await Test.createTestingModule({
imports: [
TypeOrmModule.forRoot({ ...withCache, logging: false }),
TypeOrmModule.forFeature([Company, Project, User, UserProfile, Note]),
TypeOrmModule.forFeature([Company, Project, User, UserProfile, Note, UserLicense]),
],
controllers: [
CompaniesController,
Expand All @@ -179,6 +195,7 @@ describe('#crud-typeorm', () => {
UsersController2,
UsersController3,
NotesController,
UserLicenseController,
],
providers: [
{ provide: APP_FILTER, useClass: HttpExceptionFilter },
Expand All @@ -187,6 +204,7 @@ describe('#crud-typeorm', () => {
UsersService2,
ProjectsService,
NotesService,
UserLicenseService,
],
}).compile();

Expand Down Expand Up @@ -376,6 +394,19 @@ describe('#crud-typeorm', () => {
});
});

describe('#query loadRelationIds', () => {
it('should return relation objects with only ids', (done) => {
request(server)
.get('/user.licenses')
.end((_, res) => {
expect(res.status).toBe(200);
expect(res.body[0].license.name).toBeUndefined();
expect(res.body[0].license.id).toBeDefined();
done();
});
});
});

describe('#query nested join', () => {
it('should return status 400, 1', (done) => {
const query = qb
Expand Down
11 changes: 7 additions & 4 deletions packages/crud/src/interfaces/query-options.interface.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import {
QueryFields,
QuerySort,
} from '@nestjsx/crud-request/lib/types/request-query.types';
import { QueryFields, QuerySort } from '@nestjsx/crud-request/lib/types/request-query.types';

import { QueryFilterOption } from '../types';

Expand All @@ -11,6 +8,7 @@ export interface QueryOptions {
persist?: QueryFields;
filter?: QueryFilterOption;
join?: JoinOptions;
loadRelationIds?: LoadRelationIdsOptions;
sort?: QuerySort[];
limit?: number;
maxLimit?: number;
Expand All @@ -32,3 +30,8 @@ export interface JoinOption {
select?: false;
required?: boolean;
}

export interface LoadRelationIdsOptions {
relations?: string[];
disableMixedMap?: boolean;
}