X
Xata10mo ago
Raveen

Query syntax in Typescript sdk

Is there any way I can do this using the typescript sdk ? Basically want to filter using calculated value SELECT *, (1 - (discounted_price / original_price)) * 100 AS discount_percentage FROM products WHERE (1 - (discounted_price / original_price)) * 100 > 20;
2 Replies
kostas
kostas10mo ago
Hi, with the Typescript SDK filters cannot use computed values so the discount_percentage would need to be stored as an actual column value in order to use it in a filter. There is a feature request for generated columns (https://feedback.xata.io/feature-requests/p/generated-columns) which would automatically compute and store the discount_percentage based on the computation definition for that column. We are considering this, and it will be possible to do it when full Postgres SQL access becomes available later this year. Right now, you can use the sql method (https://xata.io/docs/sdk/sql/overview#fetch-all-records) to run this with SQL:
const results = await xata.sql`SELECT (1-(discounted_price/original_price))*100 AS discount_percentage FROM products WHERE (1-(discounted_price/original_price))*100 > 20`;
const results = await xata.sql`SELECT (1-(discounted_price/original_price))*100 AS discount_percentage FROM products WHERE (1-(discounted_price/original_price))*100 > 20`;
Raveen
Raveen10mo ago
Great! that new feature looks promising, thanks for your time
Want results from more Discord servers?
Add your server