prisma icon indicating copy to clipboard operation
prisma copied to clipboard

Support case insensitive advanced `Json` filtering

Open Pehesi97 opened this issue 4 years ago • 30 comments

Bug description

There is no support for case insensitive advanced JSON filtering on Prisma v2.23.0.

How to reproduce

Run any query filtering JSON fields and see that "string_contains" is case sensitive.

{
  applicantData :{
    path: ["details", "lastName"],
    string_contains: searchString,
  }
}

Expected behavior

There should be a field mode for this field type too (currently it's possible to filter other field types with case insensitivity)

Prisma information

It shouldn't be necessary to share Prisma information for this case.

Environment & setup

  • OS: Mac OSX, Apple Silicon
  • Database: PostgreSQL
  • Node.js version: Node v15.14.0

Prisma Version

prisma               : 2.23.0
@prisma/client       : 2.23.0
Current platform     : darwin
Query Engine         : query-engine adf5e8cba3daf12d456d911d72b6e9418681b28b (at node_modules/@prisma/engines/query-engine-darwin)
Migration Engine     : migration-engine-cli adf5e8cba3daf12d456d911d72b6e9418681b28b (at node_modules/@prisma/engines/migration-engine-darwin)
Introspection Engine : introspection-core adf5e8cba3daf12d456d911d72b6e9418681b28b (at node_modules/@prisma/engines/introspection-engine-darwin)
Format Binary        : prisma-fmt adf5e8cba3daf12d456d911d72b6e9418681b28b (at node_modules/@prisma/engines/prisma-fmt-darwin)
Default Engines Hash : adf5e8cba3daf12d456d911d72b6e9418681b28b
Studio               : 0.393.0
Preview Features     : filterJson

Pehesi97 avatar Jun 01 '21 03:06 Pehesi97

Is there a way to do this yet?

palashCItobuz avatar Nov 24 '21 11:11 palashCItobuz

We need this feature as well. Is there any workaround for now?

turakvlad avatar Mar 30 '22 13:03 turakvlad

We need this feature as well. Any plan to add this feature?

endo64 avatar Jun 07 '22 07:06 endo64

We need this as well, anything in the near future planned?

redtailryan avatar Jun 07 '22 16:06 redtailryan

How is this feature not implemented yet? Its crucial in some applications.

VitoMedlej avatar Jun 23 '22 09:06 VitoMedlej

This feature would be very neat to have. For string_contains and the other string ones. The array ones too ideally

flesler avatar Jun 24 '22 15:06 flesler

for MySQL, following this...: https://stackoverflow.com/a/59000485/694133 I tested it manually and it worked successfully... I will research how to implement it and try to submit a PR. But if someone can get it sooner that would be great :)

jeffdrumgod avatar Aug 28 '22 00:08 jeffdrumgod

Any updates on this feature??

shokhboz-abdullaev avatar Dec 01 '22 06:12 shokhboz-abdullaev

Hi, I would like to request this feature as well or understand a workaround if anyone has one.

pagreczner avatar Dec 28 '22 22:12 pagreczner

Would love this feature as well.

tbell511 avatar Dec 31 '22 17:12 tbell511

Bump on this

ajhollowayvrm avatar Mar 02 '23 19:03 ajhollowayvrm

I would love this feature as well!

rikardkling avatar Mar 02 '23 19:03 rikardkling

Totally waiting for this feature too!

vlapo avatar Mar 03 '23 07:03 vlapo

It would be great to have indeed.

multipliedtwice avatar Mar 07 '23 08:03 multipliedtwice

It's quite surprising seeing something like this left out. As bad as "bumps" are, I don't see a single contributor-made comment here and the issue has existed for almost two years.

kindlyfire avatar May 09 '23 10:05 kindlyfire

I am a maintainer here. We are aware this issue exists and we added labels to it to reflect that we understood it. When we have capacity and this gets enough priority among all our issues, we might implement it in the future. No more bumps necessary unless they add new information or additional use cases. Thank you.

janpio avatar May 17 '23 19:05 janpio

Is there any workaround?

FacundoSpira avatar Aug 24 '23 19:08 FacundoSpira

Hello, Does anyone have work around on this one?

raaauf6933 avatar Sep 15 '23 04:09 raaauf6933

No more bumps necessary unless they add new information or additional use cases.

The use case we have is that we're storing email addresses as JSON so that they can include name, i.e. {"name": "Foo Bar", "address": "[email protected]"}. For inbound emails, we need to match with a contact for association and since email RFC states that addresses are case-insensitive, we're not able to get a match using Prisma when the address has characters with different casing.

Looks like we'll end up converting all addresses to lowercase before saving to database but that does mean we're making a change to the user data which we'd have preferred not to do even if it's essentially the same from definition point of view.

muvaf avatar Sep 22 '23 08:09 muvaf

help us please

baladao avatar Nov 14 '23 19:11 baladao

We also have a use case, though maybe theres a better option out there for us? We have some data stored in JSON that a user can search for. e.g.

{
    name: "John Doe",
    position: "Software Engineer"
}

Our users may want to search for "engineer", but nothing would show up without specifically searching for "Engineer".

In the mean time our work around is to try search for the original value, the lower case version, the upper case version and the capitalised version. This isn't perfect but hits most of our searches pretty well

{
	OR: [
		{
			data: {
				path: [filter],
				string_contains: value,
			},
		},
		{
			data: {
				path: [filter],
				string_contains: value.toLowerCase(),
			},
		},
		{
			data: {
				path: [filter],
				string_contains: value.toUpperCase(),
			},
		},
		{
			data: {
				path: [filter],
				string_contains:
					value.toLowerCase().charAt(0).toUpperCase() +
					value.slice(1),
			},
		},
	],
}

If theres something else we could do for our use case please let me know

spencer-robertson avatar Dec 11 '23 02:12 spencer-robertson

i'd also love this. searching on an open body in a json field property is tricky and needs to be case insensitive

LizDodion avatar Dec 20 '23 09:12 LizDodion

Need this ASAP, any updates or work around ?

mwibutsa-koin avatar Jan 16 '24 14:01 mwibutsa-koin

Need this ASAP, any updates or work around ?

Have you tried to keep lowercased copy in another column and match them by id?

multipliedtwice avatar Jan 16 '24 16:01 multipliedtwice

Need this ASAP, any updates or work around ?

Have you tried to keep lowercased copy in another column and match them by id?

Yeah, that's what I did. but it's still an ugly solution. but it works for the time being.

dmitriyzhuk avatar Feb 16 '24 17:02 dmitriyzhuk

We also have a use case, though maybe theres a better option out there for us? We have some data stored in JSON that a user can search for. e.g.

Thank you @spencer-robertson ! This was very helpful to us, as this covers the vast majority of our cases in the mean time without requiring data-side hacks. It seems there is a small cost in performance, but it seems worth it.

If anyone else would find this useful, I created a utility to make this method easier to use as needed

const JsonMultiCaseQuery = (path: string[], string_contains: string, jsonProperty: string): {OR: Prisma.Enumerable<{[x: string]: Prisma.JsonFilter}>} => ({
  OR: [
    // as TypEd
		{
			[jsonProperty]: {
				path,
				string_contains,
			},
		},
    // lower case
		{
			[jsonProperty]: {
				path,
				string_contains: string_contains.toLowerCase(),
			},
		},
    // UPPER CASE
		{
			[jsonProperty]: {
				path,
				string_contains: string_contains.toUpperCase(),
			},
		},
    // Capitalized
		{
			[jsonProperty]: {
				path,
				string_contains:
					string_contains.charAt(0).toUpperCase() +
					string_contains.toLowerCase().slice(1),
			},
		},
	],
});

If anyone has suggestions on better typing, I'd welcome feedback. Prisma is new to me and I wanted to avoid typing this specifically for a given model or schema with generated types. I tried generic jsonProperty: T and keying with a mapped type [x in T], but ran into some issues, so went with a string for simplicity for now.

johnMorone avatar Feb 20 '24 20:02 johnMorone

oof

egeste avatar Mar 09 '24 04:03 egeste

we need this feature. please do it :)

sefaun avatar Jul 10 '24 12:07 sefaun

Bump

birosrichard avatar Jul 11 '24 21:07 birosrichard

+1 forced to use raw query 😔

hengkydev avatar Jul 20 '24 08:07 hengkydev