ppetzold / nestjs-paginate

Pagination and filtering helper method for TypeORM repositories or query builders using Nest.js framework :book::paperclip:
MIT License
435 stars 98 forks source link

auto select primary keys if `select` prop was provided #960

Open samislam opened 2 months ago

samislam commented 2 months ago

Based on the documentation, if you want to enable the select feature through the URL query (localhost?select=...), you have to ensure doing two things: First you must add the select property into the paginate() function call:

paginate(query, repo, {
   sortableColumns: / ** /,
   select: /* this needs to be set*/, // ๐Ÿ‘ˆ
})

Second You need to ensure that in the select option, you're selecting the primary key, for example id:

paginate(query, repo, {
   sortableColumns: / ** /,
   select: ["id" ๐Ÿ‘ˆ , /* and optionally any other column */],
})

My suggestion is, instead of making it that way, let's make the library itself add the primary key internally, because without that, if someone requests:

localhost?select=username

It's not going to work and it will return every other column, which makes someone think that there is a bug.

My suggestion is that the library should handle it internally somehow like this:

const repoPrimaryKey = /* the logic to get the repository primary key */
const selectedColumns = new Set()
if(select !== undefined && Array.isArray(select)) {
  selectedColumns.forEach(columnName = selectedColumns.add(columnName))
  selectedColumns.add(repoPrimaryKey)
}
const isSelectEnabled = !!selectedColumns.length
// ... rest
ppetzold commented 2 months ago

happy to merge if you find a solution. as far as I remember the mandatory PK + client select being a subset of a mandatory server side set, were more like cheap fixes while we struggled to make a cleaner version work.