How to use sql template strings

await tx.update(inventoryItems).set({
        masterProductId: sql`
        CASE
          ${chunk
            .map((update) => `WHEN id in (${update.ids.map((id) => `'${id}'`)}) THEN '${update.masterProductId}'`)
            .join('\n')}
        END
        `
      });

Query: update "InventoryItem" set "masterProductId" = 
        CASE
          $1
        END
         -- params: ["WHEN id in ('450a5c19-af32-4ee6-a565-2ba6b4664549','742a257d-23da-4092-bfa6-ba7d8ba6a4f8','450a5c19-af32-4ee6-a565-2ba6b4664549','742a257d-23da-4092-bfa6-ba7d8ba6a4f8','ea8ecac1-c73a-4590-8cda-7ce4344a95ca','03dca948-f970-4a42-83c8-999e1a61e2b6') THEN '1232a182-035c-4534-bc4b-402f9a00bced'"]
Jan 11, 12:09:45PM debug: req.url /api/master-products/bulk/map/save 
Jan 11, 12:09:45PM error: PostgresError: syntax error at or near "END"


this should be resolving to valid sql
Was this page helpful?