SoftwareBrothers / adminjs

AdminJS is an admin panel for apps written in node.js
https://adminjs.co
MIT License
8.11k stars 655 forks source link

Filtering based on context with a Many to Many relation #1547

Open EliseLei opened 1 year ago

EliseLei commented 1 year ago

Hello everyone,

I have an issue with my current adminJS/TypeOrm implementation.

Context

Basically, I have 2 entities Foo and Bar which are linked with a Many-To-Many relationship

class Foo {

baz!: string;

@ManyToMany(() => Bar, (bar) => bar.id, {
    eager: true
  })
  @JoinTable()
bars!: Bar[]
}
class Bar {

qux!: string;

}

What I'm trying to achieve

I'm trying to filter records based on a given list of bar that I have in my context, so I plugged in a function filterRecords in my Foo resource:

export const FooResource: ResourceWithOptions = {
  resource: Foo,
  options: {
    id: 'Foo',
    properties: {
      baz: { isVisible: false },
      bars: {
        type: 'reference',
        reference: 'Bar',
      },
    },
    actions: {
      list: {
        before: filterRecords,
      },
    },
  },
};

Here is the implementation of my filterRecords function:

export const filterRecords = (
  request: ActionRequest,
  context: ActionContext
) => {
    const { query } = request;
   const newQuery = {
      ...query,
   } ;
  newQuery['filters.bars'] = context.foo.bars;
  request.query = newQuery;
  return request;
};

The problem

This is not working because I'm getting an error:

TypeError: Cannot read properties of undefined (reading 'type')
    at Object.isParserForType (/node_modules/.pnpm/@adminjs+typeorm@4.0.0/node_modules/@adminjs/typeorm/src/utils/filter/date-filter.parser.ts:5:78)
    at /node_modules/.pnpm/@adminjs+typeorm@4.0.0/node_modules/@adminjs/typeorm/src/utils/filter/filter.converter.ts:17:42

However, if I try to filter with the baz property such as:

export const filterRecords = (
  request: ActionRequest,
  context: ActionContext
) => {
    const { query } = request;
   const newQuery = {
      ...query,
   } ;
  newQuery['filters.baz'] =  'baz';
  request.query = newQuery;
  return request;
};

It works just fine, I believe there is something I'm not doing right with the context filtering with a Many-To-Many relationship.

Anyone faced this issue before?

carmof commented 2 months ago

Any solution for this? I'm facing the same issue