Implementing search functionality in Prisma/GraphQL
Asked Answered
N

3

8

I want to implement a simple searching functionality with Prisma I have seen some info about using the where clause but it’s case sensitive so it’s not a good solution.

I’ve also seen some tutorials using external services. I don’t want to use any external services. I’d like to do something as simple as possible.

Any way I can tweak the query to be case insensitive? or any other approach you guys recommend?

Thanks :)

Negotiation answered 5/12, 2018 at 4:58 Comment(0)
P
5

This feature isn't implemented yet: https://github.com/prisma/prisma1/issues/1183

However, you can have a raw access to your database if it supports this feature: https://www.prisma.io/docs/prisma-graphql-api/reference/raw-database-access-qwe4/#overview

Pylorectomy answered 5/12, 2018 at 14:56 Comment(4)
Great response, thanks a lot for pointing to the relevant issues!Migrant
thank you for your help Errorname. it's a bit discouraging that every "simple" thing I try to do with Prisma it's not yet implemented 😭 I also tried with the raw access... but I wasn't able to get it working... anyways thank for the information.Negotiation
Prisma is a "young" technology, but it shows great promise. Which is why more and more developers are using Prisma. But that also mean there is more and more various feature requests, and more and more pressures on the team! They are hiring, but it's not an easy task to scale a team :) I guess we just have to be patientPylorectomy
@Pylorectomy do you know if this solution is up to date? In 2020 is there a case insensitive way to query?Toshikotoss
R
2

Try mode

const users = await prisma.user.findMany({
  where: {
    email: {
      endsWith: "prisma.io",
      mode: "insensitive", // Default value: default
    },
  },
});
Rhinencephalon answered 27/5, 2021 at 5:56 Comment(0)
B
0

You can do search-like queries using the prisma client. Here is an example of an auto-generated interface for the where query parameter of a User entity in one of my apps.

export interface UserWhereInput {
  name?: String;
  name_not?: String;
  name_in?: String[] | String;
  name_not_in?: String[] | String;
  name_lt?: String;
  name_lte?: String;
  name_gt?: String;
  name_gte?: String;
  name_contains?: String;
  name_not_contains?: String;
  name_starts_with?: String;
  name_not_starts_with?: String;
  name_ends_with?: String;
  name_not_ends_with?: String;
  ...
  AND?: UserWhereInput[] | UserWhereInput;
}

Note name_contains and name_starts_with. These are both valid to use for a simple search, here's an example resolver -

const userSearchResolver = async (_root, _args, _context) => {
    return await _context.prisma.users({
      where: {
        name_starts_with: _args.searchQuery
      }
    });
  }
);
Bedspring answered 10/3, 2019 at 22:43 Comment(1)
Just realized you were looking for case-sensitive search... sorry... I'd recommend making a separate database client to connect to the prisma database not through prisma client, and just do a text search using the standard db client. For example, if you want to use mongodb for prisma, you can just use the mongo client in your resolver to perform a text searchBedspring

© 2022 - 2024 — McMap. All rights reserved.