adminjs icon indicating copy to clipboard operation
adminjs copied to clipboard

Filtering based on context with a Many to Many relation

Open EliseLei opened this issue 1 year ago • 1 comments

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/@[email protected]/node_modules/@adminjs/typeorm/src/utils/filter/date-filter.parser.ts:5:78)
    at /node_modules/.pnpm/@[email protected]/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?

EliseLei avatar Aug 17 '23 21:08 EliseLei

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

carmof avatar May 23 '24 18:05 carmof