inArray not working as expected
This works but looks like a (sql injection)
const tagsArray = selectedTags.map((tag) =>
'${tag}'
).join(', ');
where.push(sql${tagData.tags} @> ARRAY[${sql.raw(tagsArray)}]::text[]
);
Why doesn't this just work (tm)?
where.push(inArray(tagData.tags, selectedTags));26 Replies
Hum 🧐 that’s not expected
https://orm.drizzle.team/docs/operators#inarray
do you have a particular error?
Drizzle ORM - Filters
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
The
tagData
is a CTE which gives a type error for inArray
that says "no overload matches the call"
.
And then the query errors out with
with selectedTags
being [ "tag2" ]
Maybe I missed something: https://drizzle.run/n0mpj7jpps2yvbwwvua81mms
Do not hesitate to share a small repro, just to be sure
Drizzle Run
inArray from sub select - Drizzle Run
I'll try to reproduce it in playground. That's probably the easiest.
Here we go! That's most stripped down version that still shows it.
https://drizzle.run/g9pjwezeysxkxsfh6d83ayye
Drizzle Run
inArray from cte - Drizzle Run
Any idea, @Raphaël M (@rphlmr) ⚡ ?
It was because of the cast to ARRAY
I shared a safe
toArray
Thanks for the help. I would have never figured that out by myself.
I wished the
where.push(inArray(tagData.tags, selectedTags));
would just work
and TBH I don't fully understand why is doesn't yet.
tagData.tags should be an array and selectedTags also is an array
why is there a casting problem?inArray works with a list (from a query or a js array). It translates to a “in” operator.
Sql array are different and arrayContains translate to a “@ > {…}” 😅
I have a lot of these helpers in my projects, and I would love to have some of them in Drizzle.
I will try to take a deeper look into it. inArray works fine everywhere I need it.
But shouldn't this also work with an IN operator?
I don't quite get this
Why the
_T
?Just an eslint convention for unused declaration
https://drizzle.run/gct3zuow3i13kvm33rerlb3u
This is simpler.
Drizzle Run
inArray from cte - Drizzle Run
no no, not the
_
.. but the T
itself if it isn't even used.
...and I also get an type warning despite
but that sounds more like an eslint
config issue.
man, your utility functions are golden. thanks for those.
but I still don't quite understand the why that approach works and my previous approach doesn't
It could be
[P in PGCastTypes]: P extends
${string}[] ? P : never;
.Maybe it was not possible with the TS version when I first did it (2 years ago).Because you aggregated the data in the CTE and not in the final result.
in
works with a scalar column but not an arrayinteresting ... sounds like I need to read up on that difference ... first time I am trying it like that
also curious to see if there is a big difference in the execution plan
I always try to aggregate in the last query
now that we have views, it can be simpler to pre-build complex ... views 😄
that does feel counter intuitive to me though ... it seems aggregating as early as possible should scale better ... but that's really just my unsubstantiated gut feeling 🙃
drizzle supports views?
yes since monday!
and check constraints :p
ok ... that is recent 🙂
Yes, after a long wait :tired: