balena-io / pinejs

Generate rest APIs from natural language models
Apache License 2.0
63 stars 11 forks source link

Fix using $filter on PATCH&DELETE requests on translated properties #789

Closed joshbwlng closed 3 months ago

joshbwlng commented 3 months ago

Before this a PATCH w/ a $filter on a translated field would not use the correct field:

UPDATE "device"
SET "is pinned on-release" = $1
WHERE "device"."id" IN ((
    SELECT "device"."id"
    FROM (
        SELECT "device"."created at", "device"."id", <...rest resin fields>
        FROM (
            SELECT *, <resin computed terms>
            FROM "device"
        ) AS "device"
        WHERE <resin.device.update permissions>
    ) AS "device"
    WHERE NOT (
        ("device"."should be running-release") IS NOT NULL AND ("device"."should be running-release") = ($9)
    )
)) [ 9, 9, 2, 2, 3, 2, 3, 9 ]

with this change we start using the translated model in the WHERE "<resource>"."id" IN` of UPDATEs & DELETEs:

UPDATE "device"
SET "is pinned on-release" = $1
WHERE "device"."id" IN ((
    SELECT "device"."$modifyid"
    FROM (
        SELECT <...first group of v6 fields>,
            "device"."is pinned on-release" AS "should be running-release",
            <...rest v6 fields>,
            "device"."$modifyid"
        FROM (
            SELECT "device"."created at", "device"."id", <...rest resin fields>, "device"."$modifyid"
            FROM (
                SELECT *, <resin computed terms>, "device"."id" AS "$modifyid"
                FROM "device"
            ) AS "device"
            WHERE <resin.device.update permissions>
        ) AS "device"
    ) AS "device"
    WHERE NOT (
        ("device"."should be running-release") IS NOT NULL AND ("device"."should be running-release") = ($9)
    )
)) [ 9, 9, 2, 2, 3, 2, 3, 9 ]

Generated SQL & execution plans when combined v See: https://github.com/balena-io-modules/odata-to-abstract-sql/pull/154 See: https://gist.github.com/thgreasi/e3f20b90d01336f4e66053cd4bda6f7f

Change-type: patch See: https://balena.fibery.io/Work/Project/Server-side-pagination-for-devices-Cycle-4-673 See: https://balena.fibery.io/Work/Task/pinejs-Fix-using-$filter-on-PATCH-DELETE-requests-on-translated-properties-2090