tommybananas / finale

Create flexible REST endpoints and controllers from Sequelize models in your Express app
187 stars 36 forks source link

Bug: Nested search uses strict equal instead of like #98

Closed rrozza-apolitical closed 5 months ago

rrozza-apolitical commented 5 months ago

This bug can be reproduced with the following example:

Child Resource:

      const childEntity = finale.resource({
        model: childSchema,
        endpoints: 'child_entities',
        include: [{
          as: 'parent',
          attributes: ['name'],
          model: parentSchema,
        }],
        sort: {
          attributes: ['parent.name'], // allow sort only by parent.name
          default: 'parent.name', // default sort by parent.name
          param: 'sort',
        },
      });

Endpoint: /child-entity?slug=some-slug&parent.name=some-name

Query executed:

SELECT 
  "child_entities"."id",
  "child_entities"."slug",
  "child_entities"."parentId",
  "child_entities"."createdAt", 
  "child_entity"."updatedAt", 
FROM 
  "child_entities" AS "child_entity" 
  INNER JOIN "parents" AS "parent" ON "child_entity"."parentId" = "parent"."id" 
  AND "parent"."name" = 'some-name' 
WHERE 
  "child_entity"."slug" = 'some-slug' 
ORDER BY 
  "parent.name" ASC 
LIMIT 
  100 OFFSET 0;

The problem originates in the strict use of = at the time of joining the table, see AND "parent"."name" = 'some-name'

Expected query:

SELECT 
  "child_entities"."id",
  "child_entities"."slug",
  "child_entities"."parentId",
  "child_entities"."createdAt", 
  "child_entity"."updatedAt", 
FROM 
  "child_entities" AS "child_entity" 
  INNER JOIN "parents" AS "parent" ON "child_entity"."parentId" = "parent"."id" 
  AND "parent"."name" LIKE '%some-name%' 
WHERE 
  "child_entity"."slug" = 'some-slug' 
ORDER BY 
  "parent.name" ASC 
LIMIT 
  100 OFFSET 0;

Note that the difference at AND "parent"."name" LIKE '%some-name%'