Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE]: Ability to use conditional OR across joined tables in the query function #2250

Open
bitaccesscomau opened this issue May 3, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@bitaccesscomau
Copy link

Describe what you want

Using the query function, you are unable to have a conditional OR across related tables.

// API endpoint things here

const offsetParam = Number(url.searchParams.get('offset')) || 0;
const limitParam = Number(url.searchParams.get('limit')) || 10;
const idParam = url.searchParams.get('id');
const handleParam = url.searchParams.get('handle');
const scannedValueParam = url.searchParams.get('scannedValue');
	
	
const result = await db.query.product.findMany({
        where: (product, { eq, and }) => and(
            idParam ? eq(product.id, idParam) : undefined,
            handleParam ? eq(product.handle, handleParam) : undefined,
	    scannedValueParam ? eq(product.handle, scannedValueParam) : undefined
        ),
        offset: offsetParam,
        limit: limitParam,
        with: {
            tag: {
		where: (tag, { eq }) => scannedValueParam ? eq(tag.tagValue, scannedValueParam) : undefined,
                columns: {
                    productId: false
                }
            }
        },
        orderBy: product.created
    });

In the above example I can only return results where the scannedValue matches both the handle and the tag, but not either one. I cannot reference the related tag table in the upper or lower where clause as would be expected if performing the query in raw SQL. The SQL-like pseudocode equivalent of what I am trying to achieve is as per below.

SELECT * FROM product
LEFT OUTER JOIN tag on product.id = tag.productId
WHERE (product.handle = scannedValue OR tag.tagValue = scannedValue)

Ideally as the relation is specified, the upper where property should have access to the related tables.

@bitaccesscomau bitaccesscomau added the enhancement New feature or request label May 3, 2024
@MorelSerge
Copy link

I find that in general, it is not possible to use any joined tables in the upper where clauses. It'll always replace the table name with the root table, creating invalid queries and erroring out.

My use case is rather simple:
Query a table (e.g. user) and join in a 1-1 table (e.g. user-information), but select only those users with a condition on the joined table (e.g. user-information.is-admin = true). I can't add a where clause to the with clause, because it's a 1-1 relationship, which apparently removes that functionality. I also can't add the check in the root where clause, because that introduces the bug of using the wrong table name.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants