nestjsx / crud

NestJs CRUD for RESTful APIs
https://github.com/nestjsx/crud/wiki
MIT License
4.01k stars 526 forks source link

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

Open qstyler opened 1 year ago

qstyler commented 1 year ago

…param

Allows @Crud decorator accept loadRelationIds query param that is later accepted by TypeORM.

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

[ ] Bugfix
[x] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Build related changes
[ ] Tests
[ ] Release
[ ] CI related changes
[ ] Other... Please describe:

What is the current behavior?

Issue Number: N/A

What is the new behaviour?

Introduces loadRelationIds, a new field in QueryOptions. That is passed to TypeORM.

This allows the loading of relations ids in objects without joining the whole table.

For example this controller:

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

getting all records will return this:

[
  {
    "userId": 1,
    "licenseId": 1,
    "yearsActive": 3,
    "license": {
      "id": 1
    }
  },
  {
    "userId": 1,
    "licenseId": 2,
    "yearsActive": 5,
    "license": {
      "id": 2
    }
  },
  {
    "userId": 1,
    "licenseId": 4,
    "yearsActive": 7,
    "license": {
      "id": 4
    }
  },
  {
    "userId": 2,
    "licenseId": 5,
    "yearsActive": 1,
    "license": {
      "id": 5
    }
  }
]

This is very useful for building rest APIs without @RelationId() and @Column() hacks.

Does this PR introduce a breaking change?

[ ] Yes
[x] No
afilp commented 8 months ago

@qstyler This repo seems to not be supported anymore, would you like to submit your PR to this forked repo, which is more up-to-date? Thanks!

https://github.com/gid-oss/dataui-nestjs-crud