Magic sql`` operator array

hey guys. how can I pass a array fo strings to magic sql operator?
9 Replies
Angelelz
Angelelz8mo ago
Depends on how are you planning to use it
HAL 9000
HAL 90008mo ago
I am going to check if a column value is in this array. I just want to pass it safely because the array comes from url search params
Angelelz
Angelelz8mo ago
Use inArray You only need to check that length in > 0 inArray will do that parsing for you
HAL 9000
HAL 90008mo ago
The column I am trying to to check is JSONB tough @Angelelz I can’t use inArray because the column value is JSONB. I don’t know how to exactly use with JSONB columns
Angelelz
Angelelz8mo ago
Oh hey, yeah So, you have a jsonb column, and now you want to see if that jsonb exists in an array? Or do you have a value, and you're trying to see if it's present in a jsonb column that is an array?
HAL 9000
HAL 90008mo ago
first of all, thanks for the reply, I have a JSONB column with the following structure: { "value": "Completed" } and I get array of values from url search params and pass it to a function where I do the database query using drizzle orm ["Completed", "In Progress", "Cancelled"]. I want to check the value field of JSONB column exists in this array of values for filtering purposes.
Angelelz
Angelelz8mo ago
Try this:
inArray(sql`${table.column}->>'value'`, yourArray)
inArray(sql`${table.column}->>'value'`, yourArray)
HAL 9000
HAL 90008mo ago
Thanks a lot, I'll try it. One more thing, which is completely unrelated to this: I haven't been able to find information on how to make a unique constraint deferrable in the documentation.
Angelelz
Angelelz8mo ago
I don't have experience with this concept. I don't think drizzle supports it. Editing migrations would be the way to go, I would think
Want results from more Discord servers?
Add your server