Suggested way to query equivalent to prisma's findUnique

In prisma, if I specify an index on 3 fields (say field1, field2, and field3), I am able to query on that unique combination and do something like so:
await prisma.someTable.findUnique({
where: {
field1_field2_field3: { // This type requires all 3 specified
field1: 'asdf',
field2: 'asdf',
field3: 'asdf'
}
}
});
await prisma.someTable.findUnique({
where: {
field1_field2_field3: { // This type requires all 3 specified
field1: 'asdf',
field2: 'asdf',
field3: 'asdf'
}
}
});
Is there anything analogous in drizzle? The only workaround I see online is to just create a new field to represent the index, but that seems very hacky.
4 Replies
jakeleventhal
jakeleventhalOP11mo ago
I believe this may just be a missing feature. There should be some way to have a findUnique function that is essentially a copy of findFirst except with different type defs for the where clause based on where the unique indexes are defined.
jakeleventhal
jakeleventhalOP11mo ago
Found the most relevant issue and posted a comment since most people were being a bit unclear in the comments: https://github.com/drizzle-team/drizzle-orm/issues/742#issuecomment-1877096040
GitHub
[FEATURE]: Add findUnique to relational queries · Issue #742 · driz...
Describe what you want Hello 👋, Relational queries are pushing the DX of Drizzle closer than Prisma, I love it! In order to close the gap, it would be great to have findUnique implemented. It's...
Angelelz
Angelelz11mo ago
There is still no feature parity between Prisma and drizzle yet. I personally don't have any idea what that API is doing for you under the hood? Is it just filtering by those 3 fields? Is it looking at your actual indexes making sure is unique? Does it throw and error if more that one is received back? Does it add a limit 1 to the query? I would suggest to find out what is it that you need from findUnique exactly, in terms of SQL queries, and go from there.
jakeleventhal
jakeleventhalOP11mo ago
i will clarify in a comment in the GH thread added very detailed comment with type interface + examples
Want results from more Discord servers?
Add your server